问题
My win32 GUI content changes every second but it doesn't show update unless the window is manually moved. I tried to pop up a messagebox every second to trigger the window to refresh and it worked. Hence it proves that my content really changes but the window doesnt update. I want the window to refresh without popping up a messagebox everytime, is there a windows function for this? Thanks
case WM_PAINT:
RECT fingerprintSection;
fingerprintSection.left=500;
fingerprintSection.top=300;
fingerprintSection.bottom=540;
fingerprintSection.right=660;
wmId = LOWORD(wParam);
hdc = BeginPaint(hWnd, &ps);
refresh=!refresh;
if((start==true)&&(refresh==true)&&(stop!=true))
{
windowName = MultiByteStringToWideString(name1, CP_ACP);
LoadAndBlitBitmap(windowName.c_str(), hdc,500,0);//loading a picture that doesnt change
fingerprint();
LoadAndBlitBitmap(TEXT("outresized.bmp"), hdc,500,300);//loading a picture that constantly change
refresh=!refresh;
//RedrawWindow(hWnd,&fingerprintSection,NULL,RDW_INTERNALPAINT|RDW_VALIDATE|RDW_UPDATENOW|RDW_NOCHILDREN);
InvalidateRect( hWnd, &fingerprintSection, TRUE );
}
EndPaint(hWnd, &ps);
break;
回答1:
If you are drawing in a window outside of WM_PAINT (I guess in this case you are probably doing some GDI drawing on a timer when you get a WM_TIMER message), then you should call InvalidateRect() e.g.
InvalidateRect( hWnd, NULL, FALSE ); // invalidate whole window
This should cause Windows to redraw the whole window. If you have only written a small area, pass a RECT describing the area you have written.
If you want to draw when your windows procedure receives a WM_PAINT message AND for example, want to force that to happen every second, then set a timer...
#define SECOND_TIMER 1000
case WM_INITDIALOG:
SetTimer( hWnd, SECOND_TIMER, SECOND_TIMER, NULL );
//other initialisation stuff
break;
case WM_TIMER:
if( wParam == SECOND_TIMER )
{
InvalidateRect( hWnd, NULL, FALSE ); // invalidate whole window
}
break;
In this simple case the whole window should be redrawn every second since Windows will send a WM_PAINT message due to the InvalidateRect. Ideally you should only invalidate the part you are redrawing.
来源:https://stackoverflow.com/questions/22277773/win32-content-changed-but-doesnt-show-update-unless-window-is-moved