I am trying to use the createPen/SelectObject GDI functions to change the DC pen in a VB6 simple program.
The program is a vb6 form with Picture1 pictureBox control. It waits for the user to click inside the pictureBox to draw a black background with 2 lines (one horizontal and one vertical, intersecting at the mouse X,Y location).
The program draws the 2 lines in white color when I use the GetStockObject(WHITE_PEN) function as in the following code:
SelectObject lMemoryDC, GetStockObject(WHITE_PEN)
but when I use the following code:
lPen = CreatePen(CLng(0), CLng(0), RGB(0, 250, 0))
lOldPen = SelectObject(lMemoryDC, lPen)
it does not draw any of the 2 lines, at least I don't see them in the final result, I only see the black bakground color.
Here is my code:
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim lMemoryDC As Long, lMemBitMap As Long, lOrigBitmap As Long
Dim lPointAPI As POINTAPI
Dim lPen As Long, lOldPen As Long
Dim wid As Long
Dim hgt As Long
Picture1.AutoRedraw = True
wid = Picture1.ScaleWidth
hgt = Picture1.ScaleHeight
'Picture1.ForeColor = vbRed
lMemoryDC = CreateCompatibleDC(Picture1.hdc) '(GetDC(0))
lMemBitMap = CreateCompatibleBitmap(lMemoryDC, Picture1.ScaleWidth, Picture1.ScaleHeight)
lOrigBitmap = SelectObject(lMemoryDC, lMemBitMap)
'Here I am creating a green color pen
* lPen = CreatePen(CLng(0), CLng(0), RGB(0, 250, 0))
lOldPen = SelectObject(lMemoryDC, lPen)*
Call MoveToEx(lMemoryDC, X - 100, Y, lPointAPI)
Call LineTo(lMemoryDC, X + 100, Y)
Call MoveToEx(lMemoryDC, X, Y - 100, lPointAPI)
Call LineTo(lMemoryDC, X, Y + 100)
SelectObject lMemoryDC, lOldPen
DeleteObject lPen
BitBlt Picture1.hdc, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, lMemoryDC, 0, 0, vbSrcCopy
Picture1.Refresh
'Cleanup memory
DeleteObject lMemBitMap
DeleteDC lMemoryDC
End Sub
Thank you
you are in monocrome using lMemBitMap = CreateCompatibleBitmap(lMemoryDC, Picture1.ScaleWidth, Picture1.ScaleHeight)
see https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-createcompatiblebitmap
About managing colors this is what I found:
Case 12 is what you need
Mario.