如题:WinMain里的WNDCLASS的窗口类名称为什么都用static修饰???windows程序设计的每个例子都是这样的
#include <windows.h>
 
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     static TCHAR szAppName[] = TEXT ("HelloWin") ;
     WNDCLASS     wndclass ;
     ……
     wndclass.lpszClassName = szAppName ;
     ……main里边的这个szAppName[] 就算不加static,也是与main函数同生存时间的
这地方又必要吗?为什么要这样写?

解决方案 »

  1.   

    因为 WNDCLASS::lpszClassName 是一个指针
      

  2.   

    现在看来加不加static 不应影响代码的实现,但使用static依赖性更小。
    因为szAppName 需要放在静态存储区,程序运行期间经常要用(动态分配内存当然也可以),
    使用static 关键字显式指定要存在静态存储区,而字符串常量则不然,是约定或者是特性。
    貌似两者的差别比较模糊了。
      

  3.   

    static的是预先放于内存中静态存储区访问的而VS2010后来就是全局函数了差别并不明显,用和不用都可以的
      

  4.   

    你分别将
    int x=0,y=0;
    POINT pt[2]={x,y,x+20,y+20};
    前加上static试试运行,按上下键观察斜线能否运动.
    #include<Windows.h>
    LRESULT CALLBACK Proc(HWND,UINT,WPARAM,LPARAM);
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine ,INT nCmdShow)
    {
        WNDCLASS wndclass;
        MSG msg;
        HWND hwnd;
    wndclass.cbClsExtra=0;
    wndclass.cbWndExtra=0;
    wndclass.hInstance=hInstance;
    wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
    wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wndclass.lpfnWndProc= Proc;
    wndclass.lpszClassName="Move";
    wndclass.lpszMenuName=NULL;
    wndclass.style=NULL;
    RegisterClass(&wndclass);
    hwnd=CreateWindow ("Move","UP&DOWN",WS_OVERLAPPEDWINDOW,300,200,800,600,NULL,NULL,hInstance,NULL);
    ShowWindow(hwnd,nCmdShow);
    while(GetMessage(&msg,NULL,0,0))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    return 0;
    }
    LRESULT CALLBACK Proc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
    {
       int x=0,y=0;
       POINT pt[2]={x,y,x+20,y+20};
        HDC hdc;
        PAINTSTRUCT ps;
    switch(msg)
    {
            case WM_KEYDOWN:
                switch(wParam)
                {
                    case VK_DOWN:
                        y+=10;
                        break;
    case VK_UP:
    y-=10;
    break;
    }
    InvalidateRect(hwnd,NULL,TRUE);
    break;
    case WM_PAINT:
    hdc=BeginPaint(hwnd,&ps);
    Polyline(hdc,pt,2);
    EndPaint(hwnd,&ps);
    break;
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    }
    return DefWindowProc(hwnd,msg,wParam,lParam);
    }