使用API函数如何添加菜单
时间:2011-12-11
来源:互联网
下面我提供一段简单的窗口代码,希望有人帮我添加上创建菜单的代码,先谢谢了。菜单很简单,类似于标准windows程序的文件菜单。
结构为
文件
打开
保存
另存为
分割线
退出
#include <windows.h>
#include "resource.h"
const char clsname[]="mywindows";
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
BOOL InitWindowsClass(HINSTANCE hInstance);
BOOL InitWindows(HINSTANCE hInstance,int nShowCmd);
int WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nShowCmd )
{
MSG msg;
if(!InitWindowsClass(hInstance))
return FALSE;
if(!InitWindows(hInstance,nShowCmd))
return FALSE;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
BOOL InitWindowsClass(HINSTANCE hInstance)
{
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_HAND);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WndProc;
wndclass.lpszClassName=clsname;
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;
return RegisterClass(&wndclass);
}
BOOL InitWindows(HINSTANCE hInstance,int nShowCmd)
{
HWND hwnd;
HMENU hmenu;
hmenu=LoadMenu(hInstance,muname);
hwnd=CreateWindow(clsname,"Application",WS_OVERLAPPEDWINDOW,0,0,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
if(!hwnd)
return FALSE;
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"Exit the application?","message",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
作者: ivanwu1020 发布时间: 2011-12-11
#include "stdafx.h" #include <windows.h> #pragma comment(linker,"/subsystem:windows") #include "resource.h" const char clsname[]="mywindows"; LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam); BOOL InitWindowsClass(HINSTANCE hInstance); BOOL InitWindows(HINSTANCE hInstance,int nShowCmd); int WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nShowCmd ) { MSG msg; if(!InitWindowsClass(hInstance)) return FALSE; if(!InitWindows(hInstance,nShowCmd)) return FALSE; while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } BOOL InitWindowsClass(HINSTANCE hInstance) { WNDCLASS wndclass; wndclass.cbClsExtra=0; wndclass.cbWndExtra=0; wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.hCursor=LoadCursor(NULL,IDC_HAND); wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION); wndclass.hInstance=hInstance; wndclass.lpfnWndProc=WndProc; wndclass.lpszClassName=clsname; wndclass.lpszMenuName=NULL; wndclass.style=CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS; return RegisterClass(&wndclass); } BOOL InitWindows(HINSTANCE hInstance,int nShowCmd) { HWND hwnd; HMENU hmenu; hmenu=LoadMenu(hInstance,MAKEINTRESOURCE(IDR_MENU1)); hwnd=CreateWindow(clsname,"Application",WS_OVERLAPPEDWINDOW,0,0,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL); if(!hwnd) return FALSE; SetMenu(hwnd,hmenu); ShowWindow(hwnd,SW_SHOWNORMAL); UpdateWindow(hwnd); return TRUE; } LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam) { switch(msg) { case WM_CLOSE: if(IDYES==MessageBox(hwnd,"Exit the application?","message",MB_YESNO)) { DestroyWindow(hwnd); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd,msg,wParam,lParam); } return 0; }
其实加一句SetMenu就行了
作者: maoxing63570 发布时间: 2011-12-11
wndclass.lpszMenuName=NULL;
在resource View中添加Menu资源
作者: VisualEleven 发布时间: 2011-12-11
简单一点,把这里修改一下
wndclass.lpszMenuName=NULL;
在resource View中添加Menu资源
这个我是知道的,可是现在在学习API,所以想深入了解下,还是谢谢你。
作者: ivanwu1020 发布时间: 2011-12-11
C/C++ code
#include "stdafx.h"
#include <windows.h>
#pragma comment(linker,"/subsystem:windows")
#include "resource.h"
const char clsname[]="mywindows";
LRESULT CALLBACK WndProc(HWND hwn……
能写下这段代码么,
BOOL SetMenu(HWND hWnd,HMENU hMenu)
我知道这是给指定窗口设置菜单,那么hMenu这个菜单句柄通过什么函数获得或者创建?谢谢了
作者: ivanwu1020 发布时间: 2011-12-11
BOOL WINAPI SetMenu(
__in HWND hWnd,
__in_opt HMENU hMenu
);
函数作用为:Assigns a new menu to the specified window.
注意保证hMenu已正确加载。
作者: fight_in_dl 发布时间: 2011-12-11
C/C++ code
BOOL WINAPI SetMenu(
__in HWND hWnd,
__in_opt HMENU hMenu
);
函数作用为:Assigns a new menu to the specified window.
注意保证hMenu已正确加载。
前辈:
我想了解下 hMenu=LoadMenu(hinstance,"menuname")这个第二个参数菜单名是怎么和我要设计的菜单关联起来的,也就是说菜单的定义过程。
作者: ivanwu1020 发布时间: 2011-12-11
使用LoadMenu前需要在资源中定义好资源,比如资源ID是IDM_MAINMENU
就可以
LoadMenu(hinstance,MAKEINTRESOURCE(IDM_MAINMENU))
如果用API建立菜单需要用函数
CreateMenu
InsertMenuItem
SetMenu
作者: gameslq 发布时间: 2011-12-11
hMenu=LoadMenu(hinstance,"menuname") LoadMenu是从资源中加载菜单
使用LoadMenu前需要在资源中定义好资源,比如资源ID是IDM_MAINMENU
就可以
LoadMenu(hinstance,MAKEINTRESOURCE(IDM_MAINMENU))
如果用API建立菜单需要用函数
CreateMenu
InsertMenuItem……
我就是想用API函数手动的写菜单,能帮我写个样板么?
格式我在一楼写了,先谢谢了。
作者: ivanwu1020 发布时间: 2011-12-11
引用 7 楼 gameslq 的回复:
hMenu=LoadMenu(hinstance,"menuname") LoadMenu是从资源中加载菜单
使用LoadMenu前需要在资源中定义好资源,比如资源ID是IDM_MAINMENU
就可以
LoadMenu(hinstance,MAKEINTRESOURCE(IDM_MAINMENU))
如果用API建立菜单需要用函数
Creat……
1#不是把代码给你了吗?自己没有试一下吗?
作者: VisualEleven 发布时间: 2011-12-11
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28