|
-
Jul 4th, 2001, 01:00 AM
#1
Thread Starter
PowerPoster
this code does not show a bitmap!
Please look at the following source code. I have a bimap in my resource file and I want to first load that bitmap into the memory DC and then I want to copy it on to the screen
But the following code just shows me an error message:
The message is my own. I call it whenver i cannot load the image from the resource file:
Code:
#include <windows.h>
#include "resource.h"
#define TheClass "myclass"
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch(msg)
{
case WM_CREATE:
HBITMAP hbball;
hbball = LoadBitmap(NULL, "BALLBMP");
if(!hbball)
{
MessageBox(hwnd, "Load of resource BALLBMP failed.", "Error",
MB_OK | MB_ICONEXCLAMATION);
}
return 0;
case WM_PAINT:
if(hbball)
{
HDC winhdc, memhdc;
PAINTSTRUCT ps;
BITMAP bm;
winhdc = BeginPaint(hwnd, &ps);
memhdc = CreateCompatibleDC(winhdc);
SelectObject(memhdc, hbball);
GetObject(hbball, sizeof(bm), &bm);
BitBlt(winhdc,0,0,bm.bmWidth, bm.bmHeight, memhdc, 0,0,SRCCOPY);
DeleteDC(memhdc);
EndPaint(hwnd, &ps);
}
return 0;
case WM_CLOSE:
DestroyWindow(hwnd);
return 0;
case WM_DESTROY:
DeleteObject(hbball);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wparam, lparam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE HPrevInstance, LPSTR lpCmdLine, int ShowCmd)
{
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
//Creates the window class
wc.cbClsExtra = 0;
wc.cbSize = sizeof(wc);
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = TheClass;
wc.lpszMenuName = MAKEINTRESOURCE(MYMENU);
wc.style = CS_VREDRAW | CS_HREDRAW;
//Registers the Window Class -- if it does not succeed then it
//shows an error message.
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Error occurred while \
Registering the Main Class", "Error!", MB_OK);
return 0;
}
//Creates the main window hwnd
hwnd = CreateWindow(TheClass,
"The Ball Example",
WS_OVERLAPPEDWINDOW,
0,
0,
500,
500,
NULL,
NULL,
hInstance,
NULL);
//Check if there is any error when we make our windows' Hwnd
if(hwnd == NULL)
{
MessageBox(NULL, "Error making the Hwnd for our window","Error",MB_OK);
return 0;
}
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
Do you know what is rong with that?
-
Jul 4th, 2001, 01:29 AM
#2
Try this if you are using Visual C++:
Code:
LoadBitmap(NULL, MAKEINTRESOURCE(BALLBMP));
-
Jul 4th, 2001, 01:51 AM
#3
Thread Starter
PowerPoster
i HAVE DONE THAT
I have tried that but it still does not load the bitmap
-
Jul 4th, 2001, 01:58 AM
#4
Try this then:
Code:
//global(at the top)
HINSTANCE hInst;
//somewhere in winmain
hInst = hInstance;
//WM_PAINT
LoadBitmap(hInstance, MAKEINTRESOURCE(BALLBMP));
-
Jul 4th, 2001, 05:18 AM
#5
Monday Morning Lunatic
Code:
LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(BALLBMP));
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jul 5th, 2001, 12:04 AM
#6
Thread Starter
PowerPoster
Now it does not show anything
Now it does does not show the error message but now it does show anything on my winodw.
-
Jul 5th, 2001, 05:09 AM
#7
Monday Morning Lunatic
Code:
SelectObject(memhdc, hbball);
Change that memhdc to winhdc and see what happens.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jul 5th, 2001, 10:28 AM
#8
Frenzied Member
Here:
PHP Code:
#include <windows.h>
#include "resource.h"
#define TheClass "myclass"
HBITMAP hbball; //<-----HERE
HINSTANCE hInst; //<-----HERE
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch(msg)
{
case WM_CREATE:
hbball = LoadBitmap(hInst, MAKEINTRESOURCE(BALLBMP)); //<-----HERE
if(!hbball)
{
MessageBox(hwnd, "Load of resource BALLBMP failed.", "Error",
MB_OK | MB_ICONEXCLAMATION);
}
return 0;
case WM_PAINT:
if(hbball)
{
HDC winhdc, memhdc;
PAINTSTRUCT ps;
BITMAP bm;
winhdc = BeginPaint(hwnd, &ps);
memhdc = CreateCompatibleDC(winhdc);
SelectObject(memhdc, hbball);
GetObject(hbball, sizeof(bm), &bm);
BitBlt(winhdc,0,0,bm.bmWidth, bm.bmHeight, memhdc, 0,0,SRCCOPY);
DeleteDC(memhdc);
EndPaint(hwnd, &ps);
}
return 0;
case WM_CLOSE:
DestroyWindow(hwnd);
return 0;
case WM_DESTROY:
DeleteObject(hbball);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wparam, lparam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE HPrevInstance, LPSTR lpCmdLine, int ShowCmd)
{
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
//Creates the window class
wc.cbClsExtra = 0;
wc.cbSize = sizeof(wc);
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = TheClass;
wc.lpszMenuName = MAKEINTRESOURCE(MYMENU);
wc.style = CS_VREDRAW | CS_HREDRAW;
hInst = hInstance; //<-----HERE
//Registers the Window Class -- if it does not succeed then it
//shows an error message.
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Error occurred while \
Registering the Main Class", "Error!", MB_OK);
return 0;
}
//Creates the main window hwnd
hwnd = CreateWindow(TheClass,
"The Ball Example",
WS_OVERLAPPEDWINDOW,
0,
0,
500,
500,
NULL,
NULL,
hInstance,
NULL);
//Check if there is any error when we make our windows' Hwnd
if(hwnd == NULL)
{
MessageBox(NULL, "Error making the Hwnd for our window","Error",MB_OK);
return 0;
}
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
Since you had your Bitmap as a local it was getting destroyed everytime it left create
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com

-
Jul 5th, 2001, 10:34 AM
#9
Frenzied Member
Oh and you got 2 memory leaks, you need to add:
PHP Code:
DeleteDC(memhdc);
//And
WM_CLOSE:
DeleteObject hbball;
return DefWindowProc(hwnd, msg, wparam, lparam);
break;
Plus if when you get farther along your graphics seem a bit slow on the refresh, make your HDCs global and use select object only once to get them. That way it will not have to recreate them everytime you BitBlt.
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com

-
Jul 6th, 2001, 12:22 AM
#10
Thread Starter
PowerPoster
Great guys!
It is working now.
So the only problem was that I was not declaring my "HBITMAP hbball"as global?
-
Jul 6th, 2001, 01:16 PM
#11
Frenzied Member
If you got it to work using a global then yeah, except for those pesky memory leaks you got
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com

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
|