最小化消息的截获 ( 积分: 50 )

  • 主题发起人 主题发起人 huhuc
  • 开始时间 开始时间
H

huhuc

Unregistered / Unconfirmed
GUEST, unregistred user!
我想在某个程序最小化时候,放到系统托盘了,现在用的是WMSysCommand.WParam=SC_MINIMIZE<br>可以通过。<br>但是有的程序,比如flashget在你按下win+D的时候,或者点那个显示桌面的时候,窗体也会消失,请问这是怎么实现的?
 
我想在某个程序最小化时候,放到系统托盘了,现在用的是WMSysCommand.WParam=SC_MINIMIZE<br>可以通过。<br>但是有的程序,比如flashget在你按下win+D的时候,或者点那个显示桌面的时候,窗体也会消失,请问这是怎么实现的?
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1201512<br><br>-----------------------转自网络<br><br>如果你要在应用程序窗体最小化的时候隐藏,那么你要写如下的函数:<br>procedure TForm1.MinimizeForm(Sender:TObject);<br>begin<br> &nbsp;//Hide Application Task Bar<br> &nbsp;ShowWindow(Application.Handle,SW_HIDE); <br>end;<br><br>然后在Form1的构造函数加入下列语句:<br>Application.OnMinimize:=MinimizeForm;
 
在主Form上添加ApplicationEvents(在Additional页内)控件,有最小化事件.
 
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/objects/shell/shell.asp
 
参见shell object的MinimizeAll方法。
 
好像有个ToggleDesktop ,不过不知道怎么拦截这两个东西,楼上的MinimizeAll不知道怎么用<br><br>http://www.delphibbs.com/delphibbs/dispq.asp?LID=1424533
 
代码:<br><br>unit Unit1;<br>interface<br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls, Buttons, ExtCtrls, Spin,shellAPI, Menus, ImgList;<br><br>const<br> &nbsp;WM_BARICON=WM_USER+200;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;SpinEdit1: TSpinEdit;<br> &nbsp; StaticText1: TStaticText;<br> &nbsp; &nbsp;BitBtn1: TBitBtn;<br> &nbsp; &nbsp;BitBtn2: TBitBtn;<br> &nbsp; &nbsp;PopupMenu1: TPopupMenu;<br> &nbsp; &nbsp;N1: TMenuItem;<br> &nbsp; &nbsp;N2: TMenuItem;<br><br> &nbsp; procedure BitBtn1Click(Sender: TObject);<br> &nbsp; &nbsp;procedure BitBtn2Click(Sender: TObject);<br> &nbsp; &nbsp;procedure SpinEdit1Change(Sender: TObject);<br> &nbsp; &nbsp;procedure N2Click(Sender: TObject);<br> &nbsp; &nbsp;procedure N1Click(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;procedure WMSysCommand(var Message: TMessage); message WM_SYSCOMMAND;<br> &nbsp; &nbsp;procedure WMBarIcon(var Message:TMessage);message WM_BARICON; <br> Private declarations }<br> <br> &nbsp; &nbsp;{ Private declarations }<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>{ TForm1 }<br> <br><br>procedure TForm1.BitBtn1Click(Sender: TObject);<br>begin<br>end;<br> <br>procedure TForm1.BitBtn2Click(Sender: TObject);<br>begin<br> &nbsp;close;<br>end;<br> <br>procedure TForm1.SpinEdit1Change(Sender: TObject);<br>begin<br> &nbsp;Timer1.Interval:=strtoint(SpinEdit1.Text)*1000;<br>end;<br> <br>procedure TForm1.WMBarIcon(var Message: TMessage);<br>var<br> &nbsp;lpData:PNotifyIconData;<br> &nbsp;pos:TPoint;<br>begin<br> &nbsp;case &nbsp;Message.LParam of<br> &nbsp; WM_LBUTTONDBLCLK:<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;//如果用户双击击任务栏图标则将图标删除并回复窗口。<br> &nbsp; &nbsp; &nbsp;lpData := new(PNotifyIconDataA);<br> &nbsp; &nbsp; &nbsp;lpData.cbSize := 88;//SizeOf(PNotifyIconDataA);<br> &nbsp; &nbsp; &nbsp;lpData.Wnd := Form1.Handle;<br> &nbsp; &nbsp; &nbsp;lpData.hIcon := application.Icon.Handle;// Form1.Icon.Handle;<br> &nbsp; &nbsp; &nbsp;lpData.uCallbackMessage := WM_BARICON;<br> &nbsp; &nbsp; &nbsp;lpData.uID :=0;<br> &nbsp; &nbsp; &nbsp;lpData.szTip := 'Samples';<br> &nbsp; &nbsp; &nbsp;lpData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;<br> &nbsp; &nbsp; &nbsp;Shell_NotifyIcon(NIM_DELETE,lpData);<br> &nbsp; &nbsp; &nbsp;dispose(lpData);<br> &nbsp; &nbsp; &nbsp;Form1.Visible := True;<br> &nbsp; &nbsp;end;<br> &nbsp;WM_RBUTTONDOWN: &nbsp;//用户单击右键,则弹出菜单<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;getcursorpos(pos);<br> &nbsp; &nbsp; &nbsp;popupmenu1.Popup(pos.x,pos.y);<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br>end;<br> <br>procedure TForm1.WMSysCommand(var Message: TMessage);<br>var<br> &nbsp;lpData:PNotifyIconData;<br>begin<br> &nbsp;if Message.WParam =SC_ICON then<br> &nbsp;begin//如果用户最小化窗口则将窗口隐藏并在任务栏上添加图标<br> &nbsp; &nbsp;lpData:= new(PNotifyIconDataA);<br> &nbsp; &nbsp;lpData.cbSize := 88; //SizeOf(PNotifyIconDataA);<br> &nbsp; &nbsp;lpData.Wnd := Form1.Handle;<br> &nbsp; &nbsp;lpData.hIcon :=application.Icon.Handle;// Form1.Icon.Handle;<br> &nbsp; &nbsp;lpData.uCallbackMessage := WM_BARICON;<br> &nbsp; &nbsp;lpData.uID :=0;<br> &nbsp; &nbsp;lpData.szTip := 'Hello';<br> &nbsp; &nbsp;lpData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;<br> &nbsp; &nbsp;Shell_NotifyIcon(NIM_ADD,lpData);<br> &nbsp; &nbsp;dispose(lpData);<br> &nbsp; &nbsp;Form1.Visible :=False;<br> &nbsp;end<br> &nbsp;else<br> &nbsp;begin //如果是其它的SystemCommand消息则调用系统缺省处理函数处理之。<br> &nbsp; &nbsp;DefWindowProc(Form1.Handle,Message.Msg,Message.WParam,Message.LParam);<br> &nbsp;end;<br>end;<br> <br>procedure TForm1.N2Click(Sender: TObject);<br>var<br> &nbsp;lpData:PNotifyIconData;<br>begin<br> &nbsp;lpData := new(PNotifyIconDataA);<br> &nbsp;lpData.cbSize := 88;//SizeOf(PNotifyIconDataA);<br> &nbsp;lpData.Wnd :=Form1.Handle;<br> &nbsp;lpData.hIcon :=application.Icon.Handle;// Form1.Icon.Handle;<br> &nbsp;lpData.uCallbackMessage :=WM_BARICON;<br> &nbsp;lpData.uID :=0;<br> &nbsp;lpData.szTip := 'Samples';<br> &nbsp;lpData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;<br> &nbsp;Shell_NotifyIcon(NIM_DELETE,lpData);<br> &nbsp;dispose(lpData);<br> &nbsp;Form1.Visible :=True;<br> <br>end;<br> <br>procedure TForm1.N1Click(Sender: TObject);<br>begin<br> &nbsp;close;<br>end;<br> <br>end.<br> <br>如果在退出时一起删除图标,则:<br> <br>procedure TForm1.N2Click(Sender: TObject);<br>var<br> &nbsp;lpData:PNotifyIconData;<br>begin<br> &nbsp;lpData := new(PNotifyIconDataA);<br> &nbsp;lpData.Wnd :=Form1.Handle;<br> &nbsp;lpData.uID :=0;<br> &nbsp;Shell_NotifyIcon(NIM_DELETE,lpData);<br> &nbsp;dispose(lpData);<br> &nbsp;close;<br><br>end;<br> <br>在TNotifyIconData结构中最基本的元素就是Wnd和uID,由这两个元素确立托盘图标在系统托盘中的唯一性(其实这跟操作系统中用全局原子区别它是一样的),uID是自定义的托盘标识,当一个窗口有多个托盘图标时才显得有意义。<br>您说用Shell_NotifyIcon(NIM_DELETE,@lpData);不行,则可能是您设置TNotifyIconData时没做好,要用NIM_DELETE删除图标,正常应该有三个元素是必须设置的:除上面两个外,还有一个就是:cbSize !而且它必须在最前设置,否则无效。这跟new(PNotifyIconDataA);是一样的意思。如:<br><br>procedure TForm1.N1Click(Sender: TObject);<br>var<br> &nbsp;lpData : TNotifyIconData;<br>begin<br> //下面SizeOf()不能是PNotifyIconData,否则变成指针的大小了<br> &nbsp;lpData.cbSize := SizeOf(TNotifyIconData);<br> &nbsp;lpData.Wnd :=Form1.Handle;<br> &nbsp;lpData.uID :=0;<br> &nbsp;Shell_NotifyIcon(NIM_DELETE,@lpData);<br><br> &nbsp;close;<br>end;<br> 其实这样就已经可以完全正常删除一个托盘图标,当然用NIM_ADD添加就不一样了
 
无泪啊,你说了这么多,审题了吗?<br>还好问题解决了!
 

Similar threads

D
回复
0
查看
923
DelphiTeacher的专栏
D
D
回复
0
查看
715
DelphiTeacher的专栏
D
D
回复
0
查看
676
DelphiTeacher的专栏
D
后退
顶部