提示:
1、功能表項目的標識存放在 WM_MENUSELECT 訊息的 lParam 參數中的低兩位.
2、WM_MENUSELECT 也包括系統功能表發送的訊息.
3、因為頂層菜單(File Edit Help)一般在資源檔中設同樣的標識符(譬如: 65535), 所以系統會把它們的位置(或者叫順序)當作標識; 因此執行 GetMenuString 時, 需要使用用非預設(非0)的 MF_BYPOSITION 參數.
4、因為系統功能表的標識都大於 $F000, 所以要求我們自訂的菜單標識一般要小於這個數; 在程式中我們也是根據這個道理來區別菜單來源的.
本例:
本例使用的資源檔(TestRes.rc):
#define IDM_New 101#define IDM_Open 102#define IDM_Save 103#define IDM_Exit 104#define IDM_Cut 201#define IDM_Copy 202#define IDM_Paste 203#define IDM_About 301MyMenu1 MENUEX DISCARDABLEBEGIN POPUP "&File" ,65535 BEGIN MENUITEM "&New" ,IDM_New MENUITEM "&Open" ,IDM_Open MENUITEM "&Save" ,IDM_Save MENUITEM SEPARATOR MENUITEM "E&xit" ,IDM_Exit END POPUP "&Edit" ,65535 BEGIN MENUITEM "Cu&t" ,IDM_Cut MENUITEM "&Copy" ,IDM_Copy MENUITEM "&Paste" ,IDM_Paste END POPUP "&Help" ,65535 BEGIN MENUITEM "&About" ,IDM_About ENDEND
本例代碼檔案:
program Project1;{$R 'TestRes.res' 'TestRes.rc'}uses Windows, Messages;{收到 WM_MENUSELECT 訊息時需要做的工作}procedure OnMenuSelect(h: HWND; wParam: Integer);var buf: array[0..255] of Char; s: string; w: Word;begin w := LoWord(wParam); Str(w, s); case w of 0..99 : GetMenuString(GetMenu(h), w, buf, Length(buf), MF_BYPOSITION); 100..$F000 : GetMenuString(GetMenu(h), w, buf, Length(buf), MF_BYCOMMAND); $F001..$FFFF : GetMenuString(GetSystemMenu(h, False), w, buf, Length(buf), MF_BYCOMMAND); end; s := s + ' - ' + buf; SetWindowText(h, PChar(s));end;function WndProc(wnd: HWND; msg: UINT; wParam,lParam: Integer): Integer; stdcall;begin Result := 0; case msg of WM_MENUSELECT : OnMenuSelect(wnd, wParam); {收到 WM_MENUSELECT 訊息後調用 OnMenuSelect 過程} WM_DESTROY : PostQuitMessage(0); else Result := DefWindowProc(wnd, msg, wParam, lParam); end;end;function RegMyWndClass: Boolean;var cls: TWndClass;begin cls.style := CS_HREDRAW or CS_VREDRAW; cls.lpfnWndProc := @WndProc; cls.cbClsExtra := 0; cls.cbWndExtra := 0; cls.hInstance := HInstance; cls.hIcon := 0; cls.hCursor := LoadCursor(0, IDC_ARROW); cls.hbrBackground := HBRUSH(COLOR_WINDOW + 1); cls.lpszMenuName := 'MyMenu1'; cls.lpszClassName := 'MyWnd'; Result := RegisterClass(cls) 0;end;{程式入口}const tit = 'New Form'; ws = WS_OVERLAPPEDWINDOW; x = 100; y = 100; w = 300; h = 180;var hWnd: THandle; Msg : TMsg;begin RegMyWndClass; hWnd := CreateWindow('MyWnd', tit, ws, x, y, w, h, 0, 0, HInstance, nil); ShowWindow(hWnd, SW_SHOWNORMAL); UpdateWindow(hWnd); while(GetMessage(Msg, 0, 0, 0)) do begin TranslateMessage(Msg); DispatchMessage(Msg); end;end.