下面是o*o以前关于该问题所做的回答,如果还有不明白的地方,请参照下面两个贴子:
http://www.gislab.ecnu.edu.cn/delphibbs/DispQ.asp?LID=206834
http://www.delphibbs.com/delphibbs/DispQ.asp?LID=209455
**********************************************************
D5的作法与D4完全不同,简单多了。PopupList是全局变量呀。
先将Menus.pas的TrackPopupMenu那句改了,去掉Buttons[FTrackButton]
在FORM上加一PopupMenu1,随便填几个MenuItem。
private
DefMenuProc: TFarProc;
procedure MenuWndProc(var msg: Tmessage);
{ Private declarations }
procedure TForm1.FormCreate(Sender: TObject);
begin
DefMenuProc:=Pointer(GetWindowLong(PopupList.Window,GWL_WNDPROC));
SetWindowLong(PopupList.Window,GWL_WNDPROC,LongInt(MakeObjectInstance(MenuWndProc)));
end;
var Item:Integer;
procedure TForm1.MenuWndProc(var msg:Tmessage);
var pop:HMENU; p:TPoint; s:array[0..255]of Char;
begin
case msg.Msg of
WM_MENURBUTTONUP: begin
GetCursorPos(p);
pop:=CreatePopupMenu;
GetMenuString(PopupMenu1.Handle,Item,s,255,MF_BYCOMMAND);
AppendMenu(pop,0,100,PChar('Popup From '+String(s)));
TrackPopupMenu(pop,1,p.x,p.y,0,PopupList.Window,nil);
DestroyMenu(pop);
end;
WM_MENUSELECT: Item:=LOWORD(msg.wParam);
WM_COMMAND: if LOWORD(msg.wParam)=100 then MessageBox(Handle,'','OK!',0);
end;
with msg do Result:=CallWindowProc(DefMenuProc,PopupList.Window,Msg,WParam,LParam);
end;