Advertisement

GDI and WM_PAINT message

Started by May 24, 2002 10:21 AM
6 comments, last by jplocster 22 years, 5 months ago
Hi, I am currently trying to display a bitmap in a child window but if that window gets resized the bitmap disappears. I have tried processing the WM_PAINT message in the child windows WindowProc() but I am doing something wrong as my program will hang when I rezise the window (randomly it seems). What is the correct method of repainting a window / processing the WM_PAINT message? I am using MS VC++ 6.0. I have been using GDI functions to load the bitmap. Here is the code inside the child windows WinProc() HDC my_DC; RECT my_rect= {0,0,0,0}; LPRECT WRect = &my_rect; int x = 0; int y = 0; HBITMAP my_bmp; switch( msg ) { case WM_PAINT: my_bmp = (HBITMAP)LoadImage(NULL,"bir.bmp",IMAGE_BITMAP,0 ,0,LR_LOADFROMFILE); if(my_bmp == NULL) { assert(0); } my_DC = CreateDC("DISPLAY" ,NULL,NULL,NULL); if(my_DC == NULL) { assert(0); } if( !GetWindowRect(hWnd,WRect) ) { assert(0); } DrawState(my_DC,NULL,NULL,(LONG)my_bmp,NULL,WRect->left,WRect->top,WRect->right - WRect->left,WRect->bottom - WRect->top,DST_BITMAP); DeleteDC(my_DC); break; case WM_SIZE: UpdateWindow(hWnd); break; case WM_CLOSE: DestroyWindow(hWnd); break; } return DefWindowProc( hWnd, msg, wParam, lParam );
It looks like you need to add BeginPaint and EndPaint there...
EDIT: there's an example over here

[edited by - Alimonster on May 24, 2002 11:37:25 AM]
Advertisement
as in this:

case WM_PAINT:
BeginPaint(hWnd,lpPaint);
my_bmp = (HBITMAP)LoadImage(NULL,"bir.bmp",IMAGE_BITMAP,0 ,0,LR_LOADFROMFILE);
if(my_bmp == NULL)
{
assert(0);
}
my_DC = CreateDC("DISPLAY" ,NULL,NULL,NULL);
if(my_DC == NULL)
{
assert(0);
}

if( !GetWindowRect(hWnd,WRect) )
{
assert(0);
}
DrawState(my_DC,NULL,NULL,(LONG)my_bmp,NULL,WRect->left,WRect->top,WRect->right - WRect->left,WRect->bottom - WRect->top,DST_BITMAP);
DeleteDC(my_DC);
EndPaint(hWnd,lpPaint);
break;
Have a look at the edit link I posted above. You get the DC to use for your window returned via the BeginPaint call, so you don''t have to create your own DC.
GOTCHA!

I rewrote the code to handle the WM_PAINT message as follows:

case WM_PAINT:
my_DC = BeginPaint(hWnd,&my_paint);
my_bmp = (HBITMAP)LoadImage(NULL,"bir.bmp",IMAGE_BITMAP,0 ,0,LR_LOADFROMFILE);
if(my_bmp == NULL)
{
assert(0);
}
if( !GetWindowRect(hWnd,&my_rect) )
{
assert(0);
}
if( !GetClientRect(hWnd,&client_rect) )
{
assert(0);
}
y = my_rect.top + ((my_rect.top - my_rect.bottom) - client_rect.bottom);
DrawState(my_DC,NULL,NULL,(LONG)my_bmp,NULL,my_rect.left,y,my_rect.right - my_rect.left,my_rect.bottom - y,DST_BITMAP);
EndPaint(hWnd,&my_paint);
break;

However, the bitmap is still not being redrawn. Any other ideas. P.S. it doesnt seem to crash anymore. Thx!
Perhaps my method for displaying a bitmap in a child window is flawed to begin with? Is there an easier way? A more "correct" way? MSDN help did show any other functions for loading bitmaps from a file (at least in my search), only from a resource in the executable. I originally read in the bitmap data and used the setpixel function, but that was sooooooo slow. Anyway, my goal is to be able to display bitmaps in a client window. The main window would be able to have many child windows all with seperate bitmaps in them. Any ideas, comments, verbal lashings?
Advertisement
Here''s a sample function. It''s written in Delphi but the syntax is just about the same and you should be able to guess what everything does. If not then feel free to ask me:

procedure DrawOurStuff(DrawDC: HDC);var  Bmp: HBITMAP;  BmpInfo: BITMAP;  DC: HDC;  Width, Height: Integer;begin  Bmp := LoadImage(GetModuleHandle(nil), ''circles.bmp'', IMAGE_BITMAP, 0,0, LR_LOADFROMFILE or                                                                         LR_CREATEDIBSECTION);  DC := CreateCompatibleDC(0);  assert(DC <> 0);  SelectObject(DC, Bmp);  GetObject(bmp, sizeof(BITMAP), @bmpInfo);  Width := bmpInfo.bmWidth;  Height := bmpInfo.bmHeight;  BitBlt(DrawDC, 0,0, Width, Height, DC, 0,0, SRCCOPY);  DeleteDC(DC);  DeleteObject(Bmp);end; 

The above code draws a bitmap to the screen. (Remember that Delphi isn''t case-sensitive, and that '':='' means ''='' in C or C++.
SWEET,

That pretty much fixed it! There are still a few minor issues with multiple windows overlapping and interfering with each other but I think I can handle that. Thanks a bunch

This topic is closed to new replies.

Advertisement