Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with GDI issues in VB6 2

Status
Not open for further replies.

SemperFiDownUnda

Instructor
Aug 6, 2002
1,561
AU
Ok here is the deal. I'm using some owner draw controls from vbaccelerator

I'm doing all the drawing for my listboxes, grids, comboboxes. I've got it doing everything I want but at this point in time I'm loading monochrome bitmaps from disk to use as patterned brushes then using PatBlt to fill an area with the bitmaps pattern.

I would like to hold these bitmaps in the application some way but everything I've tried converts the bitmap to a higher colordepth thus making the PatBlt use not only the pattern but the colors in the bitmap (black and white) instead of the current background and foreground colors.

Here is the code I'm using
Code:
      lCol = HSLtoRGB(iHue, _
                       240, _
                       60)
      SetBkColor lhDC, lCol
      lCol = HSLtoRGB(iHue, _
                       240, _
                       120)
      SetTextColor lhDC, lCol

      hBm = LoadImage(0, "C:\Program Files\Microsoft Visual Studio\VB98\arstest\images\DiagPattern.bmp", IMAGE_BITMAP, 0, 0, LR_MONOCHROME + LR_LOADFROMFILE) 'CreateBitmapIndirect(tBM)
      'hBm = picPattern.Picture.Handle
      
      hBr = CreatePatternBrush(hBm)
      hBrHold = SelectObject(lhDC, hBr)
      PatBlt lhDC, tR.Left, tR.Top, (tR.Right - tR.Left), (tR.Bottom - tR.Top), PATCOPY
      SelectObject lhDC, hBrHold
      DeleteObject hBr
      'DeleteObject hBm
      SetTextColor lhDC, 0

I want to get rid of the LoadImage for something that can grab the bitmap from something within the app.

I'm banging my head agianst the wall on this one.

Hope I've been helpful,
Wayne Francis

If you want to get the best response to a question, please check out FAQ222-2244 first
 
You need to embed your bitmaps in a resource file and load your bitmaps from resource at runtime. But this is not as simple as stated. You have to be careful.

The problem is that when VB loads a bitmap as a picture object, it increases its color depth as you stated in your post causing problems thereafter. This applies to LoadResPicture function as well which load pictures from resource.

To resolve this issue you have to avoid using VB picture functions and use bitmap functions (like the LoadImage) from Win32 GDI API.

The LoadBitmap function is the GDI counterpart of the LoadResPicutre function. Both load bitmap from resource, but unlike the LoadResPicture function, LoadBitmap retains the original format of the bitmap, thus the brush created with the bitmap of LoadBitmap function works correctly with the PatBlt API.

Here, one important and interesting thing to be mentioned. When you load a bitmap using LoadResPicture function during runtime from VBIDE, the function loads the bitmap from the .res file attached to the project. After the project is compiled and ran independently, the LoadResPicture function loads bitmap from the executable's resource.

On the other hand, the LoadBitmap function always loads the bitmaps from the application's executable resource. When you run your program from VBIDE, this resource is the resource data of VB6.EXE and when you run your program independently after compilation, this resource is the resource data of the applications executable.

Due to this behaviour, you will not get correct bitmaps loaded when you run your program from VBIDE, because, at that time your resource file is bypassed and the LoadBitmap function will try to retrieve data from the current application's resource, i.e. the resource data of VB6.EXE. The LoadBitmap function will not even bother that you have attached a resource file to your project.

You can test your program only after compilation as when the application will run independenlty, the LoadBitmap function will load the bitmap from the resource of the current executable. At that moment, it would have no connection to VB6.EXE and will load the correct bitmaps.

Here is a sample program that I tested using resources and it worked perfectly as required.

Follow these steps.

1. Create an 8x8 bitmap in Paint with some random black and white patterns and save it as monochrome bitmap format (not in the default 24-bit format).

2. Start a new VB project, add a resource file to the project using the Resource Editor Add-In.

3. Add the bitmap file to your resource file using the "Add Bitmap..." function, the bitmap will be added to the resource file with an Id of 101. Save the resource file.

4. Now insert the following code in the form's code window.
___
[tt]
Option Explicit
Private Declare Function LoadImage Lib "user32.dll" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long
Private Declare Function LoadBitmap Lib "user32" Alias "LoadBitmapA" (ByVal hInstance As Long, lpBitmapName As Any) As Long
Private Declare Function CreatePatternBrush Lib "gdi32.dll" (ByVal hBitmap As Long) As Long
Private Declare Function SelectObject Lib "gdi32.dll" (ByVal hDC As Long, ByVal hObject As Long) As Long
Private Declare Function PatBlt Lib "gdi32.dll" (ByVal hDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal dwRop As Long) As Long
Private Declare Function DeleteObject Lib "gdi32.dll" (ByVal hObject As Long) As Long
Const PATCOPY As Long = &HF00021

Private Sub Form_Load()
AutoRedraw = True
ScaleMode = vbPixels

Dim hBm As Long, hBr As Long, hBrHold As Long
hBm = LoadBitmap(App.hInstance, ByVal 101&)
hBr = CreatePatternBrush(hBm)

ForeColor = vbBlue 'assign whatever
BackColor = vbGreen 'colors you like

hBrHold = SelectObject(hDC, hBr)

PatBlt hDC, 0, 0, ScaleWidth, ScaleHeight, PATCOPY

SelectObject hDC, hBrHold
DeleteObject hBr
DeleteObject hBm
End Sub[/tt]
___

5. Compile your program to generate exe file (this is necessary) and run your executable file.

You will see your form stroked with brush in the selected back color and fore color.

If you bypass step 5 and run your program from VBIDE you would get incorrect pattern because the LoadBitmap function would be loading incorrect bitmap from VB6.EXE resource.

You can extend and use this code in your program and I hope it will work without any problem. The ForeColor and BackColor assingments will be replaced by SetTextColor and SetBkColor fucctions respectively.

Hope that makes some sense...
 
Makes perfect sense thanks.
I've put in some compile options that will load from either disk (for when I'm debugging) or the resource when I'm running from a true compile.

Thanks for saving me from banging my head up agianst the wall any longer :)


Hope I've been helpful,
Wayne Francis

If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top