none
是API高手的就来看 RRS feed

  • 问题

  • <windows程序设计>"鼠标"中 CHECKER3 代码,那段代码可能是在vc6.0编译器上写的,但是无论如何无法在vc2010上运行,是注册错误?是窗口创建错误?是主消息程序错误?还是......求高手帮助,不胜感激!!下面是原代码改写到vc2010上,找不到问题所在,QQ1327729940

    // CHECKER3..cpp : 定义应用程序的入口点。
    //

    #include "stdafx.h"
    #include "CHECKER3..h"
    #define DIVISIONS 5
    #define MAX_LOADSTRING 100
    HINSTANCE hInst;        // 当前实例
    TCHAR szTitle[MAX_LOADSTRING];     // 标题栏文本
    static TCHAR szAppName[MAX_LOADSTRING];   // 主窗口类名
    TCHAR szChildClass[MAX_LOADSTRING];
    ATOM    MyRegisterClass(HINSTANCE hInstance);
    BOOL    InitInstance(HINSTANCE,int);
    LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
    LRESULT CALLBACK    ChildWndProc(HWND,UINT,WPARAM,LPARAM);
    INT_PTR CALLBACK About(HWND,UINT,WPARAM,LPARAM);
    int APIENTRY _tWinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPTSTR    lpCmdLine,
                         int       nCmdShow)
    {
     UNREFERENCED_PARAMETER(hPrevInstance);
     UNREFERENCED_PARAMETER(lpCmdLine);
     // TODO: 在此放置代码。
     MSG msg;
     HACCEL hAccelTable;
     // 初始化全局字符串
     LoadString(hInstance,IDS_APP_TITLE,szTitle,MAX_LOADSTRING);
     LoadString(hInstance,IDC_CHECKER3,szAppName,MAX_LOADSTRING);
     MyRegisterClass(hInstance);
     // 执行应用程序初始化:
     if (!InitInstance(hInstance,nCmdShow))
     {
      return FALSE;
     }
     hAccelTable=LoadAccelerators(hInstance,MAKEINTRESOURCE(IDC_CHECKER3));
     while(GetMessage(&msg,NULL,0,0))
     {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
     }
     return(int)msg.wParam;
    }
    ATOM MyRegisterClass(HINSTANCE hInstance)
    {
     WNDCLASS wcex;
     wcex.style   = CS_HREDRAW|CS_VREDRAW;
     wcex.lpfnWndProc = WndProc;
     wcex.cbClsExtra  = 0;
     wcex.cbWndExtra  = 0;
     wcex.hInstance  = hInstance;
     wcex.hIcon   = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_CHECKER3));
     wcex.hCursor  = LoadCursor(NULL,IDC_ARROW);
     wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
     wcex.lpszMenuName = MAKEINTRESOURCE(IDC_CHECKER3);
     wcex.lpszClassName = szAppName;
     if(!RegisterClass(&wcex))
     {
      MessageBox(NULL,TEXT("主程序句柄未能注册!!"),szAppName,MB_ICONERROR);
      return 0;
     }
     wcex.lpfnWndProc    =ChildWndProc;
     wcex.cbWndExtra     =sizeof(long);
     wcex.hIcon          =NULL;
     wcex.lpszClassName  =szChildClass;
     return RegisterClass(&wcex);
    }
    BOOL InitInstance(HINSTANCE hInstance,int nCmdShow)
    {
       HWND hWnd;
       hInst=hInstance; // 将实例句柄存储在全局变量中
       hWnd=CreateWindow(szAppName,TEXT("ALEX_程学武"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,CW_USEDEFAULT,
        CW_USEDEFAULT,CW_USEDEFAULT,
        NULL,NULL,hInstance,NULL);
       if (!hWnd)
       {
        return FALSE;
       }
       ShowWindow(hWnd,nCmdShow);
       UpdateWindow(hWnd);
       return TRUE;
    }
    LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
    {
     int x,y,cxBlock,cyBlock;
     static HWND hwndChild[DIVISIONS][DIVISIONS];
     HDC hdc;
     RECT rect;
     PAINTSTRUCT ps;
     switch(message)
     {
     case WM_CREATE:
      for(x=0;x<5;x++)
       for(y=0;y<5;y++)
        hwndChild[x][y]=CreateWindow(szChildClass,NULL,
        WS_CHILDWINDOW|WS_VISIBLE,0,0,0,0,hWnd,
        (HMENU)(y|x),
        (HINSTANCE)GetWindowLong(hWnd,GWL_HINSTANCE),NULL);
      return 0;
     case WM_SIZE:
      cxBlock=LOWORD(lParam)/5;
      cyBlock=HIWORD(lParam)/5;
      for(x=0;x<5;x++)
       for(y=0;y<5;y++)
        MoveWindow(hwndChild[x][y],x*cxBlock,y*cyBlock,
        cxBlock,cyBlock,TRUE);
      return 0;
     case WM_LBUTTONDOWN:
      MessageBeep(0);
      return 0;
      PostQuitMessage(0);
      return 0;
     }
     return DefWindowProc(hWnd,message,wParam,lParam);
    }
    LRESULT CALLBACK ChildWndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
    {
     int wmId,wmEvent;
     PAINTSTRUCT ps;
     HDC hdc;
     RECT rect;
     switch (message)
     {
     case WM_CREATE:
      SetWindowLong(hWnd,0,0);
      return 0;
     case WM_LBUTTONDOWN:
      SetWindowLong(hWnd,0,~GetWindowLong(hWnd,0));
      InvalidateRect (hWnd,NULL,FALSE);
      return 0;
     case WM_PAINT:
      hdc = BeginPaint(hWnd,&ps);
      GetClientRect(hWnd,&rect);
      Rectangle(hdc,0,0,rect.right,rect.bottom);
      // TODO: 在此添加任意绘图代码...
      if(GetWindowLong(hWnd,0))
      {
       MoveToEx(hdc,0,0,NULL);
       LineTo(hdc,rect.right,rect.bottom);
       MoveToEx(hdc,0,rect.bottom,NULL);
       LineTo(hdc,rect.right,0);
      }
      EndPaint(hWnd,&ps);
      break;
     case WM_DESTROY:
      PostQuitMessage(0);
      break;
     }
     return DefWindowProc(hWnd,message,wParam,lParam);
    }
    INT_PTR CALLBACK About(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
    {
     UNREFERENCED_PARAMETER(lParam);
     switch (message)
     {
     case WM_INITDIALOG:
      return (INT_PTR)TRUE;
     case WM_COMMAND:
      if (LOWORD(wParam)==IDOK||LOWORD(wParam)==IDCANCEL)
      {
       EndDialog(hDlg,LOWORD(wParam));
       return(INT_PTR)TRUE;
      }
      break;
     }
     return (INT_PTR)FALSE;
    }

    2011年5月17日 2:49

答案

  • 您所说的无论如何无法在vc2010上运行指的是什么?无法运行还是窗口显示不出来?或者窗口能显示,但结果不正确?

    您的TCHAR szChildClass[MAX_LOADSTRING] ;这个是在哪里赋值的呢?


    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
    2011年5月17日 2:54
    版主
  • 版主,问题解决了啊啊啊,三个星期了,原来问题出在这里,正如您所说的 TCHAR szChildClass[MAX_LOADSTRING]=TEXT("MMM") 我以前也这样赋值的,可不知怎么回事啊,好好,感谢版主,感谢MSDN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    2011年5月17日 5:58

全部回复

  • 您所说的无论如何无法在vc2010上运行指的是什么?无法运行还是窗口显示不出来?或者窗口能显示,但结果不正确?

    您的TCHAR szChildClass[MAX_LOADSTRING] ;这个是在哪里赋值的呢?


    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
    2011年5月17日 2:54
    版主
  • 您好,非常感谢您在百忙中看我的贴子

    其实 TCHAR szChildClass[MAX_LOADSTRING] 完全可以这样"TCHAR szChildClass[MAX_LOADSTRING] =TEXT("XXX")",或"static TCHAR szChildClass[MAX_LOADSTRING]=TEXT("XXX")",不过赋值与否不影响程序结果

    主要问题是:它可以在vc2010上运行(当然,如果在vc6.0上运行完全正确),但只能显示主窗口,子窗口无法显示,主消息程序也无法响应鼠标的点击动作

    我非常期待能得到您的帮助,再次感谢版主!

    2011年5月17日 3:54
  • 这是原代码(<<Windows程序设计>>,鼠标 CHECKER3),不过我想它是在vc6.0上编写的,但我期待作出正确的改进后能在vc2010上正确运行:

    CHECKER3.C
           
    /*---------------------------------------------------------------------------
           
      CHECKER3.C --   Mouse Hit-Test Demo Program No. 3
           
                                                     (c) Charles Petzold, 1998
           
    ----------------------------------------------------------------------------*/
           
    #include <windows.h>
           

    #define DIVISIONS 5
           

    LRESULT CALLBACK WndProc   (HWND, UINT, WPARAM, LPARAM) ;
           
    LRESULT CALLBACK ChildWndProc (HWND, UINT, WPARAM, LPARAM) ;
           
    TCHAR szChildClass[]                                      = TEXT ("Checker3_Child") ;
           
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
           
                                                             PSTR szCmdLine, int iCmdShow)
           
    {
           
               static TCHAR  szAppName[]   = TEXT ("Checker3") ;
           
               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 ;
           
               }
           
      
           
        wndclass.lpfnWndProc                         = ChildWndProc ;
           
               wndclass.cbWndExtra                  = sizeof (long) ;
           
               wndclass.hIcon                       = NULL ;
           
               wndclass.lpszClassName               = szChildClass ;
           

             RegisterClass (&wndclass) ;
           
               hwnd = CreateWindow (szAppName, TEXT ("Checker3 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 HWND   hwndChild[DIVISIONS][DIVISIONS] ;
           
               int                           cxBlock, cyBlock, x, y ;
           
      
           
               switch (message)
           
        {
           
               case   WM_CREATE :
           
                     for (x = 0 ; x < DIVISIONS ; x++)
           
                              for (y = 0 ; y < DIVISIONS ; y++)
           
                       hwndChild[x][y] = CreateWindow (szChildClass, NULL,
           
                       WS_CHILDWINDOW | WS_VISIBLE,
           
                       0, 0, 0, 0,
           
                      hwnd, (HMENU) (y << 8 | x),
           
                       (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
           
                                                                                       NULL) ;
           
                      return 0 ;
           
                
           
               case   WM_SIZE :
           
                      cxBlock = LOWORD (lParam) / DIVISIONS ;
           
                      cyBlock = HIWORD (lParam) / DIVISIONS ;
           
                      for (x = 0 ; x < DIVISIONS ; x++)
           
                                            for (y = 0 ; y < DIVISIONS ; y++)
           
                                                             MoveWindow (  hwndChild[x][y],
           
                                                     x * cxBlock, y * cyBlock,
           
                                                       cxBlock, cyBlock, TRUE) ;
           
                      return 0 ;
           

               case   WM_LBUTTONDOWN :
           
                      MessageBeep (0) ;
           
                      return 0 ;
           
           
           
               case   WM_DESTROY :
           
                      PostQuitMessage (0) ;
           
                      return 0 ;
           
               }
           
               return DefWindowProc (hwnd, message, wParam, lParam) ;
           
    }
           

    LRESULT CALLBACK ChildWndProc (HWND hwnd, UINT message,
           
                   WPARAM wParam, LPARAM lParam)
           
    {
           
               HDC                                          hdc ;
           
               PAINTSTRUCT                          ps ;
           
               RECT                                         rect ;
           
      
           
               switch (message)
           
              {
           
               case   WM_CREATE :
           
                      SetWindowLong (hwnd, 0, 0) ;       // on/off flag
           
                      return 0 ;
           
           
           
               case   WM_LBUTTONDOWN :
           
                      SetWindowLong (hwnd, 0, 1 ^ GetWindowLong (hwnd, 0)) ;
           
                      InvalidateRect (hwnd, NULL, FALSE) ;
           
                    return 0 ;
           
          
           
               case   WM_PAINT :
           
                      hdc = BeginPaint (hwnd, &ps) ;
           
           
           
                      GetClientRect (hwnd, &rect) ;
           
                      Rectangle (hdc, 0, 0, rect.right, rect.bottom) ;
           
           
           
                      if (GetWindowLong (hwnd, 0))
           
                      {
           
                                            MoveToEx (hdc, 0,          0, NULL) ;
           
                                             LineTo   (hdc, rect.right, rect.bottom) ;
           
                                             MoveToEx (hdc, 0,          rect.bottom, NULL) ;
           
                                             LineTo   (hdc, rect.right, 0) ;
           
                      }
           
           
           
                    EndPaint (hwnd, &ps) ;
           
                      return 0 ;
           
               }
           
               return DefWindowProc (hwnd, message, wParam, lParam) ;
           
    }

    2011年5月17日 3:58
  • 您好,非常感谢您在百忙中看我的贴子

    其实 TCHAR szChildClass[MAX_LOADSTRING] 完全可以这样"TCHAR szChildClass[MAX_LOADSTRING] =TEXT("XXX")",或"static TCHAR szChildClass[MAX_LOADSTRING]=TEXT("XXX")",不过赋值与否不影响程序结果

    主要问题是:它可以在vc2010上运行(当然,如果在vc6.0上运行完全正确),但只能显示主窗口,子窗口无法显示,主消息程序也无法响应鼠标的点击动作

    我非常期待能得到您的帮助,再次感谢版主!


    (VS2008平台测试您上面的代码)将szChildClass赋初始值以后,程序启动后显示网格,在网格中点击出现出现交叉的矩形对角线,再次点击,对角线消息。不知道这个是不是您要的效果?
    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
    2011年5月17日 5:04
    版主
  • 啊,是的,这正是我要的结果,不知vc2010行不行,我去试试,谢谢您
    2011年5月17日 5:24
  • 请问可以将代码发给我吗?
    2011年5月17日 5:27
  • 版主,问题解决了啊啊啊,三个星期了,原来问题出在这里,正如您所说的 TCHAR szChildClass[MAX_LOADSTRING]=TEXT("MMM") 我以前也这样赋值的,可不知怎么回事啊,好好,感谢版主,感谢MSDN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    2011年5月17日 5:58