March 19th, 2007, 01:17 PM
Background Image on WINAPI Window
I was wondering in advance before I start coding anything which may prove to be a waste a time, if it is possible to change the background of a WINAPI window to an image. I know it is possible to change the colour but I am not too sure about images. Thanks.
March 19th, 2007, 03:30 PM
Not sure, but I think you have to process WM_PAINT and draw it yourself.
March 19th, 2007, 03:53 PM
Thanks, I've done some research and that seems like the way to go.
March 19th, 2007, 09:27 PM
I've been battling for the last few hours to get an image onto a window that I have created, but to no avail. The properties that I set for the window e.g. size, background colour all work. I've tried a few different examples but none of them work, the one below is based on the skeleton code provided by Dev C++ for a Windows application, with "case WM_PAINT" been taking from somewhere else:
Also I'm also sure that my .rc file has been defined correctly, so does anyone have an idea as to what might be causing this?
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
char szClassName[ ] = "Television";
int WINAPI WinMain (HINSTANCE hThisInstance,
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
if (!RegisterClassEx (&wincl))
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
/* Translate virtual-key messages into character messages */
/* Send message to WindowProcedure */
/* The program return-value is 0 - The value that PostQuitMessage() gave */
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
HDC hDC, MemDCExercising;
switch (message) /* handle the messages */
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
hDC = BeginPaint(hwnd, &Ps);
// Load the bitmap from the resource
bmpExercising = LoadBitmap(hInst,"EXERCISEBMP");
// Create a memory device compatible with the above DC variable
MemDCExercising = CreateCompatibleDC(hDC);
// Select the new bitmap
GetObject(bmpExercising, sizeof(bm), &bm);
// Copy the bits from the memory DC into the current dc
BitBlt(hDC, 10, 10,bm.bmWidth, bm.bmHeight, MemDCExercising, 0, 0, SRCCOPY);
// Restore the old bitmap
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
March 19th, 2007, 10:39 PM
Are you sure you're getting the WM_PAINT message? Have tried putting a break point in the WM_PAINT handling code to see if it actually gets called?
I think you might need the following just after creating the Window:
I got the above code from the MSDN examples.
// Show the window using the flag specified by the program
// that started the application, and send the application
// a WM_PAINT message.
I also think you should be calling GetUpdateRect() to determine whether you need to call BeginPaint().
You might get better, faster assistance if this were posted in the Windows forum rather than the C/C++/C# forum. The issue appears to be Windows specific and not a language problem.
I no longer wish to be associated with this site.
March 20th, 2007, 06:11 AM
Thanks for the response but that didn't seem to work. In terms of posting on a the Windows Forum, there doesn't seem to be a section for GDI development.
March 20th, 2007, 06:36 AM
I no longer wish to be associated with this site.
March 20th, 2007, 08:05 AM
I've managed to track the problem down to the image not being loaded. I thought it was simply enough to define it within a .rc file and then reference it, but it looks like I have to take care of things in a header file too. Visual C++ takes care of this but because I'm using Dev-C++ I have no clue how to do this.