|
-
Aug 16th, 2001, 06:46 PM
#1
Thread Starter
Hyperactive Member
bitmap display
Does anyone have a piece of code that shows how to display a resource bitmap on your window? Thanks
Matt 
-
Aug 16th, 2001, 07:13 PM
#2
PowerPoster
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;
}
-
Aug 17th, 2001, 02:02 PM
#3
Thread Starter
Hyperactive Member
if you don't mind could you send me your project? Thanks alot
my email adress is [email protected]
Matt 
-
Aug 17th, 2001, 03:56 PM
#4
PowerPoster
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
-
Aug 18th, 2001, 02:25 PM
#5
Thread Starter
Hyperactive Member
Matt 
-
Aug 18th, 2001, 02:48 PM
#6
Thread Starter
Hyperactive Member
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?
Matt 
-
Aug 18th, 2001, 04:28 PM
#7
What is the resource identifier of MYBMP.bmp? Is it IDB_BITMAP1 (or similar) or MYBMP?
-
Aug 18th, 2001, 06:26 PM
#8
Thread Starter
Hyperactive Member
where would I find that info? I don't have a header file created. Do I need one?
Matt 
-
Aug 18th, 2001, 06:29 PM
#9
Thread Starter
Hyperactive Member
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
Matt 
-
Aug 18th, 2001, 06:51 PM
#10
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).
-
Aug 18th, 2001, 07:17 PM
#11
PowerPoster
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
-
Aug 18th, 2001, 08:35 PM
#12
Thread Starter
Hyperactive Member
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;
}
Matt 
-
Aug 18th, 2001, 10:04 PM
#13
PowerPoster
In that I changed the Identifier to IDB_MYBMP
Change the indentifier to MYBMP not IDB_MYBMP
-
Aug 20th, 2001, 10:22 AM
#14
Thread Starter
Hyperactive Member
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?
Matt 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|