/*_##################################### #######################################
_##
### Programming Windows Programming Design Guide-> Chapter 5 example program 6
_ ## Author: xwlee
_ ## Time: 2007.06.20
### Chang'an University
_ ## Development condition: win2003 SERVER + vc6.0
_##
_ ## Program 5-6 randrect
### Randrect. c file
### Programs that constantly display random rectangles
_ ## The program is only for verification
_##
_##
_ ## Randrect. c -- displays random rectangles
_ ## (C) Charles Petzold, 1998
_##
_####################################### ###################################*/
# Include <windows. h>
# Include <stdlib. h> // For the rand function
Lresult callback wndproc (hwnd, uint, wparam, lparam );
Void drawrectangle (hwnd );
Int cxclient, cyclient;
Int winapi winmain (hinstance, hinstance hprevinstance,
Pstr szcmdline, int icmdshow)
{
Static tchar szappname [] = text ("randrect ");
Hwnd;
MSG;
Wndclass;
Int I;
Wndclass. Style = cs_hredraw | cs_vredraw;
Wndclass. lpfnwndproc = wndproc;
Wndclass. cbclsextra = 0;
Wndclass. cbwndextra = 0;
Wndclass. hinstance = hinstance;
Wndclass. hicon = loadicon (null, idi_application );
Wndclass. hcursor = loadcursor (null, idc_arrow );
Wndclass. hbrbackground = (hbrush) getstockobject (white_brush );
Wndclass. lpszmenuname = NULL;
Wndclass. lpszclassname = szappname;
If (! Registerclass (& wndclass ))
{
MessageBox (null, text ("this program requires Windows NT! "),
Szappname, mb_iconerror );
Return 0;
}
Hwnd = createwindow (szappname, text ("random rectangles "),
Ws_overlappedwindow,
Cw_usedefault, cw_usedefault,
Cw_usedefault, cw_usedefault,
Null, null, hinstance, null );
Showwindow (hwnd, icmdshow );
Updatewindow (hwnd );
While (true)
{
If (peekmessage (& MSG, null, 0, 0, pm_remove ))
{
If (msg. Message = wm_quit)
Break;
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}
Else
{
For (I = 0; I <10000000; I ++ );
Drawrectangle (hwnd );
For (I = 0; I <10000000; I ++ );
}
}
Return msg. wparam;
}
Lresult callback wndproc (hwnd, uint imsg, wparam, lparam)
{
Switch (imsg)
{
Case wm_size:
Cxclient = loword (lparam );
Cyclient = hiword (lparam );
Return 0;
Case wm_destroy:
Postquitmessage (0 );
Return 0;
}
Return defwindowproc (hwnd, imsg, wparam, lparam );
}
Void drawrectangle (hwnd)
{
Hbrush;
HDC;
Rect;
If (cxclient = 0 | cyclient = 0)
Return;
Setrect (& rect, Rand () % cxclient, Rand () % cyclient,
Rand () % cxclient, Rand () % cyclient );
Hbrush = createsolidbrush (
RGB (RAND () % 256, Rand () % 256, Rand () % 256 ));
HDC = getdc (hwnd );
Fillrect (HDC, & rect, hbrush );
Releasedc (hwnd, HDC );
Deleteobject (hbrush );
}