熟悉菜单消息的请进(100分)

  • 主题发起人 主题发起人 Milpas
  • 开始时间 开始时间
M

Milpas

Unregistered / Unconfirmed
GUEST, unregistred user!
请问如果知道菜单已经关闭?<br>比如我调popup,将菜单弹出,在窗口别处,甚至其它窗口按下鼠标,这里菜单收回(关闭)<br>我应该拦什么消息来得到这个事件?
 
比较笨的方法,在表单设立一个boolean值,在菜单弹出时设为true,然后用SetCapture<br>给Form,然后当你单击表单时去判断是否为true,这样去触发你所要的事件,别忘了在<br>单击菜单时区别开来
 
WM_EXITMENULOOP 进入菜单消息循环<br>WM_EXITMENULOOP &nbsp;离开菜单消息循环
 
to 诸葛白痴<br>不太明白你的意思<br><br>to LiChaoHui<br>我原来也是这么想的,可是我在重载的WndProc里抓不到WM_EXITMENULOOP<br>真奇怪<br>我用的是PopupMenu,菜单弹出是用Popup这个方法
 
直接增加一个消息映射过程,就可以的,我就是这么做的<br>我做的是菜单关闭时,状态栏显示公司信息
 
能把你的代码给我看看吗?
 
please wait , i write one for u
 
研究一下下面的代码吧,那两个消息不能用来拦截弹出式菜单<br><br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls, Menus;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; MainMenu1: TMainMenu;<br>&nbsp; &nbsp; PopupMenu1: TPopupMenu;<br>&nbsp; &nbsp; File1: TMenuItem;<br>&nbsp; &nbsp; Exit1: TMenuItem;<br>&nbsp; &nbsp; N1: TMenuItem;<br>&nbsp; &nbsp; PrintSetup1: TMenuItem;<br>&nbsp; &nbsp; Print1: TMenuItem;<br>&nbsp; &nbsp; N2: TMenuItem;<br>&nbsp; &nbsp; SaveAs1: TMenuItem;<br>&nbsp; &nbsp; Save1: TMenuItem;<br>&nbsp; &nbsp; Open1: TMenuItem;<br>&nbsp; &nbsp; New1: TMenuItem;<br>&nbsp; &nbsp; Edit1: TMenuItem;<br>&nbsp; &nbsp; Object1: TMenuItem;<br>&nbsp; &nbsp; Links1: TMenuItem;<br>&nbsp; &nbsp; N3: TMenuItem;<br>&nbsp; &nbsp; GoTo1: TMenuItem;<br>&nbsp; &nbsp; Replace1: TMenuItem;<br>&nbsp; &nbsp; Find1: TMenuItem;<br>&nbsp; &nbsp; N4: TMenuItem;<br>&nbsp; &nbsp; PasteSpecial1: TMenuItem;<br>&nbsp; &nbsp; Paste1: TMenuItem;<br>&nbsp; &nbsp; Copy1: TMenuItem;<br>&nbsp; &nbsp; Cut1: TMenuItem;<br>&nbsp; &nbsp; N5: TMenuItem;<br>&nbsp; &nbsp; Repeatcommand1: TMenuItem;<br>&nbsp; &nbsp; Undo1: TMenuItem;<br>&nbsp; &nbsp; Window1: TMenuItem;<br>&nbsp; &nbsp; Show1: TMenuItem;<br>&nbsp; &nbsp; Hide1: TMenuItem;<br>&nbsp; &nbsp; N6: TMenuItem;<br>&nbsp; &nbsp; ArrangeAll1: TMenuItem;<br>&nbsp; &nbsp; Cascade1: TMenuItem;<br>&nbsp; &nbsp; ile1: TMenuItem;<br>&nbsp; &nbsp; NewWindow1: TMenuItem;<br>&nbsp; &nbsp; Help1: TMenuItem;<br>&nbsp; &nbsp; About1: TMenuItem;<br>&nbsp; &nbsp; HowtoUseHelp1: TMenuItem;<br>&nbsp; &nbsp; utorial1: TMenuItem;<br>&nbsp; &nbsp; SearchforHelpOn1: TMenuItem;<br>&nbsp; &nbsp; Keyboard1: TMenuItem;<br>&nbsp; &nbsp; Procedures1: TMenuItem;<br>&nbsp; &nbsp; Commands1: TMenuItem;<br>&nbsp; &nbsp; Index1: TMenuItem;<br>&nbsp; &nbsp; Contents1: TMenuItem;<br>&nbsp; &nbsp; Undo2: TMenuItem;<br>&nbsp; &nbsp; Repeatcommand2: TMenuItem;<br>&nbsp; &nbsp; N7: TMenuItem;<br>&nbsp; &nbsp; Cut2: TMenuItem;<br>&nbsp; &nbsp; Copy2: TMenuItem;<br>&nbsp; &nbsp; Paste2: TMenuItem;<br>&nbsp; &nbsp; PasteSpecial2: TMenuItem;<br>&nbsp; &nbsp; N8: TMenuItem;<br>&nbsp; &nbsp; Find2: TMenuItem;<br>&nbsp; &nbsp; Replace2: TMenuItem;<br>&nbsp; &nbsp; GoTo2: TMenuItem;<br>&nbsp; &nbsp; N9: TMenuItem;<br>&nbsp; &nbsp; Links2: TMenuItem;<br>&nbsp; &nbsp; Object2: TMenuItem;<br>&nbsp; &nbsp; Memo1: TMemo;<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure FormContextPopup(Sender: TObject; MousePos: TPoint;<br>&nbsp; &nbsp; &nbsp; var Handled: Boolean);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; procedure EnterMenuLoop(var Message: TWmEnterMenuLoop); message WM_ENTERMENULOOP;<br>&nbsp; &nbsp; procedure ExitMenuLoop(var Message: TWmExitMenuLoop); message WM_EXITMENULOOP;<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br><br>{ TForm1 }<br><br>procedure TForm1.EnterMenuLoop(var Message: TWmEnterMenuLoop);<br>begin<br>&nbsp; Label1.Caption := 'InMenuLoop';<br>&nbsp; Memo1.Lines.Add('EnterMenuLoop')<br>end;<br><br>procedure TForm1.ExitMenuLoop(var Message: TWmExitMenuLoop);<br>begin<br>&nbsp; Label1.Caption := 'no menu Active';<br>&nbsp; Memo1.Lines.Add('LeaveMenuLoop')<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp; Memo1.Lines.Clear;<br>end;<br><br>procedure TForm1.FormContextPopup(Sender: TObject; MousePos: TPoint;<br>&nbsp; var Handled: Boolean);<br>begin<br>&nbsp; Handled := True;<br>&nbsp; Memo1.Lines.Add('Popup a menu');<br>&nbsp; PopupMenu1.Popup(MousePos.X, MousePos.Y);<br>&nbsp; Memo1.Lines.Add('Popup menu close');<br><br>end;<br><br>end.<br>
 
你好,谢谢你的代码。<br>我试了,但是发现弹出式菜单还是无效,主菜单是可以的。<br><br>我测试的代码如下:<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, Menus, StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; PopupMenu1: TPopupMenu;<br>&nbsp; &nbsp; fsafdsafds1: TMenuItem;<br>&nbsp; &nbsp; safdsa1: TMenuItem;<br>&nbsp; &nbsp; fsda1: TMenuItem;<br>&nbsp; &nbsp; fdas1: TMenuItem;<br>&nbsp; &nbsp; f1: TMenuItem;<br>&nbsp; &nbsp; dsa1: TMenuItem;<br>&nbsp; &nbsp; f2: TMenuItem;<br>&nbsp; &nbsp; Memo1: TMemo;<br>&nbsp; &nbsp; MainMenu1: TMainMenu;<br>&nbsp; &nbsp; fdsafdsa1: TMenuItem;<br>&nbsp; &nbsp; fdsafd1: TMenuItem;<br>&nbsp; &nbsp; sdafdsaf1: TMenuItem;<br>&nbsp; &nbsp; sda1: TMenuItem;<br>&nbsp; &nbsp; fsd1: TMenuItem;<br>&nbsp; &nbsp; af1: TMenuItem;<br>&nbsp; &nbsp; dsa2: TMenuItem;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure FormContextPopup(Sender: TObject; MousePos: TPoint;<br>&nbsp; &nbsp; &nbsp; var Handled: Boolean);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; &nbsp; procedure EnterMenuLoop(var Message: TWmEnterMenuLoop); message WM_ENTERMENULOOP;<br>&nbsp; &nbsp; procedure ExitMenuLoop(var Message: TWmExitMenuLoop); message WM_EXITMENULOOP;<br>&nbsp; &nbsp; procedure InitMenuPopup(var Message: TWMInitMenuPopup); message WM_INITMENUPOPUP;<br>&nbsp; &nbsp; procedure UnInitMenuPopup(var Message: TWMInitMenuPopup); message WM_UNINITMENUPOPUP;<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; p1,<br>&nbsp; p2: TPoint;<br>begin<br>&nbsp; p1.X := Button1.Left;<br>&nbsp; p1.Y := Button1.Top + Button1.Height;<br>&nbsp; p2 := ClientToScreen(p1);<br>&nbsp; PopupMenu1.Popup(p2.X, p2.Y);<br>end;<br><br>procedure TForm1.EnterMenuLoop(var Message: TWmEnterMenuLoop);<br>begin<br>&nbsp; Memo1.Lines.Add('EnterMenuLoop')<br>end;<br><br>procedure TForm1.ExitMenuLoop(var Message: TWmExitMenuLoop);<br>begin<br>&nbsp; Memo1.Lines.Add('LeaveMenuLoop')<br>end;<br><br>procedure TForm1.FormContextPopup(Sender: TObject; MousePos: TPoint;<br>&nbsp; var Handled: Boolean);<br>begin<br>&nbsp; Handled := True;<br>&nbsp; Memo1.Lines.Add('Popup a menu');<br>&nbsp; PopupMenu1.Popup(MousePos.X, MousePos.Y);<br>&nbsp; Memo1.Lines.Add('Popup menu close');<br>end;<br><br>procedure TForm1.InitMenuPopup(var Message: TWMInitMenuPopup);<br>begin<br>&nbsp; Memo1.Lines.Add('1')<br>end;<br><br>procedure TForm1.UnInitMenuPopup(var Message: TWMInitMenuPopup);<br>begin<br>&nbsp; Memo1.Lines.Add('2')<br>end;<br><br>end.
 
如果只针对弹出式菜单,<br>在你的Popup函数后面加一行代码<br>菜单关闭时,就会执行你的代码的<br><br>弹出式菜单和模态窗口差不多
 
问题是在弹出式菜单里这4个消息都抓不到<br>&nbsp; &nbsp; procedure EnterMenuLoop(var Message: TWmEnterMenuLoop); message WM_ENTERMENULOOP;<br>&nbsp; &nbsp; procedure ExitMenuLoop(var Message: TWmExitMenuLoop); message WM_EXITMENULOOP;<br>&nbsp; &nbsp; procedure InitMenuPopup(var Message: TWMInitMenuPopup); message WM_INITMENUPOPUP;<br>&nbsp; &nbsp; procedure UnInitMenuPopup(var Message: TWMInitMenuPopup); message WM_UNINITMENUPOPUP;<br><br>奇怪。不知道是什么消息才可以
 
不行吧,我那个方法不明白吗? 就是在PopupMenu弹出时用SetCapture绑定鼠标消息,<br>然后这样你的任何单击不就会被认为是。。。
 
跟踪弹出式菜单,只能用我上面的方法了,<br>我的程序是可以捕捉到PopupMenu的弹出和关闭的<br>只不过要接手所有的弹出事件
 
不明白,来点实例吧,谢谢
 
TStringGrid怎麼使某一格顯示不同的顏色.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
579
import
I
I
回复
0
查看
642
import
I
I
回复
0
查看
364
import
I
后退
顶部