2012-09-26 24 views
0
<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程序...我不知道變量如何得到它的值

+1

您是否檢查了發生「WM_SIZE」消息時會發生什麼?看起來像轉讓給我。 –

回答

0

認爲你會問,爲什麼clientxclienty都有價值爲零時,他們沒有明確初始化爲零。

一個全局變量,如clientxclienty,具有靜態存儲持續時間。如果具有靜態存儲持續時間的變量沒有明確地初始化(從C99標準的部分6.7.8 初始化):

  • 如果它有指針類型,它被初始化爲空指針;
  • 如果它有算術類型,它被初始化爲(正或無符號)零;
  • 如果它是一個聚合,每個成員根據這些規則初始化(遞歸);
  • 如果它是一個聯合,則根據這些 規則初始化(遞歸)第一個命名成員。
0

客戶端x和客戶端y被初始化爲0(如hmjd所述),因爲它們是全局的。

當應用程序打開時,Windows向窗口過程發送WM_RESIZE消息,告訴它窗口有多大(如果用戶調整窗口大小,則再次發送該消息)。在底部附近,您可以看到根據RESIZE消息的參數設置clientx和clienty的代碼 - 實質上它們是客戶端窗口的高度和寬度(以像素爲單位)。

-1

這些值來自您以前的會話。當您關閉頂級窗口時,Windows會記住它的大小和位置。

http://support.microsoft.com/kb/235994

Windows保存封閉式窗戶在 以下注冊表位置大小和位置信息: HKEY_CURRENT_USER \軟件\微軟\的Windows \ CurrentVersion \ Explorer中\流

Windows保存大小和位置信息最多28個不同的 窗口。每個窗口的大小和位置參數都存儲在Streams項的 子項中。

相關問題