弹出菜单的处理技巧?(50分)

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

MaxWoods

Unregistered / Unconfirmed
GUEST, unregistred user!
我将程序最小化成系统图标,右击图标弹出菜单。奇怪的是如果窗口最小化,弹出菜单后如<br>果不选菜单项的话,再点击桌面空白处,菜单不会自已消失,但是如果窗口没有最小化就是<br>正常的,请问这是为什么?<br>可参你们的系统图标区,比如金山词霸,音量控制什么的,在没有可视窗口的情况下也不会<br>出现上述的弹出菜单问题!
 
unit TrayIcon;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls, Menus,shellapi;<br><br>type<br>&nbsp; //----------------------------------------------------------------------<br>&nbsp; PNotifyIconData = ^TNotifyIconDataA;<br>&nbsp; &nbsp; TNotifyIconDataA = record<br>&nbsp; &nbsp; cbSize : DWORD;<br>&nbsp; &nbsp; Wnd : HWND;<br>&nbsp; &nbsp; uID : UINT;<br>&nbsp; &nbsp; uFlags : UINT;<br>&nbsp; &nbsp; uCallbackMessage : UINT;<br>&nbsp; &nbsp; hIcon : HICON;<br>&nbsp; &nbsp; szTip : array [0..63] of AnsiChar;<br>&nbsp; end;<br>&nbsp; //----------------------------------------------------------------------<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; PopupMenu1: TPopupMenu;<br>&nbsp; &nbsp; open1: TMenuItem;<br>&nbsp; &nbsp; close1: TMenuItem;<br>&nbsp; &nbsp; N1: TMenuItem;<br>&nbsp; &nbsp; about1: TMenuItem;<br>&nbsp; &nbsp; procedure FormClose(Sender: TObject; var Action: TCloseAction);<br>&nbsp; &nbsp; procedure open1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure close1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure FormShow(Sender: TObject);<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; &nbsp; //-------------------------------------------------------------<br>&nbsp; &nbsp; IconData: TNotifyIconData;<br>&nbsp; &nbsp; procedure ShowIcon;<br>&nbsp; &nbsp; procedure IconOnClick(var message:TMessage); message WM_USER+1;<br>&nbsp; &nbsp; Procedure WMSysCommand(Var message : TMessage) ; Message WM_SYSCOMMAND ;<br>&nbsp; &nbsp; //-------------------------------------------------------------<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.IconOnClick( var message: Tmessage);<br>var p : TPoint;<br>begin<br>&nbsp;if (message.lParam = WM_LBUTTONDOWN) then<br>&nbsp;begin<br>&nbsp; &nbsp;ShowWindow(Handle, SW_SHOW );<br>&nbsp;end;<br>&nbsp;<br>&nbsp;if (message.lParam = WM_RBUTTONDOWN) then<br>&nbsp;begin<br>&nbsp; &nbsp;GetCursorPos(p);<br>&nbsp; &nbsp;SetForegroundWindow (Application.Handle); &nbsp;//&lt;---------------------------------------注意这两句。<br>&nbsp; &nbsp;Application.ProcessMessages;//&lt;------------------------------------------------------<br>&nbsp; &nbsp;popupmenu1.Popup( p.x ,p.y );<br>&nbsp;end;<br>end;<br><br>Procedure TForm1.WMSysCommand(Var Message : TMessage) ;<br>begin<br><br>&nbsp; if (Message.WParam = SC_MINIMIZE) then<br>&nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp;ShowIcon;<br>&nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; &nbsp;Inherited;<br><br>end;<br><br>procedure TForm1.ShowIcon;<br>begin<br>&nbsp; &nbsp; &nbsp; IconData.cbSize := SizeOf( IconData );<br>&nbsp; &nbsp; &nbsp; IconData.Wnd := Handle;<br>&nbsp; &nbsp; &nbsp; IconData.uID := 1;<br>&nbsp; &nbsp; &nbsp; IconData.uFlags := NIF_ICON &nbsp;or NIF_MESSAGE or NIF_TIP;<br>&nbsp; &nbsp; &nbsp; IconData.uCallBackMessage := WM_USER+1;<br>&nbsp; &nbsp; &nbsp; IconData.hIcon := application.Icon.Handle;<br>&nbsp; &nbsp; &nbsp; IconData.szTip := 'LANChat';<br>&nbsp; &nbsp; &nbsp; Shell_NotifyIcon( NIM_ADD, @IconData );<br>&nbsp; &nbsp; &nbsp; ShowWindow(Handle, SW_HIDE);<br>&nbsp; &nbsp; &nbsp; hide;<br>end;<br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br>&nbsp; &nbsp;Shell_NotifyIcon( NIM_DELETE, @IconData );<br>end;<br><br>procedure TForm1.open1Click(Sender: TObject);<br>begin<br>&nbsp; Form1.Show;<br>end;<br><br>procedure TForm1.close1Click(Sender: TObject);<br>begin<br>&nbsp; Form1.close;<br>end;<br><br>procedure TForm1.FormShow(Sender: TObject);<br>begin<br>&nbsp; <br>&nbsp; showwindow(application.handle,sw_hide);<br>&nbsp; <br><br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp; showicon;<br>end;<br><br>end.
 
楼上的方法可能无效。试试这个:<br>&nbsp; &nbsp; &nbsp; SetForegroundWindow(Application.Handle);<br>&nbsp; &nbsp; &nbsp; Application.ProcessMessages;<br>&nbsp; &nbsp; &nbsp; PopupMenu1.Popup(Mouse.CursorPos.X, Mouse.CursorPos.Y);<br>&nbsp; &nbsp; &nbsp; PostMessage(Self.Handle, WM_NULL, 0, 0) //注意比楼上多了这行<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;~~~~~~这个self指拥有popupmenu1的form
 
顶部