Device contexts... could someone offer some insight please?
I''m loading several .bmp files into a simple windows program with the hopes that eventually, depending on where I click on the screen, it will change the bitmaps that are displayed (a simple "hot-spot" interface).
Here''s some of the code of what I''m doing, with explanation to follow below it:
----------------------------------------------
HBITMAP hred, hgreen, hblue; // These are global
HDC hdc_current;
HDC hdc;
PAINTSTRUCT ps;
int num=1; // end of global stuff
// I''m not going to print the entire thing.
// Just the relevant stuff.
// This is all in the WindowsProc function:
case WM_CREATE:
{
hred = (HBITMAP)LoadImage(hinstance, "red.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
hgreen = (HBITMAP)LoadImage(hinstance, "green.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
hblue = (HBITMAP)LoadImage(hinstance, "blue.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
hdc = GetDC(hwnd);
hdc_current = CreateCompatibleDC(hdc);
SelectObject(hdc_current, hred);
return(0);
} break;
case WM_PAINT:
{
hdc = BeginPaint(hwnd,&ps);
BitBlt( hdc, 0, 0, 189, 300, hdc_current, 0, 0,SRCCOPY );
EndPaint(hwnd,&ps);
return(0);
} break;
case WM_LBUTTONDOWN:
{
num++;
if ( num > 3 ) { num -= 3; }
if ( num == 1 ) { SelectObject(hdc_current, hred); }
if ( num == 2 ) { SelectObject(hdc_current, hgreen); }
if ( num == 3 ) { SelectObject(hdc_current, hblue); }
SendMessage(hwnd, WM_PAINT, 0, 0);
return(0);
}
case WM_DESTROY:
{
ReleaseDC(hwnd, hdc_current);
DeleteDC(hdc_current);
ReleaseDC(hwnd, hdc);
DeleteDC(hdc);
PostQuitMessage(0);
return(0);
} break;
------------------------------------------
I hope that displayed properly.
What this is supposed to do is load the bitmaps into the HBITMAPs. This works fine. Then I create the hdc_current for the BitBlt function. Then I select the red bitmap (chose this randomly) into the hdc_current handle to device context. In the WM_PAINT I blt the bmp to the window, and it does display correctly. However, when I click the left mouse, it is SUPPOSED to select a different bitmap so that one will be displayed when WM_PAINT is called, but the bmp is never changed.
I am able to display all 3 at once, if I create three hdc''s for them "hdc_red, hdc_blue, hdc_green" for example, but I would MUCH MUCH MUCH MUCH rather use one, and switch between which bmp is selected to it (for our simple game, this would be much better). I''m pretty new at the windows programming stuff, but I really am VERY limited on time. We have about 4 weeks left on this project and this part needs to get done soon so if anyone can help me out at all I would GREATLY appreciate it. Thanks!
a few things that i noticed in your code that might be causing you grief....
1. you are sending your hinstance when loading the images from file. while this is OK, it is unnecessary. you can just put NULL
2. you arent saving the old bitmap (a 1x1 monochrome bitmap) that is in hdc_current. you need to save it somewhere, and restore it during WM_DESTROY
3. at absolutely NO time do you need to use GetDC/ReleaseDC in this application. when you create the compatible DC, you can pass NULL, and the bitmap will be compatible with the screen.
4. sending WM_PAINT as you currently have it will do NOTHING. actually, that isnt true. if you overlapped another application over this app, then put this app again in the foreground, the proper bitmap would be displayed. the reason that this is happening is that you arent invalidating any part of the client area of your window. instead of sending WM_PAINT, you should be calling InvalidateRect(hwnd,NULL,FALSE).
1. you are sending your hinstance when loading the images from file. while this is OK, it is unnecessary. you can just put NULL
2. you arent saving the old bitmap (a 1x1 monochrome bitmap) that is in hdc_current. you need to save it somewhere, and restore it during WM_DESTROY
3. at absolutely NO time do you need to use GetDC/ReleaseDC in this application. when you create the compatible DC, you can pass NULL, and the bitmap will be compatible with the screen.
4. sending WM_PAINT as you currently have it will do NOTHING. actually, that isnt true. if you overlapped another application over this app, then put this app again in the foreground, the proper bitmap would be displayed. the reason that this is happening is that you arent invalidating any part of the client area of your window. instead of sending WM_PAINT, you should be calling InvalidateRect(hwnd,NULL,FALSE).
Get off my lawn!
how is it that i replied to this message, but it says 0 replies?
Get off my lawn!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement