怎样将一个程序从托盘中唤醒过来?就像FOXMAIL已最小化到托盘,现在我要将它SHOW到桌面上来。(100分)

  • 主题发起人 主题发起人 del520
  • 开始时间 开始时间
D

del520

Unregistered / Unconfirmed
GUEST, unregistred user!
程序已经运行了,有可能用户没注意,又会去再次启动程序,所以我想用一代码,如果程序窗口句柄已存在,就将它SHOW出来。<br>程序有可能有两程序状态<br>1.最小化了<br>2.最小化到托盘了<br><br>所以我想到了消息,但我用SendMessage(handle,WM_SHOWWINDOW,0,0)<br>无法将我的程序唤醒过来。<br><br>应该怎么办?
 
unit.pas中<br>const<br>&nbsp; CM_RESTORE = WM_USER + $1000; {自定义的“恢复”消息}<br>&nbsp; SuperWebAppName = 'Files Just Running';<br>{指定窗口名称}<br>procedure TForm1.CreateParams(var Params: TCreateParams);<br>begin<br>&nbsp; inherited CreateParams(Params);<br>&nbsp; Params.WinClassName := SuperWebAppName;<br>end;<br>{处理“恢复”消息}<br>procedure TForm1.RestoreRequest(var message: TMessage);<br>begin<br>&nbsp; SHOW;<br>&nbsp; exit;<br>&nbsp; if IsIconic(Application.Handle) = TRUE then<br>&nbsp; &nbsp; Application.Restore<br>&nbsp; else<br>&nbsp; &nbsp; Application.BringToFront;<br>end;<br><br><br><br><br>dpr文件中:<br>program GoProxy;<br><br>uses<br>&nbsp; Forms,windows,messages,<br>&nbsp; Unit1 in 'Unit1.pas' {Form1};<br><br>{$R *.res}<br>var<br>&nbsp; RvHandle : hWnd;<br>begin<br>&nbsp; RvHandle := FindWindow(SuperWebAppName,NIL);<br>&nbsp; if RvHandle &gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; PostMessage(RvHandle,CM_RESTORE,0,0);<br>&nbsp; &nbsp; halt;<br>&nbsp; end;<br>&nbsp; Application.Initialize;<br>&nbsp; Application.CreateForm(TForm1, Form1);<br>&nbsp; Application.Run;<br>end.
 
试了一下,SHOW不出来。
 
假设是Delphi写的程序,那么需要注意Application.Handle和MainForm.Handle的区别。尼需要发消息的是Application.Handle。通过WindowFromPoint只能得到MainForm.Handle.
 
我想问一下,如果是一个子窗口,如何让他SHOW出来,我试着编写<br>Function Createform(ParentApplication:TApplication;ParentForm:TForm;const UserName:String):String; export; stdcall;<br>var<br>&nbsp; OForm_rs: TOForm_rs;<br>begin<br>&nbsp; Application:=ParentApplication;<br>&nbsp; CoInitialize(Nil);<br>&nbsp; OForm_rs:=TOForm_rs.Create(ParentForm);<br>&nbsp; OForm_rs.MyParentForm:=ParentForm;<br>&nbsp; OForm_rs.MyParentApplication:=ParentApplication;<br>&nbsp; OForm_rs.User :=UserName ;<br>&nbsp; OForm_rs.Show;<br>end;<br>但是却不知怎样找到这个窗口,无法保证只开一个,请赐教!
 
和你碰到了同一个问提,可以发消息将<br>其关但不知怎将其重SHOW!
 
谁再来看看啊?
 
找个控件不就得了!
 
不需要用到控件那么麻烦吧,总觉得好像用一个消息就可以搞定,但就是弄不出来。<br>发送一个CLOSE的消息又可以。
 
如果是最小化,用上面的方法就可以SHOW了,如果是最小化到托盘,就必须将窗口的WS_EX_TOOLWINDOW风格去掉再用上面的方法才行。
 
托盘消息处理:<br><br>const WM_MYTRAYICONCALLBACK = WM_USER + 1000 ;<br><br>private<br>&nbsp; &nbsp;MyTrayIcon : TNotifyIconData ;<br>&nbsp; &nbsp;procedure WMMyTrayIconCallBack(Var Msg : TMessage);<br>&nbsp; &nbsp;message WM_MYTRAYICONCALLBACK ;//托盘消息处理过程<br>//<br>procedure TFrmMySrv.FormCreate(Sender: TObject);<br>begin<br>&nbsp;Icon.Handle := LoadIcon(Hinstance,'MAINICON');<br>&nbsp;MyTrayIcon.cbSize := SizeOf(TNotifyIconData);<br>&nbsp;MyTrayIcon.Wnd := Handle ;<br>&nbsp;MyTrayIcon.uID := 1 ;<br>&nbsp;MyTrayIcon.uFlags := NIF_ICON or NIF_TIP or NIF_MESSAGE ;<br>&nbsp;MyTrayIcon.uCallBackMessage := WM_MYTRAYICONCALLBACK ;///////////////将自定义托盘消息传递进去<br>&nbsp;MyTrayIcon.hIcon := Icon.Handle;<br>&nbsp;StrCopy (MyTrayIcon.szTip, PChar(Caption));<br>&nbsp;Shell_NotifyIcon(NIM_ADD,@MyTrayIcon);<br>&nbsp;ShowWindow(Handle,sw_Hide);<br>&nbsp;Visible := False ;<br>&nbsp;Application.ShowMainForm := False ;<br>&nbsp;SetForegroundWindow(Application.Handle);<br>end;<br><br>////消息过程实现<br>procedure TFrmMySrv.WMMyTrayIconCallBack(var Msg: TMessage);<br>var CursorPos : TPoint;<br>begin<br>&nbsp;case Msg.LParam of<br>&nbsp; &nbsp;WM_LBUTTONDBLCLK : //双击消息:弹出主窗口<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;//你就试试下面这几句看行不行<br>&nbsp; &nbsp; &nbsp;Visible := not Visible ;<br>&nbsp; &nbsp; &nbsp;Application.ShowMainForm := Visible ;<br>&nbsp; &nbsp; &nbsp;SetForegroundWindow(Application.Handle);<br>&nbsp; &nbsp;end ;<br>&nbsp; &nbsp;WM_RBUTTONDOWN : //鼠标右键:弹出菜单<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;GetCursorPos(CursorPos);<br>&nbsp; &nbsp; &nbsp;Popupmenu1.Popup(CursorPos.X,CursorPos.Y);//popupmen1里面就可以加入显示主窗口、退出等功能<br>&nbsp; &nbsp;end ;<br>&nbsp;end ;<br>end;
 
不只道我理解的对不对 你可以试试 <br>这是工程文件<br>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<br>program Project2;<br><br>uses<br>&nbsp; Forms,windows,<br>&nbsp; Units in 'Units.pas' {Form1};<br><br>{$R *.RES}<br>var<br>fhandle : Thandle;<br>begin<br>&nbsp; fhandle:=findwindow(0,'sx');<br>&nbsp; &nbsp;if &nbsp;fhandle =0 then<br>&nbsp; &nbsp;begin<br>&nbsp; Application.Initialize;<br>&nbsp; Application.Title := 'sx';<br>&nbsp; Application.CreateForm(TForm1, Form1);<br>&nbsp; Application.Run;<br><br>&nbsp;end<br>&nbsp; else<br>&nbsp; begin<br>&nbsp; &nbsp;showwindow(fhandle,SW_RESTORE);<br>&nbsp; &nbsp;setforegroundwindow(fHandle )<br><br>&nbsp; &nbsp;end;<br>&nbsp;end.<br>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<br>这是单元文件<br>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,shellapi,<br>&nbsp; Menus, StdCtrls;<br><br>const<br>&nbsp;MY_MESSAGE = WM_USER + $100;<br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; PopupMenu1: TPopupMenu;<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure FormClose(Sender: TObject; var Action: TCloseAction);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br><br>&nbsp; &nbsp; procedure OnIconNotify(var message:Tmessage);message MY_MESSAGE;<br>&nbsp; &nbsp; procedure NotifyCreate;<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; nid:TNotifyIconData;<br>&nbsp; messageid:dword;<br><br><br>implementation<br><br>{$R *.DFM}<br><br>{ TForm1 }<br><br>procedure TForm1.NotifyCreate;<br>var<br>nid:Tnotifyicondata;<br>begin<br>nid.cbsize:=sizeof(nid);<br>nid.wnd:= form1.Handle ;<br>nid.uID := 1;<br>nid.hIcon :=application.Icon.Handle ;<br>nid.szTip:='hhhh';<br>nid.uCallbackMessage :=MY_MESSAGE;<br>nid.uFlags :=NIF_ICON or NIF_TIP or NIF_MESSAGE;<br>if not shell_notifyicon(NIM_ADD,@nid) then begin<br>&nbsp; showmessage('出错了');<br>&nbsp; application.Terminate ;<br>&nbsp; end;<br>setwindowlong(application.handle,GWL_ExSTYLE,WS_EX_TOOLWINDOW);<br>end;<br><br><br><br>procedure TForm1.OnIconNotify(var message: Tmessage);<br>const<br>busy:boolean = false;<br>var<br>mypt:tpoint;<br>begin<br>inherited;<br>if not busy &nbsp;then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp;busy:=true;<br>&nbsp; &nbsp; &nbsp;if message.LParam = WM_LBUTTONDOWN then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; application.Restore;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setforegroundwindow(application.Handle );<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if message.LParam = WM_RBUTTONDOWN then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;getcursorpos(mypt);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;popupmenu1.popup(mypt.x,mypt.y);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;message.Result:=0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; busy:= false;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br><br>form1.NotifyCreate ;<br>end;<br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br>shell_notifyicon(nim_delete,@nid);<br>end;<br><br><br>end.<br>
 
不好意思 低级错误 nid 申明了两次 应该是全局变量
 
后退
顶部