md4dog
md4dog

Reputation: 3

How to display bitmap image in background Win32

I am having a hard time finding out how to load my .bmp image into the background.

Here is my code:

wc.cbSize        = sizeof(WNDCLASSEX);
wc.style         = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc   = WndProc; 
wc.cbClsExtra    = 0;
wc.cbWndExtra    = 0;
wc.hInstance     = hInstance;
wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION); 
wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = CreatePatternBrush(LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP1)));
wc.lpszMenuName  = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

Though the problem is not entirely code related. I simply have a file x.bmp, and I have the IDB_BITMAP1 defined in my resource.rc file and included with an identifier in resource.h -- but when I run it I just get a white screen. The question here is, how can I upload my .bmp image to the .rc file? I am using MSVS20124. When I right click the .rc file add>bitmap>import...>x.bmp I get an error "could not load file". Why is this? I just want to set that x.bmp as the background in my window.

thanks

Upvotes: 0

Views: 2046

Answers (1)

David Heffernan
David Heffernan

Reputation: 613461

You are not performing any error checking in the call to LoadBitmap and the call to CreatePatternBrush. Very likely one of these calls fails and so hbrBackground is set to NULL, which results in a white background.

Your next step is to do some debugging to work out which call fails. Quite likely is that the bitmap file that you are linking as a resource uses a format that is not supported by LoadBitmap. Or perhaps the bitmap file is somehow not being linked. But so long as you actually manage to create a bitmap and then a brush, the system will use that brush to paint the background.

Upvotes: 1

Related Questions