菜單可能是Windows程式提供的一緻的使用者界面中最重要的部分,而想程式中添加菜單是Windows程式設計中相對容易的布馮。當使用者選擇菜單單選時,Windows向你的程式發送了一個含有才菜單單選ID的WM_COMMAND消息
LOWORD(wParam)
控件ID
HIWORD(wParam)
通知碼
lParam
子視窗句柄
建立菜單有三種方法;
1用WNDCLASS定義
[cpp] view plaincopyprint?
wndclass.lpszMenuName=szAppName;//I don't konw what the fucking is?在VC++6.0和VS2008竟然不顯示
2使用LoadMenu擷取hMenu,然後在CreateWindow倒數第三個參數寫上hMenu
hMenu=LoadMenu(hInstance,TEXT("MyMenu"));
hwnd=CreateWindow(//倒數第三個參數hMenu)
3第三種方法和第二種方法一樣:
hMenu=LoadMenu(hInstance,MAKEINTRESOURCE(MENUID));
hwnd=CreateWindow(//倒數第三個參數hMenu);
具體步驟跟建立自己的ICON差不多;
1建立Windows32項目,編寫好自己的.cpp檔案
2在項目名稱那裡右鍵添加資源(.rc),然後resource.h頭檔案也會自己建立
3在Resource檔案夾右鍵,添加資源(Menu)
4然後在圖形界面裡面編輯選項和ID,重命名就行了
5運作
下面看一段簡單的程式:
#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("MENUDEMO");
HWND hwnd;
WNDCLASS wndclass;
MSG msg;
HMENU hMenu;
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=NULL;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("the program require the window nt"),TEXT("TIPS "),MB_ICONERROR);
return 0;
}
hMenu=LoadMenu(hInstance,MAKEINTRESOURCE(MENUDEMO));
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
hMenu, // menu handle or child identifier
hInstance, // handle to application instance
NULL // window-creation data
//SetMenu(hwnd,hMenu);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
TranslateMessage(&msg);
DispatchMessage(&msg);
return msg.wParam;
}
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
switch(uMsg)
case WM_CREATE:
case WM_PAINT:
GetClientRect(hwnd,&rect);
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_FILE_NEW:
MessageBox(NULL,TEXT("你選擇了new"),TEXT("提示"),MB_OK);
break;
case ID_FILE_EXIT:
MessageBeep(MB_ICONEXCLAMATION);
SendMessage(hwnd,WM_CLOSE,0,0);
case ID_FILE_OPEN:
MessageBox(NULL,TEXT("你選擇了Open"),TEXT("提示"),MB_OK);
case ID_FILE_SAVE:
MessageBox(NULL,TEXT("你選擇了Save"),TEXT("提示"),MB_OK);
case ID_FILE_SAVEAS:
MessageBox(NULL,TEXT("你選擇了Save_As"),TEXT("提示"),MB_OK);
}
case WM_DESTROY:
PostQuitMessage(0);
return DefWindowProc(hwnd,uMsg,wParam,lParam);
效果如下

還有一段:
/*-----------------------------------------
MENUDEMO.C -- Menu Demonstration
(c) Charles Petzold, 1998
-----------------------------------------*/
#include <windows.h>
#include "resource.h"
#define ID_TIMER 1
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
TCHAR szAppName[] = TEXT ("MenuDemo") ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = szAppName ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, TEXT ("Menu Demonstration"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
return msg.wParam ;
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
static int idColor [5] = { WHITE_BRUSH, LTGRAY_BRUSH, GRAY_BRUSH,
DKGRAY_BRUSH, BLACK_BRUSH } ;
static int iSelection = IDM_BKGND_WHITE ;
HMENU hMenu ;
switch (message)
case WM_COMMAND:
hMenu = GetMenu (hwnd) ;
switch (LOWORD (wParam))
{
case IDM_FILE_NEW:
case IDM_FILE_OPEN:
case IDM_FILE_SAVE:
case IDM_FILE_SAVE_AS:
MessageBeep (0) ;
return 0 ;
case IDM_APP_EXIT:
SendMessage (hwnd, WM_CLOSE, 0, 0) ;
case IDM_EDIT_UNDO:
case IDM_EDIT_CUT:
case IDM_EDIT_COPY:
case IDM_EDIT_PASTE:
case IDM_EDIT_CLEAR:
case IDM_BKGND_WHITE: // Note: Logic below
case IDM_BKGND_LTGRAY: // assumes that IDM_WHITE
case IDM_BKGND_GRAY: // through IDM_BLACK are
case IDM_BKGND_DKGRAY: // consecutive numbers in
case IDM_BKGND_BLACK: // the order shown here.
CheckMenuItem (hMenu, iSelection, MF_UNCHECKED) ;
iSelection = LOWORD (wParam) ;
CheckMenuItem (hMenu, iSelection, MF_CHECKED) ;
SetClassLong (hwnd, GCL_HBRBACKGROUND, (LONG)
GetStockObject
(idColor [LOWORD (wParam) - IDM_BKGND_WHITE])) ;
InvalidateRect (hwnd, NULL, TRUE) ;
case IDM_TIMER_START:
if (SetTimer (hwnd, ID_TIMER, 1000, NULL))
{
EnableMenuItem (hMenu, IDM_TIMER_START, MF_GRAYED) ;
EnableMenuItem (hMenu, IDM_TIMER_STOP, MF_ENABLED) ;
}
case IDM_TIMER_STOP:
KillTimer (hwnd, ID_TIMER) ;
EnableMenuItem (hMenu, IDM_TIMER_START, MF_ENABLED) ;
EnableMenuItem (hMenu, IDM_TIMER_STOP, MF_GRAYED) ;
case IDM_APP_HELP:
MessageBox (hwnd, TEXT ("Help not yet implemented!"),
szAppName, MB_ICONEXCLAMATION | MB_OK) ;
case IDM_APP_ABOUT:
MessageBox (hwnd, TEXT ("Menu Demonstration Program\n")
TEXT ("(c) Charles Petzold, 1998"),
szAppName, MB_ICONINFORMATION | MB_OK) ;
}
break ;
case WM_TIMER:
MessageBeep (0) ;
case WM_DESTROY:
PostQuitMessage (0) ;
return DefWindowProc (hwnd, message, wParam, lParam) ;
效果: