提示:
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 301
MyMenu1 MENUEX DISCARDABLE
BEGIN
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
END
END
本例代码文件:
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.