您的位置:首页 > 其它

Win32程序中如何添加菜单?

2009-08-05 11:24 281 查看
我想问各位高手一个问题,现在我创建了一个win32 Application的程序并编写了主窗口的代码,然后想在我显示的主窗口中添加一个菜单,怎么加也显示不了,是什么原因啊?已经困惑我好几天了。谢谢指教!!!
(我先用insert/resource/Menu/new创建了一个新的菜单,然后在主程序中添加代码)
#include <windows.h>
#include <string.h>
#include"Simpwin.h"
#include"resource.h" //菜单资源的头文件

#pragma comment( linker, "/subsystem:/"windows/" /entry:/"WinMainCRTStartup/"" )

HINSTANCE hInst;
HWND hWndMain;

int APIENTRY WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
if(!InitApplicaion(hInstance))
return (FALSE);

if(!InitInstance(hInstance,nCmdShow))
return FALSE;

while(GetMessage(
&msg,
NULL,
0,
0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return msg.wParam;
}

BOOL InitApplicaion(HINSTANCE hInstance)//当前实例句柄
{
WNDCLASS wcSimpwin;

wcSimpwin.style = CS_HREDRAW | CS_VREDRAW;
wcSimpwin.lpfnWndProc = (WNDPROC)MainWndProc;
wcSimpwin.cbClsExtra = 0;
wcSimpwin.cbWndExtra = 0;
wcSimpwin.hInstance = hInstance;
wcSimpwin.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcSimpwin.hCursor = LoadCursor(NULL, IDC_ARROW);
wcSimpwin.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wcSimpwin.lpszMenuName = "IDR_MENU1";
wcSimpwin.lpszClassName = "SimpwinWClass";

return RegisterClass(&wcSimpwin);

}

//保存实例句柄,并创建主窗口
BOOL InitInstance(
HINSTANCE hInstance,
int nCmdShow)
{
hInst = hInstance;
HMENU hMenu; //菜单句柄
hMenu=LoadMenu(hInstance,(LPCSTR)IDR_MENU1); //在resource.h中有:#define IDR_MENU1 101
hWndMain = CreateWindow(
"SimpwinWClass",
"我得窗口",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
hMenu,
hInstance,
NULL);
if(!hWndMain)
return FALSE;
ShowWindow(hWndMain, nCmdShow);
UpdateWindow(hWndMain);

return TRUE;
}

//处理主窗口消息
LRESULT CALLBACK MainWndProc(
HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;

switch(message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd,&ps);
TextOut(hdc, 20, 10, hello, lstrlen(hello));
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;

default:
return DefWindowProc(hWnd, message, wParam, lParam);
}

return 0;
}

红色的代码就是我为了显示菜单而添加的代码,为什么添加了这些代码还是无法显示我插入的菜单呢?是不是哪里漏了什么了啊?

我用的就是VC6.0么。
不过谢谢各位指点!我总算解决了,原来只要从Project/Add To Project/Files中把我的资源Script4.rc加载一下就能显示菜单了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐