读书人

为啥Windows程序设计显示不了菜单?

发布时间: 2012-08-21 13:00:21 作者: rapoo

为什么Windows程序设计显示不了菜单??
这两天在VC++6.0 和VS2008调试程序,有关菜单的,为什么会一直显示不了菜单的,步骤应该没错。
首先写了一个cpp文件,然后新建创建资源,然后在里面添加菜单,接着编辑菜单内容和ID,然后重命名,保存,接着编译通过了,但是一直是不显示菜单的。what't the fucking!
下面是源码:
MyMenu.cpp

C/C++ code
   #include<windows.h>#include"resource.h"LRESULT CALLBACK WindowProc(  HWND hwnd,      // handle to window  UINT uMsg,      // message identifier  WPARAM wParam,  // first message parameter  LPARAM lParam   // second message parameter);int WINAPI WinMain(  HINSTANCE hInstance,      // handle to current instance  HINSTANCE hPrevInstance,  // handle to previous instance  LPSTR lpCmdLine,          // command line  int nCmdShow              // show state){    static TCHAR szAppName[]=TEXT("MyMenu");    HWND hwnd;    MSG msg;    WNDCLASS wndclass;    wndclass.cbClsExtra=0;    wndclass.cbWndExtra=0;    wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);    wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);    wndclass.hIcon=LoadIcon(NULL,IDI_ERROR);    wndclass.hInstance=hInstance;    wndclass.lpfnWndProc=WindowProc;    wndclass.lpszClassName=szAppName;    wndclass.lpszMenuName=szAppName;    wndclass.style=CS_HREDRAW|CS_VREDRAW;    if(!RegisterClass(&wndclass))    {        MessageBox(NULL,TEXT("the program require the window nt"),TEXT("tips"),MB_ICONERROR);        return 0;    }    hwnd=CreateWindow(      szAppName,  // registered class name      TEXT("this is title"), // window name      WS_OVERLAPPEDWINDOW,        // window style      CW_USEDEFAULT,                // horizontal position of window      CW_USEDEFAULT,                // vertical position of window      CW_USEDEFAULT,           // window width      CW_USEDEFAULT,          // window height      NULL,      // handle to parent or owner window      NULL, // menu handle or child identifier     hInstance,  // handle to application instance        NULL      // window-creation data);    ShowWindow(hwnd,nCmdShow);    UpdateWindow(hwnd);    while(GetMessage(&msg,NULL,0,0))    {        TranslateMessage(&msg);        DispatchMessage(&msg);    }    return msg.wParam;}LRESULT CALLBACK WindowProc(  HWND hwnd,      // handle to window  UINT uMsg,      // message identifier  WPARAM wParam,  // first message parameter  LPARAM lParam   // second message parameter){    switch(uMsg)    {    case WM_CREATE:        return 0;    case WM_PAINT:        return 0;    case WM_DESTROY:        PostQuitMessage(0);        return 0;    }    return DefWindowProc(hwnd,uMsg,wParam,lParam);}





[解决办法]
在createwindow后加
loadmenu()
Setmenu()
有参数的自己去弄
[解决办法]
LZ..。hMenu = LoadMenu (hInstance, MAKEINTRESOURCE (ID_MENU)) ;//ID_MENU是菜单ID
在CreateWindow的到底第三个参数那里填上hMenu..就可以了
还有你的WM_PAINT消息的格式错了。。。

hdc = BeginPaint (hwnd, &ps) ;

EndPaint (hwnd, &ps) ;

[解决办法]
首先,我想说static TCHAR是没有意义的,字符串变量只是一个指针,真正地字符串一定是全局变量,也就是静态变量。

createwindow函数实际上屏蔽了窗口类的一些设置,如果窗口类与createwindow冲突以createwindow为准,在createwindow中传递-1表示使用窗口类中的设置

读书人网 >C++

热点推荐