首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

WIN32程序设计的有关问题,大家近来看看

2012-01-13 
WIN32程序设计的问题,大家近来看看我刚学WIN32程序设计不久,以前书上的程序例子都是一个窗口,我今天突发奇

WIN32程序设计的问题,大家近来看看
我刚学WIN32程序设计不久,以前书上的程序例子都是一个窗口,我今天突发奇想写了个两

个窗口的程序,没想到这个程序把我弄糊涂了.

我先声名了两个窗口过程函数,又声名了2个句柄和窗口类,在2个窗口类分别定义了不同

的窗口过程函数.编译,连接成功,在第一个窗口里按鼠标时弹出MSGBOX显示The   first  

window,关闭第一个窗口创建第二个窗口后,按鼠标也弹出MSGBOX显示The   first   window,

而且在WndProc2中处理的F12也不管用,这是怎么回事?

代码如下:
/*------------------------
      HELLOWIN.C   --   Displays   "DOUBLE   WINDOWS "   in   client   area
                                  (c)   Charles   Petzold,   1998
    ------------------------*/

#include   <windows.h>

LRESULT   CALLBACK   WndProc   (HWND,   UINT,   WPARAM,   LPARAM)   ;
LRESULT   CALLBACK   WndProc2   (HWND,   UINT,   WPARAM,   LPARAM)   ;
int   WINAPI   WinMain   (HINSTANCE   hInstance,   HINSTANCE   hPrevInstance,
                                        PSTR   szCmdLine,   int   iCmdShow)
{
          static   TCHAR   szAppName[]   =   TEXT   ( "HelloWin ")   ;
          HWND                   hwnd   ,hwnd2;  
  MSG                     msg   ;
          WNDCLASS           wndclass   ,   wndclass2   ;
         
          wndclass.style                   =   CS_HREDRAW   |   CS_VREDRAW   ;
          wndclass.lpfnWndProc       =   WndProc   ;
          wndclass.cbClsExtra         =   0   ;
          wndclass.cbWndExtra         =   0   ;
          wndclass.hInstance           =   hInstance   ;
          wndclass.hIcon                   =   LoadIcon   (hInstance,   "CDMENU ")   ;
          wndclass.hCursor               =   LoadCursor   (NULL,   IDC_ARROW)   ;
          wndclass.hbrBackground   =   (HBRUSH)   GetStockObject   (WHITE_BRUSH)   ;
          wndclass.lpszMenuName     =   NULL   ;
          wndclass.lpszClassName   =   szAppName   ;
         
  wndclass2.style                   =   CS_HREDRAW   |   CS_VREDRAW   ;
          wndclass2.lpfnWndProc       =   WndProc2   ;
          wndclass2.cbClsExtra         =   0   ;
          wndclass2.cbWndExtra         =   0   ;
          wndclass2.hInstance           =   hInstance   ;


          wndclass2.hIcon                   =   LoadIcon   (hInstance,   "CDMENU ")   ;
          wndclass2.hCursor               =   LoadCursor   (NULL,   IDC_ARROW)   ;
          wndclass2.hbrBackground   =   (HBRUSH)   GetStockObject   (WHITE_BRUSH)   ;
          wndclass2.lpszMenuName     =   NULL   ;
          wndclass2.lpszClassName   =   szAppName   ;
          if   (!RegisterClass   (&wndclass))
          {
                    MessageBox   (NULL,   TEXT   ( "This   program   requires   Windows   NT! "),  
                                            szAppName,   MB_ICONERROR)   ;
                    return   0   ;
          }
          RegisterClass   (&wndclass2);

  hwnd   =   CreateWindow   (szAppName,                                     //   window   class   name
                                                    TEXT   ( "The   Hello   Program "),   //   window   caption
                                                    WS_OVERLAPPEDWINDOW,                           //   window   style
                                                    CW_USEDEFAULT,                             //   initial   x   position
                                                    CW_USEDEFAULT,                             //   initial   y   position
                                                    CW_USEDEFAULT,                             //   initial   x   size
                                                    CW_USEDEFAULT,                             //   initial   y   size
                                                    NULL,                                               //   parent   window   handle


                                                    NULL,                                               //   window   menu   handle
                                                    hInstance,                                     //   program   instance   handle
                                                    NULL)   ;                                           //   creation   parameters
         
          ShowWindow   (hwnd,   iCmdShow)   ;
          UpdateWindow   (hwnd)   ;
         
          while   (GetMessage   (&msg,   NULL,   0,   0))
          {
                    TranslateMessage   (&msg)   ;
                    DispatchMessage   (&msg)   ;
          }
         
  hwnd2   =   CreateWindow   (szAppName,                                     //   window   class   name
                                                    TEXT   ( "The   Hello   Program2 "),   //   window   caption
                                                    WS_OVERLAPPEDWINDOW,                           //   window   style
                                                    CW_USEDEFAULT,                             //   initial   x   position
                                                    CW_USEDEFAULT,                             //   initial   y   position
                                                    CW_USEDEFAULT,                             //   initial   x   size


                                                    CW_USEDEFAULT,                             //   initial   y   size
                                                    NULL,                                               //   parent   window   handle
                                                    NULL,                                               //   window   menu   handle
                                                    hInstance,                                     //   program   instance   handle
                                                    NULL)   ;                                           //   creation   parameters
         
          ShowWindow   (hwnd2,   iCmdShow)   ;
          UpdateWindow   (hwnd2)   ;
         
          while   (GetMessage   (&msg,   NULL,   0,   0))
          {
                    TranslateMessage   (&msg)   ;
                    DispatchMessage   (&msg)   ;
          }
 
  return   msg.wParam   ;
}

LRESULT   CALLBACK   WndProc   (HWND   hwnd,   UINT   message,   WPARAM   wParam,   LPARAM   lParam)
{
   
          PAINTSTRUCT   ps   ;
         

  switch   (message)
          {

  case   WM_LBUTTONDOWN:
    MessageBox   (NULL,   TEXT   ( "The   first   window. "),  
                                            TEXT( "The   first   window. "),   MB_ICONERROR)   ;
                    return   0   ;
  case   WM_PAINT:
             
                    BeginPaint   (hwnd,   &ps);



    EndPaint   (hwnd,   &ps)   ;
                    return   0;
          case   WM_DESTROY:
                    PostQuitMessage   (0)   ;
                    return   0   ;
          }
          return   DefWindowProc   (hwnd,   message,   wParam,   lParam)   ;
}

LRESULT   CALLBACK   WndProc2   (HWND   hwnd,   UINT   message,   WPARAM   wParam,   LPARAM   lParam)
{
     
          PAINTSTRUCT   ps   ;
       
         
  switch   (message)
          {

          case   WM_KEYUP:
  switch(wParam)
  {
  case   VK_F12:
                      ShowWindow   (hwnd,   SW_SHOWMAXIMIZED)   ;}
                  return   0;          
          case   WM_LBUTTONDOWN:
    MessageBox   (NULL,   TEXT   ( "The   second   window. "),  
                                            TEXT   ( "The   second "),   MB_ICONERROR)   ;
                    return   0   ;
  case   WM_PAINT:
               
                    BeginPaint   (hwnd,   &ps);
   
    EndPaint   (hwnd,   &ps)   ;
                    return   0;
          case   WM_DESTROY:
                    PostQuitMessage   (0)   ;
                    return   0   ;
          }
          return   DefWindowProc   (hwnd,   message,   wParam,   lParam)   ;
}

[解决办法]
2次创建用的是个同个classname
[解决办法]
两个类名相同,第二个窗口在注册时一定会出错
[解决办法]
HWND CreateWindow(
LPCTSTR lpClassName,
LPCTSTR lpWindowName,
DWORD dwStyle,
int x,
int y,
int nWidth,
int nHeight,
HWND hWndParent,
HMENU hMenu,
HINSTANCE hInstance,
LPVOID lpParam
);
Parameters

lpClassName
[in] Pointer to a null-terminated string or a class atom created by a previous call to the RegisterClass or RegisterClassEx function. The atom must be in the low-order word of lpClassName; the high-order word must be zero. If lpClassName is a string, it specifies the window class name. The class name can be any name registered with RegisterClass or RegisterClassEx, provided that the module that registers the class is also the module that creates the window. The class name can also be any of the predefined system class names. For a list of system class names, see the Remarks section.


热点排行