<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」。我很困惑~~多謝~~~ :)隨機矩形窗口API程序...我不知道變量如何得到它的值
您是否檢查了發生「WM_SIZE」消息時會發生什麼?看起來像轉讓給我。 –