首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • 关于static用法的问题
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-07-22 09:02:24 楼主
    该程序的功能是,把客户区分为5×5的单元格,然后当鼠标之余某一个单元格内时则在此单元格内画两条对角线,当用户在同一单元格内再点击一次,则两条对角线消失。
    下面为整个源程序,请大家给我看看当我把蓝色代码改成红色的就会出现一堆错误,请问错误原因在哪,为什么不能用非static的变量,如果能用该怎么改?

    #include <windows.h>

    #define DIVISIONS 5

    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR  szCmdLine, int iCmdShow)
    {
        static TCHAR szAppName[] = TEXT ("Checker1") ;
        HWND        hwnd ;
        MSG          msg ;
        WNDCLASS    wndclass ;
       
        wndclass.style        = CS_HREDRAW ¦ CS_VREDRAW ;
        wndclass.lpfnWndProc  = WndProc ;
        wndclass.cbClsExtra    = 0 ;
        wndclass.cbWndExtra    = 0 ;
        wndclass.hInstance    = hInstance ;
        wndclass.hIcon        = LoadIcon (NULL, IDI_APPLICATION) ;
        wndclass.hCursor      = LoadCursor (NULL, IDC_ARROW) ;
        wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
        wndclass.lpszMenuName  = NULL ;
        wndclass.lpszClassName = szAppName ;
       
        if (!RegisterClass (&wndclass))
        {
              MessageBox (NULL, TEXT ("Program requires Windows NT!"),
                          szAppName, MB_ICONERROR) ;
              return 0 ;
        }
       
        hwnd = CreateWindow (szAppName, TEXT ("Checker1 Mouse Hit-Test Demo"),
                              WS_OVERLAPPEDWINDOW,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              NULL, NULL, hInstance, NULL) ;
       
        ShowWindow (hwnd, iCmdShow) ;
        UpdateWindow (hwnd) ;
       
        while (GetMessage (&msg, NULL, 0, 0))
        {
              TranslateMessage (&msg) ;
              DispatchMessage (&msg) ;
        }
        return msg.wParam ;
    }

    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        //把这句替换为下面的就会错为什么??
        //static BOOL fState[DIVISIONS][DIVISIONS] ;
        BOOL fState[DIVISIONS][DIVISIONS];
        memset(fState,0,sizeof(BOOL)*25);

        static int  cxBlock=0, cyBlock=0; //每个矩形框的宽度和高度
        HDC        hdc ;
        int        x, y ;
        PAINTSTRUCT ps ;
        RECT        rect ;
       
        switch (message)
        {
        case WM_SIZE :
              cxBlock = LOWORD (lParam) / DIVISIONS ;
              cyBlock = HIWORD (lParam) / DIVISIONS ;
              return 0 ;
             
        case WM_LBUTTONDOWN :
              x = LOWORD (lParam) / cxBlock ;
              y = HIWORD (lParam) / cyBlock ;
             
              if (x < DIVISIONS && y < DIVISIONS)
              {
                  fState [x][y] ^= 1 ; //每次fState[x][y]与1进行异或运算后将结果赋给fState[x][y]
                 
                  rect.left  = x * cxBlock ;
                  rect.top    = y * cyBlock ;
                  rect.right  = (x + 1) * cxBlock ;
                  rect.bottom = (y + 1) * cyBlock ;
     
                  //使整个客户区都无效
                  InvalidateRect (hwnd, &rect, FALSE) ;
              }
              else
                  MessageBeep (0) ;
              return 0 ;
             
        case WM_PAINT :

      //每次都将整个客户区根据fState矩阵的值进行重绘
          hdc = BeginPaint (hwnd, &ps) ;
             
              for (x = 0 ; x < DIVISIONS ; x++)
              for (y = 0 ; y < DIVISIONS ; y++)
              {
                  Rectangle (hdc, x * cxBlock, y * cyBlock,
                            (x + 1) * cxBlock, (y + 1) * cyBlock) ;
                       
                  if (fState [x][y])
                  {
                        MoveToEx (hdc,  x    * cxBlock,  y    * cyBlock, NULL) ;
                        LineTo  (hdc, (x+1) * cxBlock, (y+1) * cyBlock) ;
                        MoveToEx (hdc,  x    * cxBlock, (y+1) * cyBlock, NULL) ;
                        LineTo  (hdc, (x+1) * cxBlock,  y    * cyBlock) ;
                  }
              }
              EndPaint (hwnd, &ps) ;
              return 0 ;
                 
        case WM_DESTROY :
              PostQuitMessage (0) ;
              return 0 ;
        }
        return DefWindowProc (hwnd, message, wParam, lParam) ;
    }
    40  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • zaodt
    • 等级:
    发表于:2008-07-22 09:08:191楼 得分:0

    BOOL fState[DIVISIONS][DIVISIONS];


    这是局部变量,函数退出后就没了;


    所以前边要加个 static ,声明为静态变量,


    可以在 C 语言书中找到这个 static
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • vcPlayer
    • 等级:
    发表于:2008-07-22 09:19:502楼 得分:0
    引用 1 楼 zaodt 的回复:

    BOOL fState[DIVISIONS][DIVISIONS];


    这是局部变量,函数退出后就没了;


    所以前边要加个 static ,声明为静态变量,


    可以在 C 语言书中找到这个 static


    相当于把局部变量的生存期延长到与全局变量一样。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • Conry
    • 等级:
    发表于:2008-07-22 09:58:473楼 得分:0
    在我这里没有错误,lz看看改了其它地方没有
    还有你的错误是什么
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-07-22 17:28:264楼 得分:0
    Conry 你好,我就是程序中蓝色部分代码替换为红色部分,想利用非static进行实现,但产生很多错误和警告,不知什么原因,请给我详解,谢谢!
    错误和警告如下所示:

    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(61) : error C2143: syntax error : missing ';' before 'type'
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(62) : error C2275: 'HDC' : illegal use of this type as an expression
            d:\program files\microsoft visual studio\vc98\include\windef.h(239) : see declaration of 'HDC'
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(62) : error C2146: syntax error : missing ';' before identifier 'hdc'
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(62) : error C2065: 'hdc' : undeclared identifier
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(63) : error C2143: syntax error : missing ';' before 'type'
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(64) : error C2275: 'PAINTSTRUCT' : illegal use of this type as an expression
            d:\program files\microsoft visual studio\vc98\include\winuser.h(2170) : see declaration of 'PAINTSTRUCT'
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(64) : error C2146: syntax error : missing ';' before identifier 'ps'
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(64) : error C2065: 'ps' : undeclared identifier
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(65) : error C2275: 'RECT' : illegal use of this type as an expression
            d:\program files\microsoft visual studio\vc98\include\windef.h(292) : see declaration of 'RECT'
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(65) : error C2146: syntax error : missing ';' before identifier 'rect'
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(65) : error C2065: 'rect' : undeclared identifier
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(70) : error C2065: 'cxBlock' : undeclared identifier
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(71) : error C2065: 'cyBlock' : undeclared identifier
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(75) : error C2065: 'x' : undeclared identifier
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(76) : error C2065: 'y' : undeclared identifier
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(82) : error C2224: left of '.left' must have struct/union type
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(83) : error C2224: left of '.top' must have struct/union type
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(84) : error C2224: left of '.right' must have struct/union type
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(85) : error C2224: left of '.bottom' must have struct/union type
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(88) : warning C4133: 'function' : incompatible types - from 'int *' to 'const struct tagRECT *'
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(97) : warning C4133: 'function' : incompatible types - from 'int *' to 'struct tagPAINTSTRUCT *'
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(97) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct HDC__ *'
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(102) : warning C4047: 'function' : 'struct HDC__ *' differs in levels of indirection from 'int '
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(102) : warning C4024: 'Rectangle' : different types for formal and actual parameter 1
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(107) : warning C4047: 'function' : 'struct HDC__ *' differs in levels of indirection from 'int '
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(107) : warning C4024: 'MoveToEx' : different types for formal and actual parameter 1
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(108) : warning C4047: 'function' : 'struct HDC__ *' differs in levels of indirection from 'int '
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(108) : warning C4024: 'LineTo' : different types for formal and actual parameter 1
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(109) : warning C4047: 'function' : 'struct HDC__ *' differs in levels of indirection from 'int '
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(109) : warning C4024: 'MoveToEx' : different types for formal and actual parameter 1
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(110) : warning C4047: 'function' : 'struct HDC__ *' differs in levels of indirection from 'int '
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(110) : warning C4024: 'LineTo' : different types for formal and actual parameter 1
    F:\C++书籍\Windows程序设计\源码\Chap07\Checker1\Checker1.c(113) : warning C4133: 'function' : incompatible types - from 'int *' to 'const struct tagPAINTSTRUCT *'
    Error executing cl.exe.
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • lfchen
    • 等级:
    发表于:2008-07-22 17:37:575楼 得分:0
    BOOL fState[DIVISIONS][DIVISIONS];
        memset(fState,0,sizeof(BOOL)*25);
    //这两句没有问题,这段程序也不会因为改了这两句出现这么多错误
    //看第一个错误,是在type之前缺少;
    //找找哪里有type
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-07-22 17:47:046楼 得分:0
    但我如果用例程上的蓝色代码运行,则没有错误,请问这是为什么,而且我很疑惑的是为什么例程声明为static后,没有进行初始化也是对的。
    我如果不声明为static,这个变量也是作用在WndProc中,只是一个初始化一次,一个是此次初始化吧。
    修改 删除 举报 引用 回复

    网站简介广告服务网站地图帮助联系方式诚聘英才English 问题报告
    北京创新乐知广告有限公司 版权所有 京 ICP 证 070598 号
    世纪乐知(北京)网络技术有限公司 提供技术支持
    Copyright © 2000-2008, CSDN.NET, All Rights Reserved