关于window托盘小图标双击的问题(50分)

  • 主题发起人 主题发起人 willrain
  • 开始时间 开始时间
W

willrain

Unregistered / Unconfirmed
GUEST, unregistred user!
我的程序是放在windows右下角的小图标里,象QQ那样,双击后显示主界面,虽然能正常显示,但不能马上获得焦点,任务栏老是在那里闪,如何能双击小图标后将焦点自动转移到程序主界面上?
 
MainForm.SetFocus;
 
我也做了类似的东东,我是这么处理TrayIcon的双击事件的<br>procedure TfmMain.TrayMsg(var Sender: TMessage); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //处理托盘事件<br>var<br>&nbsp; MouseID: Integer; &nbsp; &nbsp; &nbsp; &nbsp; //保存鼠标事件<br>begin<br>&nbsp; MouseID := Sender.LParam;<br>&nbsp; case MouseID of<br>&nbsp; &nbsp; WM_LButtonDBLCLK: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //双击:恢复/隐藏<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if actMinimize.Enabled then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; actMinimize.Execute;<br>&nbsp; &nbsp; &nbsp; end else<br>&nbsp; &nbsp; &nbsp; &nbsp; actRestore.Execute;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; WM_RButtonDown: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //右击:菜单<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; PopupMenu.Popup(Mouse.CursorPos.X, Mouse.CursorPos.Y);<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br><br>procedure TfmMain.actMinimizeExecute(Sender: TObject); &nbsp; &nbsp; &nbsp; &nbsp; //最小化窗口:隐藏<br>begin<br>&nbsp; Application.Minimize;<br>&nbsp; actMinimize.Enabled := false;<br>&nbsp; actRestore.Enabled := true;<br>end;<br><br>procedure TfmMain.actRestoreExecute(Sender: TObject); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//恢复窗口<br>begin<br>&nbsp; ShowWindow(Application.Handle, SW_Show);<br>&nbsp; Application.Restore;<br>&nbsp; actMinimize.Enabled := true;<br>&nbsp; actRestore.Enabled := false;<br>end;<br>
 
请问怎样将程序图标放入window托盘?
 
用其它控件吧,很方便的.我用的的Raize component3.0,里面有RzTrayIcon
 
SetforeGroundWindow(handle);
 
procedure TfrmMain.OnIconNotify(var Message: TMessage);<br>begin<br>&nbsp; &nbsp; case Message.LParam of<br>&nbsp; &nbsp; &nbsp; &nbsp; WM_LBUTTONDBLCLK:<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ShowWindow(GetWindow(Handle,GW_OWNER),SW_SHOWNORMAL);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SetForegroundWindow(Handle);<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>end;<br>
 
兄弟在去年事业期间写了一个Class关于TrayIcon的,初学的朋友可以Email:snguobin◎sina.com<br>&nbsp; &nbsp;高手就免了。
 
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>if (message.lParam = WM_LBUTTONDOWN) then<br>begin<br>&nbsp; ShowWindow(Handle, SW_SHOW );<br>end;<br><br>if (message.lParam = WM_RBUTTONDOWN) then<br>begin<br>&nbsp; GetCursorPos(p);<br>&nbsp; SetForegroundWindow (Application.Handle); //&lt;-------------------avoid a little bug.<br>&nbsp; Application.ProcessMessages;<br>&nbsp; popupmenu1.Popup( p.x ,p.y );<br>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; ShowIcon;<br>&nbsp;end<br>&nbsp;else<br>&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; 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>
 
多人接受答案了。
 
后退
顶部