memory dc, filling with a brush

sesri
03-21-2006, 03:03 AM
Dear All,

I am trying to create a memorydc with compatiblebitmap, filling a small region with red solid brush, and finally blitting it to form1.hdc. All i get is a small square with grey hatch instead of a red square. (the same works when i use form1.hdc inplace of nDc.) Can some one help me identify/correct the mistake? Thanking you.

*************
Dim nBrush As Long, oBrush As Long
Dim nBitMap As Long, oBitMap As Long
Dim nRegion As Long, nReturn As Long
Dim objBrush As LOGBRUSH
Dim nDC As Long

objBrush.lbColor = vbRed: objBrush.lbStyle = 0: objBrush.lbHatch = 0

nDC = CreateCompatibleDC(Form1.hdc)
nBitMap = CreateCompatibleBitmap(nDC, 300, 300)
nBrush = CreateBrushIndirect(objBrush)
nRegion = CreateRectRgn(0, 0, 40, 40)

oBitMap = SelectObject(nDC, nBitMap)
oBrush = SelectObject(nDC, nBrush)
nReturn = SelectObject(nDC, nRegion)

nReturn = PaintRgn(nDC, nRegion)
nReturn = BitBlt(Form1.hdc, 0, 0, 40, 40, nDC, 0, 0, vbSrcCopy)
Form1.Refresh

nReturn = DeleteObject(SelectObject(nDC, oBitMap))
nReturn = DeleteObject(SelectObject(nDC, oBrush))
nReturn = DeleteObject(nRegion)
nReturn = DeleteDC(nDC)
*************

DougT
03-21-2006, 04:27 AM
Hi,

Note: When a memory device context is created, it initially has a 1-by-1 monochrome bitmap selected into it. If this memory device context is used in CreateCompatibleBitmap, the bitmap that is created is a monochrome bitmap. To create a color bitmap, use the hDC that was used to create the memory device context, as shown in the following code:

HDC memDC = CreateCompatibleDC ( hDC );
HBITMAP memBM = CreateCompatibleBitmap ( hDC, nWidth, nHeight );
SelectObject ( memDC, memBM );


I think translates into:

nDC = CreateCompatibleDC(0)
nBitMap = CreateCompatibleBitmap(Form1.hdc, 300, 300)

Well, it draws a red square for me ! :p

Regards
Doug

MilanJ
03-21-2006, 05:19 AM
nDC = CreateCompatibleDC(Form1.hdc)
nBitMap = CreateCompatibleBitmap(nDC, 300, 300)


Here's one problem, it should be:

nDC = CreateCompatibleDC(Form1.hdc)
nBitMap = CreateCompatibleBitmap(Form1.hdc, 300, 300)

Use the same DC as the previous call in CreateCompatibleBitmap function. You used Form.hdc in first call and nDC in the second.
MilanJ

DougT
03-21-2006, 05:34 AM
Hi,

Good point. It seems to work though with CreateCompatibleDC(0) as well as CreateCompatibleDC(Form1.hdc). According to the documentation, an argument of 0 will create a DC compatible with the Screen. I don't know enough about GDI etc to comment on one method over the other but I think you're right. If you stick to what the documentation says it's probably going to work consistantly.

Regards
Doug

MilanJ
03-21-2006, 09:19 PM
Hi,

Good point. It seems to work though with CreateCompatibleDC(0) as well as CreateCompatibleDC(Form1.hdc). According to the documentation, an argument of 0 will create a DC compatible with the Screen. I don't know enough about GDI etc to comment on one method over the other but I think you're right. If you stick to what the documentation says it's probably going to work consistantly.

Regards
Doug
Hi Doug,
the problem, that Sesri encountered, is caused - as I wrote - by the fact, that he had created DC compatible with Form1 DC and then he called CreateCompatibleBitmap with the handle of the newly created DC, but haven't selected any bitmap to the DC (with SelectObject) yet. But when a memory device context is created, it initially has a 1-by-1 monochrome bitmap selected into it. If this memory device context is used in CreateCompatibleBitmap, the bitmap that is created is a monochrome bitmap. So, in Sesri's case - he created monochrome bitmap and that's why he obtained grey square instead of the red one. He needs to use again Form1 DC in bitmap creation to create color bitmap. That was cause of the problem.

Regards
Milan

sesri
03-21-2006, 09:44 PM
Hi,

Thank you Milan/Doug. I thought when it said createcompatibledc, it is going to be compatible with form1.hdc ( using same color scheme, brush/pen etc.) and was trying to continue with the newly created dc. Thank you for the clarification.

with regards :)
sesri

sesri
03-24-2006, 01:52 AM
Hi Doug,
the problem, that Sesri encountered, is caused - as I wrote - by the fact, that he had created DC compatible with Form1 DC and then he called CreateCompatibleBitmap with the handle of the newly created DC, but haven't selected any bitmap to the DC (with SelectObject) yet. But when a memory device context is created, it initially has a 1-by-1 monochrome bitmap selected into it. If this memory device context is used in CreateCompatibleBitmap, the bitmap that is created is a monochrome bitmap. So, in Sesri's case - he created monochrome bitmap and that's why he obtained grey square instead of the red one. He needs to use again Form1 DC in bitmap creation to create color bitmap. That was cause of the problem.

Regards
Milan

Hi Milan,

can you tell me why this is not working. i am unable to grasp/get it work.


Dim nReturn As Long, nDC As Long
Dim nBrush As Long, oBrush As Long
Dim objBrush As LOGBRUSH

Toolbar1.Buttons(1).Image = 1

objBrush.lbColor = vbRed: objBrush.lbHatch = 0: objBrush.lbStyle = 0
nBrush = CreateBrushIndirect(objBrush)
nRegion = CreateRectRgn(Toolbar1.Buttons(1).Left / Screen.TwipsPerPixelX, Toolbar1.Buttons(1).Top / Screen.TwipsPerPixelY, (Toolbar1.Buttons(1).Left + Toolbar1.Buttons(1).Width) / Screen.TwipsPerPixelX, (Toolbar1.Buttons(1).Top + Toolbar1.Buttons(1).Height) / Screen.TwipsPerPixelY)

nDC = GetWindowDC(Toolbar1.hwnd)
oBrush = SelectObject(nDC, nBrush)
nReturn=SelectObject(nDc, nRegion)
nReturn = PaintRgn(nDC, nRegion)

nReturn = DeleteObject(SelectObject(nDC, oBrush))
nReturn = DeleteObject(nRegion)


Thanks in advance.

MilanJ
03-24-2006, 02:09 AM
Hi,
could you post the code as a small example project in a zip attachment?
I'd try to debug it, but without your form it is more difficult for me to deal with it.
Regards.
Milan

sesri
03-24-2006, 03:00 AM
Hi,
could you post the code as a small example project in a zip attachment?
I'd try to debug it, but without your form it is more difficult for me to deal with it.
Regards.
Milan

Hi,

Here it is. My idea basically is to draw on to the icon in toolbar button with lines/filled boxes. I guess, the problem with this code is *** it is not drawing on the button face *** but on the form, and when you press the button, the red square gets erased.

MilanJ
03-24-2006, 03:43 AM
Hi,

Here it is. My idea basically is to draw on to the icon in toolbar button with lines/filled boxes. I guess, the problem with this code is *** it is not drawing on the button face *** but on the form, and when you press the button, the red square gets erased.
Hmm, the problem is, that your drawing is not permanent - it is drawn on the Toolbar1, but when you, for example, minimize your form and then show it again, the drawing vanishes. I don't know, if and how to make your drawing on Toolbar permanent.
I think, that there should be a better way to solve your problem - I saw, you asked about it in another thread - but I really don't know the solution now.
Very sorry about it.
Milan

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum