使用API函数怎么添加菜单
使用API函数如何添加菜单
今天在学习资源相关知识,按照书上的例子编写,一直调试不过,可能对菜单的创建过程还是不太明了。
下面我提供一段简单的窗口代码,希望有人帮我添加上创建菜单的代码,先谢谢了。菜单很简单,类似于标准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;
}
------解决方案--------------------
今天在学习资源相关知识,按照书上的例子编写,一直调试不过,可能对菜单的创建过程还是不太明了。
下面我提供一段简单的窗口代码,希望有人帮我添加上创建菜单的代码,先谢谢了。菜单很简单,类似于标准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;
}
------解决方案--------------------
- 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 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; }