c - 随机矩形上的 Windows API 程序...我不知道变量如何获取其值

标签 c windows multithreading api mfc

<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 初始化 节):

  • 如果是指针类型,则初始化为空指针;
  • 如果是算术类型,则初始化为(正数或无符号)零;
  • 如果是聚合,则根据这些规则(递归地)初始化每个成员;
  • 如果是 union 体,则根据这些来初始化(递归地)第一个命名成员 规则。

关于c - 随机矩形上的 Windows API 程序...我不知道变量如何获取其值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12596856/

相关文章:

c - 程序在这里如何使用堆栈?

windows - 更改 Azure 存储 Blob 中文件的内容类型

python - 使用计时器逐行读取

c - 为什么-32768 在下面的平台上有 signed long 类型?

c++ - 为什么使用 '\0' 而不是 memset 的 0?

c - c中的模板样式矩阵实现

Windows azure : Security Concerns

wpf - WPF 是用户界面设计的 future 吗?我现在应该学吗?

multithreading - coldfusion 11后台处理程序和cfmail线程

multithreading - 带有异步 block 的串行调度队列