<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>
我不知道程序顶部的变量 clientx 和 clienty 在程序运行时如何获得它们的值...因为我在程序中没有看到任何值分配 ....我曾经在我的Visual Studio 2010,当它在 WinMain() 中运行到“ShowWindow(hwnd,iCmdShow);”时,clientx 和 clienty 得到了它们的值(735 和 654 随机......)。但在此之前,clientx 和 clienty 都是“ 0”。我很困惑~~非常感谢~~~ :)