Monday 28 November 2011

Will Windows always save and refresh your program’s windows? in C programming

Will Windows always save and refresh your program’s windows?

No. Windows itself is not responsible for saving and restoring your program’s windows. Instead, Windows sends a message to your program—the WM_PAINT message—that notifies your program that its client area needs repainting.

The client area refers to the portion of the screen that your program is in control of—that is, the portion of the screen occupied by your program’s windows. Whenever another program overlays your program with another window, your client area is covered by that application’s window. When that application’s window is removed, Windows sends a WM_PAINT message to your program. Your Windows program should contain an event loop that looks for and responds to such messages. When your program receives the WM_PAINT message, you are responsible for initiating the appropriate code to repaint your application’s window.

The WM_PAINT message is generated by Windows when one of the following events occurs:
  •  A previously hidden portion of your program’s client area is uncovered.
  •   Your application’s window is moved or resized.
  •   Your application’s window is scrolled by the use of a scrollbar.
  •  A pull-down or pop-up menu is invoked.
Additionally, you can force your program to repaint the screen (thus generating a WM_PAINT message to your own program) by calling the Windows API function InvalidateRect(). 

Your program should contain an event loop that captures incoming messages and responds to them. Here is an example of a typical event loop that responds to a WM_PAINT message:

switch(message)
{
...
case WM_PAINT:
hdcOutput = BeginPaint(hwndMyWindow, &psPaint);
hpenDraw = CreatePen(PS_SOLID, 3, 0L);
SelectObject(hdcOutput, hpenDraw);
Rectangle(hdcOutput, 0, 0, 150, 150);
TextOut(hdcOutput, 200, 200, “Just the FAQ’s, Ma’am...”, 24);
...
}

When the preceding program is run, a WM_PAINT message is generated by Windows on program start-up and any time the client area is moved, resized, or scrolled. It should be noted that actions such as cursor movement and drag-and-drop operations do not require a WM_PAINT message to be generated. In these cases, Windows saves and restores the portion of the screen that has been covered with the cursor or icon.

Cross Reference:

XXI.14: How do you determine a Windows program’s client area size?

No comments:

Post a Comment