Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I'd like to add a background to the main window of my win32 app.
I'm using resedit to manage resources, and I've already added a bitmap and set it to the main window.
Now I'm having issues to display it, here is the bitmap in my resource file (generated by resedit):
C++
CONTROL         IDB_BG, 0, WC_STATIC, SS_BITMAP, 0, 0, 517, 252, WS_EX_LEFT


Here is how I'm trying to display the bitmap:
C++
case WM_INITDIALOG:
     bg = (HBITMAP) LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BG));
 break;
 case WM_PAINT:
     PAINTSTRUCT     ps;
     HDC             hdc;
     BITMAP          bitmap;
     HDC             hdcMem;
     HGDIOBJ         oldBitmap;

     hdc = BeginPaint(hwndDlg, &ps);

     hdcMem = CreateCompatibleDC(hdc);
     oldBitmap = SelectObject(hdcMem, bg);

     GetObject(bg, sizeof(bitmap), &bitmap);
     BitBlt(hdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hdcMem, 0, 0, SRCCOPY);

     SelectObject(hdcMem, oldBitmap);
     DeleteDC(hdcMem);

     EndPaint(hwndDlg, &ps);
 break;


The program compiles but the bitmap isn't displayed.

Thanks for the help!
Posted
Comments
CPallini 12-Aug-14 17:18pm    
Why don't you check the return value of Windows API functions?
enhzflep 12-Aug-14 18:53pm    
I'm assuming(hoping) you've made bg static, right?
Luca D'Amico 13-Aug-14 7:00am    
Well no, it isn't declared as static. Why should I made it static ?
enhzflep 13-Aug-14 19:53pm    
Because you're initializing it in WM_INITDIALOG and then using it in WM_PAINT. If the variable is declared inside the WindowProcedure function, then it (the bg variable) is created and destroyed each time WindowProcedure is called. That means that the procedure is called for WM_INIT, the variable is created and then it's set to the HBITMAP that holds the image loaded from disk. It then finishes and the 'bg' variable is destroyed and this HBITMAP value is lost. Some time later, the function is called again, the 'bg' variable is created and holds garbage, before you then try to use this to obtain an image size and finally, to draw it.
Using the keyword static means that the variable isn't destroyed each time and the same value obtained when loading the image will be available when the time come to draw it.

You could instead move the line that loads the bitmap into the WM_PAINT handler and place a corresponding 'DeleteObject(bg);' line after you've used it in the BitBlt.
Though as KarstenK says, you're likely better-off doing this in the WM_ERASEBKG handler.
Either would draw the bitmap as the dialog's background, starting from the top-left.


Yet another approach would be to load the image in the WM_INITDIALOG handler as you've done, followed by setting the image as the HBITMAP to be used for the IDB_BKG picture-control. The control would then draw the image for you.

You could do that, with:
bg = (HBITMAP) LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_OF_BMP_RESOURCE));
SendDlgItemMessage(hwndDlg, IDB_BG, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)bg);


Notice the text ID_OF_BMP_RESOURCE. This should point to the image itself. In your code, you've set this to the resource-id of the bitmap _control_. This should actually be the resource-id of a bitmap _image_.

If you wanted to use a tiled background, you could always investigate the use of CreatePatternBrush, which can create a brush from an image, freeing you up from the calculations involved in redrawing and repositioning a single small image over a large area - making a patternBrush means you can simply fill the area with FillRect and the tiling is taken care of for you.

1 solution

Drawing the background is better done in the WM_ERASEBKGND handler.

Check everything else: the return codes. Is hwndDlg the right window for the drawing. Or you draw the bitmap on a invisible position (0,0).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900