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

Win32程序中怎么添加菜单

2012-10-26 
Win32程序中如何添加菜单我想问各位高手一个问题,现在我创建了一个win32 Application的程序并编写了主窗口

Win32程序中如何添加菜单
我想问各位高手一个问题,现在我创建了一个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; 


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

[解决办法]
HMENU hMenu; //菜单句柄 
这个是个局部变量,定义成全局变量试试。
[解决办法]
就代码方面,这么添加菜单应该没问题的。 lz添加的菜单有菜单项吗?没有菜单项的话,似乎不会显示~~
[解决办法]
hMenu=LoadMenu(hInstance,(LPCSTR)IDR_MENU1);



Check return value? is NULL?
[解决办法]

菜单的名字不是ID 直接和类名一样就可以了!!!

后面也不用再LoadMenu()了
 
[解决办法]

探讨


我现在用MessageBox输出了GetLastError的信息,发现现在问题是:

1812——指定的映像文件不包含资源区域

请问这句话是什么意思?应该怎么解决呢?谢谢!!!

热点排行