菜单怎么阻止了消息的接收?(100分)

  • 主题发起人 主题发起人 newboy123
  • 开始时间 开始时间
N

newboy123

Unregistered / Unconfirmed
GUEST, unregistred user!
发现菜单弹出后会阻断消息,<br>下面是个试验程序.<br><br>新建程序,在主窗口放 2个label 1个timer 1个popupmenu, popupmenu设为主窗口右键菜单.<br>菜单弹出后,label2就停了<br>哪位帮忙看看, 谢谢!<br><br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, ExtCtrls, StdCtrls, Menus;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; Label2: TLabel;<br>&nbsp; &nbsp; Timer1: TTimer;<br>&nbsp; &nbsp; PopupMenu1: TPopupMenu;<br>&nbsp; &nbsp; N11111: TMenuItem;<br>&nbsp; &nbsp; N22221: TMenuItem;<br>&nbsp; &nbsp; procedure Timer1Timer(Sender: TObject);<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; &nbsp; procedure AppMsg(var msg:TMsg;var hdl:Boolean);<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; cnt1:Integer=0;<br>&nbsp; cnt2:Integer=0;<br>&nbsp; msgc:UInt=WM_USER+$1000;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.AppMsg(var msg:TMsg;var hdl:Boolean);<br>begin<br>&nbsp; if msg.message=msgc then<br>&nbsp; begin<br>&nbsp; &nbsp; Inc(cnt2);<br>&nbsp; &nbsp; Label2.Caption := IntToStr(cnt2);<br>&nbsp; &nbsp; hdl := True;<br>&nbsp; end;<br>&nbsp; <br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp; Application.OnMessage := AppMsg;<br>end;<br><br>procedure TForm1.Timer1Timer(Sender: TObject);<br>begin<br>&nbsp; Inc(cnt1);<br>&nbsp; Label1.Caption := IntToStr(cnt1);<br>&nbsp; PostMessage(Application.Handle,msgc,0,0);<br>end;<br><br>end.
 
AppMsg?<br><br>WndProc吧,还要override.<br><br>唉
 
AppMsg是事件处理过程. 再说,不弹出菜单时运行正常.
 
用自定义消息解决,没搞明白原因.<br><br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls, ExtCtrls, Menus;<br><br>const<br>&nbsp; WM_MYMESSAGE=WM_USER+$1000;<br><br>type<br>&nbsp; TWMMyMessage=record<br>&nbsp; &nbsp; Msg: &nbsp; Cardinal;<br>&nbsp; &nbsp; Name: &nbsp;DWORD;<br>&nbsp; &nbsp; Time: &nbsp;DWORD;<br>&nbsp; &nbsp; Result:Longint;<br>&nbsp; end;<br><br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Timer1: TTimer;<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; Label2: TLabel;<br>&nbsp; &nbsp; PopupMenu1: TPopupMenu;<br>&nbsp; &nbsp; N1111: TMenuItem;<br>&nbsp; &nbsp; procedure Timer1Timer(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; &nbsp; procedure MyMsgdo(var msg:TWMMyMessage); message WM_MYMESSAGE;<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; cnt1:Integer=0;<br>&nbsp; cnt2:Integer=0;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Timer1Timer(Sender: TObject);<br>begin<br>&nbsp; Inc(cnt1);<br>&nbsp; Label1.Caption := IntToStr(cnt1);<br>&nbsp; PostMessage(Application.MainForm.Handle,WM_MYMESSAGE,0,0);<br>end;<br><br>procedure TForm1.MyMsgdo(var msg:TWMMyMessage);<br>begin<br>&nbsp; Inc(cnt2);<br>&nbsp; Label2.Caption := IntToStr(cnt1);<br>end;<br><br><br>end.
 
如果阻止了消息接收你整个窗口都会死掉。
 
后退
顶部