Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > API > memory dc, filling with a brush


Reply
 
Thread Tools Display Modes
  #1  
Old 03-21-2006, 03:03 AM
sesri sesri is offline
Freshman
 
Join Date: Nov 2005
Posts: 32
Post memory dc, filling with a brush


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)
*************
Reply With Quote
  #2  
Old 03-21-2006, 04:27 AM
DougT's Avatar
DougT DougT is offline
Ultimate Antique

Administrator
* Expert *
 
Join Date: Sep 2005
Location: Maldon,Essex, UK
Posts: 3,939
Default

Hi,
Quote:
Originally Posted by Microsoft
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:
Code:
nDC = CreateCompatibleDC(0) nBitMap = CreateCompatibleBitmap(Form1.hdc, 300, 300)
Well, it draws a red square for me !

Regards
Doug
__________________
semel insanivimus omnes
S Data in context = Information, S Information in context = Knowledge, S Knowledge in context = Experience
S Experience in context = Wisdom= Data
Reply With Quote
  #3  
Old 03-21-2006, 05:19 AM
MilanJ MilanJ is offline
Junior Contributor
 
Join Date: Sep 2005
Posts: 370
Default

Quote:
Originally Posted by sesri
nDC = CreateCompatibleDC(Form1.hdc)
nBitMap = CreateCompatibleBitmap(nDC, 300, 300)
Here's one problem, it should be:
Code:
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
Reply With Quote
  #4  
Old 03-21-2006, 05:34 AM
DougT's Avatar
DougT DougT is offline
Ultimate Antique

Administrator
* Expert *
 
Join Date: Sep 2005
Location: Maldon,Essex, UK
Posts: 3,939
Default

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
__________________
semel insanivimus omnes
S Data in context = Information, S Information in context = Knowledge, S Knowledge in context = Experience
S Experience in context = Wisdom= Data
Reply With Quote
  #5  
Old 03-21-2006, 09:19 PM
MilanJ MilanJ is offline
Junior Contributor
 
Join Date: Sep 2005
Posts: 370
Default

Quote:
Originally Posted by DougT
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
Reply With Quote
  #6  
Old 03-21-2006, 09:44 PM
sesri sesri is offline
Freshman
 
Join Date: Nov 2005
Posts: 32
Default

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
Reply With Quote
  #7  
Old 03-24-2006, 01:52 AM
sesri sesri is offline
Freshman
 
Join Date: Nov 2005
Posts: 32
Default

Quote:
Originally Posted by MilanJ
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.

Code:
    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.
Reply With Quote
  #8  
Old 03-24-2006, 02:09 AM
MilanJ MilanJ is offline
Junior Contributor
 
Join Date: Sep 2005
Posts: 370
Default

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
Reply With Quote
  #9  
Old 03-24-2006, 03:00 AM
sesri sesri is offline
Freshman
 
Join Date: Nov 2005
Posts: 32
Default

Quote:
Originally Posted by MilanJ
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.
Attached Files
File Type: zip P7.zip (2.2 KB, 4 views)
Reply With Quote
  #10  
Old 03-24-2006, 03:43 AM
MilanJ MilanJ is offline
Junior Contributor
 
Join Date: Sep 2005
Posts: 370
Default

Quote:
Originally Posted by sesri
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
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->