Does anyone have a piece of code that shows how to display a resource bitmap on your window? Thanks
Printable View
Does anyone have a piece of code that shows how to display a resource bitmap on your window? Thanks
Here is a little code that I wrote some weeks ago:
-The bitmap in the resource file is named "MYBMP"
-The resource header file is called "resource.h"
Code:#include <windows.h>
#include "resource.h"
static char g_szClassName[] = "MyWindowClass";
static HINSTANCE hinst = NULL;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HWND hwnd;
PAINTSTRUCT ps;
HDC hdc, memhdc;
HBITMAP mybmp;
BITMAP bm;
//MYBMP is a bitmap in the resource file
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX WndClass;
MSG Msg;
hinst = hInstance;
mybmp = LoadBitmap(hinst, MAKEINTRESOURCE(MYBMP));
WndClass.cbSize = sizeof(WNDCLASSEX);
WndClass.style = CS_HREDRAW | CS_VREDRAW;
WndClass.lpfnWndProc = WndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = hinst;
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndClass.lpszMenuName = NULL;
WndClass.lpszClassName = g_szClassName;
WndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&WndClass))
{
MessageBox(0, "Window Registration Failed! ", "Error!",
MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
return 0;
}
hwnd = CreateWindowEx(
NULL,
g_szClassName,
"Coloured Shapes Generator",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 320, 240,
NULL, NULL, hinst, NULL);
if(hwnd == NULL)
{
MessageBox(0, "Window Creation Failed! ", "Error!",
MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wparam, LPARAM lparam)
{
switch(Message)
{
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
memhdc = CreateCompatibleDC(hdc);
SelectObject(memhdc, mybmp);
//Draws the bitmap on the memory DC
GetObject(mybmp, sizeof(bm), &bm);
//Gets the info about the bitmap and stores that in "bm"
BitBlt(hdc, 0,0, bm.bmWidth, bm.bmHeight, memhdc, 0,0, SRCCOPY);
//Draws the bitmap on the window from the memory DC
DeleteDC(memhdc);
EndPaint(hwnd, &ps);
return 0;
case WM_CLOSE:
DeleteObject(mybmp);
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, Message, wparam, lparam);
}
return 0;
}
if you don't mind could you send me your project? Thanks alot
my email adress is [email protected]
i cannot find the project because it is on one of the cds that I created after reinstalling the os.
YOu can get project from the link below (it is from winprog.org examples):
http://www.winprog.org/tutorial-old/files/image_one.zip
just as good! Thanks
one more thing, when I use your example code I have MYBMP.bmp under the resource folder in my project, which contains the drawn picture you can create. When I compile I get
the error:
error C2065: 'MYBMP' : undeclared identifier
what am I doing wrong?
What is the resource identifier of MYBMP.bmp? Is it IDB_BITMAP1 (or similar) or MYBMP?
where would I find that info? I don't have a header file created. Do I need one?
actually for some reason its not in the project explorer but I found a resource.h file that goes with my project. It is
#define IDB_BITMAP1
Well, look in the resource tab thingy in VC++ and look at the bitmap you added.... it should have an identifier(such as IDB_BITMAP1) change that, and change your resource.h (I think VC++ does this automatically though).
MYBMP.bmp is the name of the bitmap that you have on your computer (the actuall file) but as dennis said, you need you change the id of the bitmap in the resource file to MYBMP, not the bitmap file name;)
ok, under Resource Files I have the bitmap "MYBMP.bmp". In that I changed the Identifier to IDB_MYBMP, then the actual bitmap is called bitmap1.bmp. I set the file name to that. I still get the error
'MYBMP' : undeclared identifier
anyway heres the code I am using, this does not include the callback although I have it.
Code:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HWND hwnd;
PAINTSTRUCT ps;
HDC hdc, memhdc;
HBITMAP mybmp;
BITMAP bm;
//MYBMP is a bitmap in the resource file
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX WndClass;
MSG Msg;
hinst = hInstance;
mybmp = LoadBitmap(hinst, MAKEINTRESOURCE(MYBMP)); //error here-
WndClass.cbSize = sizeof(WNDCLASSEX);
WndClass.style = CS_HREDRAW | CS_VREDRAW;
WndClass.lpfnWndProc = WndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = hinst;
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndClass.lpszMenuName = NULL;
WndClass.lpszClassName = g_szClassName;
WndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&WndClass))
{
MessageBox(0, "Window Registration Failed! ", "Error!",
MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
return 0;
}
hwnd = CreateWindowEx(
NULL,
g_szClassName,
"Coloured Shapes Generator",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 320, 240,
NULL, NULL, hinst, NULL);
if(hwnd == NULL)
{
MessageBox(0, "Window Creation Failed! ", "Error!",
MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
Change the indentifier to MYBMP not IDB_MYBMPQuote:
In that I changed the Identifier to IDB_MYBMP
Well, now Im getting no errors but nothing is being displayed. Same code as above, changed the identifier to MYBMP. Any idea what could be wrong?