CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2001
    Posts
    3

    Colors in Memory DC

    Hi there,
    I'm calling the lineTo API function on memory DC created by a call to CreateCompatibleDC. After a BitBlt to a PictureBox I can only get a black line, even after doing a CreatePen and SelectObject on the memory DC for a colored pen. Line styles work but not the color. I'm doing a vbSrcCopy on the BitBlt. What am I missing?
    Thanks,
    StuartD.


  2. #2
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Colors in Memory DC

    The CreatePen is what should set your foreground (Line) color. Here is a snippet from a routine I use to draw colored lines

    Case DrawLine ' Draw a line
    Dim pt as POINTAPI
    If otype = ePrinter then
    m = (TwipsPerInch / Printer.TwipsPerPixelX) ' determined in points
    else
    m = PixelsPerInch
    End If
    hndpen = CreatePen(oElements(X).BorderStyle, oElements(X).width, oElements(X).fgColor)
    oldpen = SelectObject(picHDC, hndpen)
    ' Move currentx and currenty to starting position
    MoveToEx picHDC, (oElements(X).X * m) * LtoRRatio, _
    (oElements(X).Y * m) * TtoBRatio, pt
    ' draw Line
    LineTo picHDC, (oElements(X).bX * m) * LtoRRatio, (oElements(X).bY * m) * TtoBRatio
    ' Cleanup
    hndpen = SelectObject(picHDC, oldpen)
    DeleteObject hndpen




    John G

  3. #3
    Join Date
    Oct 2001
    Posts
    3

    Re: Colors in Memory DC

    Thanks for the response. That's more or less what I'm doing.

    If you paste the code below into a new project with a Picture Box control called Picture1... Then I get a black line when I've created a red pen to draw it - I guess it is something to do with the BitBlt or the creation of the memory DC ???

    Thanks,
    StuartD.

    Private Type POINT_TYPE
    x As Long
    y As Long
    End Type

    Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
    Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
    Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    Private Declare Function BitBlt Lib "gdi32" (ByVal hDCDest As Long, ByVal XDest As Long, ByVal YDest As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hDCSrc As Long, ByVal XSrc As Long, ByVal YSrc As Long, ByVal dwRop As Long) As Long
    Private Declare Function MoveToEx Lib "gdi32.dll" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, lpPoint As POINT_TYPE) As Long
    Private Declare Function LineTo Lib "gdi32.dll" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
    Private Declare Function CreatePen Lib "gdi32.dll" (ByVal fnPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As Long
    Private Declare Function DeleteObject Lib "gdi32.dll" (ByVal hObject As Long) As Long
    Private Declare Function CreateSolidBrush Lib "gdi32.dll" (ByVal crColor As Long) As Long
    Private Declare Function Rectangle Lib "gdi32.dll" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long



    Private Sub Form_Load()
    Dim hMemDC As Long

    hMemDC = CreateCompatibleDC(Picture1.hdc)

    If (hMemDC <> 0) Then
    Dim hOldBitMap As Long

    hBitMap = CreateCompatibleBitmap(hMemDC, 500, 500)

    If (hBitMap <> 0) Then
    Dim newPen As Long
    Dim oldPen As Long
    Dim pt As POINT_TYPE
    Dim oldBrush As Long
    Dim newBrush As Long

    hOldBitMap = SelectObject(hMemDC, hBitMap)


    'Clear the background

    newBrush = CreateSolidBrush(RGB(255, 255, 255))

    oldBrush = SelectObject(hMemDC, newBrush)

    Rectangle hMemDC, 0, 0, 500, 500

    SelectObject hMemDC, oldBrush

    DeleteObject newBrush


    'Select the Pen


    newPen = CreatePen(0, 3, vbRed)
    oldPen = SelectObject(hMemDC, newPen)

    MoveToEx hMemDC, 10, 10, pt

    LineTo hMemDC, 100, 100

    'Now copy the memory DC to the real DC

    BitBlt Picture1.hdc, 0, 0, 500, 500, hMemDC, 0, 0, vbSrcCopy

    SelectObject hMemDC, oldPen
    DeleteObject newPen

    End If
    End If

    End Sub



  4. #4
    Join Date
    Oct 2001
    Posts
    3

    Re: Colors in Memory DC

    Oops - you need to set autoredraw on the control too...


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured