<pre>
#include<Windows.h>
#include<process.h>
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
HWND hwnd;
int clientx,clienty;
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
{
static TCHAR szAppName[]=TEXT("hello");
MSG msg;
WNDCLASS wndclass;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.hInstance=hInstance;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.lpfnWndProc=WndProc;
wndclass.lpszClassName=szAppName;
wndclass.lpszMenuName=NULL;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("this program requires windows NT"),TEXT("wrong"),MB_ICONERROR);
return 0;
}
hwnd=CreateWindow(szAppName,TEXT("random rectangles"),
WS_OVERLAPPEDWINDOW,
100,100,800,600,
NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
VOID Thread(PVOID pvoid)
{
HBRUSH hbrush;
HDC hdc;
int xleft,xright,ytop,ybottom,ired,igreen,iblue;
while(TRUE)
{
if(clientx!=0||clienty!=0)
{
xleft=rand()%clientx;
xright=rand()%clientx;
ytop=rand()%clienty;
ybottom=rand()%clienty;
ired=rand()%255;
igreen=rand()%255;
iblue=rand()%255;
hdc=GetDC(hwnd);
hbrush=CreateSolidBrush(RGB(ired,igreen,iblue));
SelectObject(hdc,hbrush);
Rectangle(hdc,min(xleft,xright),min(ytop,ybottom),max(xleft,xright),max(ytop,ybottom));
ReleaseDC(hwnd,hdc);
DeleteObject(hbrush);
}
}//while
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_CREATE:
_beginthread(Thread,0,NULL);
return 0;
case WM_SIZE:
clientx=LOWORD(lParam);
clienty=HIWORD(lParam);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
<code>
I do not know how the variable clientx and clienty at the top of the program get their value s when the program runs…Because I didn’t see any value assignments in the program ….I used to debug it in my visual studio 2010, when it ran to “ShowWindow(hwnd,iCmdShow);”in the WinMain(),the clientx and clienty got their values (735 and 654 random…).But before that the clientx and clienty both were “0”. I was confused~~ Thanks a lot~~~ 🙂
I think you are asking why
clientxandclientyboth have value zero when they are not explicitly initialised to zero.A global variable, as
clientxandclienty, has static storage duration. If a variable with static storage duration is not explicitly initialised (from section 6.7.8 Initialization of C99 standard):rules.