簡體   English   中英

Windows API程序上的隨機散角...我不知道變量如何獲得其值

[英]A windows API program on random ractangles…I don't know how the variable get its value

<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“。 我很困惑~~非常感謝~~~ :)

您是在問為什么當clientxclienty沒有顯式初始化為零時它們都具有零值。

全局變量(如clientxclienty )具有靜態存儲持續時間。 如果未顯式初始化具有靜態存儲持續時間的變量(根據C99標准的6.7.8節):

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

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

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

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

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

Windows在以下注冊表位置中保存已關閉窗口的大小和位置信息:HKEY_CURRENT_USER \\ Software \\ Microsoft \\ Windows \\ CurrentVersion \\ Explorer \\ Streams

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM