守
守望天使
Unregistered / Unconfirmed
GUEST, unregistred user!
托盘的功能实现了(程序开始运行就隐藏了窗体);但是碰到了下面的问题;<br><br>1。如何实现最小化和关闭都是隐藏窗体到托盘中,就如flashget功能;<br>2。在切换状态栏上的程序图标能否根据窗体的显示而显示,窗体不显示也隐藏(如果程序开始运行时不隐藏窗体,用一个窗体来实现托盘的话,用showWindow就可以实现);<br><br>如有解答,请先调试通过再发上来<br><br>托盘实现方法如下用了两个窗体,主窗体被隐藏,另外一个窗体成为代替主窗体的可以通过托盘菜单调用显示的窗体)<br>unit UntTemp;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, Menus, ExtCtrls, shellapi, StdCtrls;<br>const<br> ICON_ID = 1; // 图标在本应用程序中的编号<br> MI_ICONEVENT = WM_USER + 1; // 图标上的鼠标事件<br><br><br>type<br> TFrmTemp = class(TForm)<br> PopupMenu1: TPopupMenu;<br> EnableOrDisable: TMenuItem;<br> AppItem: TMenuItem;<br> ExitItem: TMenuItem;<br> procedure FormCreate(Sender: TObject);<br> procedure FormClose(Sender: TObject; var Action: TCloseAction);<br> procedure FormActivate(Sender: TObject);<br> procedure AppItemClick(Sender: TObject);<br> procedure EnableOrDisableClick(Sender: TObject);<br> procedure ExitItemClick(Sender: TObject);<br><br> private<br> MainHandle:THandle;<br> NormalIcon,DisabledIcon:TIcon;<br> //正常和失效两种情况下的图标<br> Status:Boolean;<br> //标志"允许使用"还是"禁止使用"<br> procedure InstallIcon;<br> procedure ChangeIcon(s:Boolean);<br> procedure UnInstallIcon; <br> procedure IconOnClick(var message:TMessage); message MI_ICONEVENT;<br> //捕捉自定义消息MI_ICONEVENT的过程IconOnClick的声明<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> FrmTemp: TFrmTemp;<br><br>implementation<br><br>uses UntMain;<br><br><br><br>{$R *.dfm}<br><br><br>procedure TFrmTemp.IconOnClick(var message:TMessage);<br>//处理鼠标在指示状态栏对应的图标上的单击事件<br>var<br> p:TPoint;<br>begin<br> //如果按下的是鼠标左键,并且允许显示,则显示Form2<br> if((message.lParam=WM_LBUTTONDOWN) and (Status=True)) then<br> begin <br> FrmMain.WindowState := wsnormal;<br> FrmMain.Show; <br> end;<br> //如果按下的是鼠标右键,则显示弹出菜单<br> if(message.lParam=WM_RBUTTONDOWN)then<br> begin<br> GetCursorPos(p);<br> PopupMenu1.Popup(p.x,p.y);<br> end;<br>end;<br><br>procedure TFrmTemp.ChangeIcon(s:Boolean);<br>//自定义过程ChangeIcon,改变本程序在指示状态栏的图标<br>var<br> IconData:TNotifyIconData;<br>begin<br> IconData.cbSize:=SizeOf(IconData);<br> IconData.Wnd:=Handle;<br> IconData.uID:=ICON_ID;<br> if s=False then //禁止使用<br> begin<br> IconData.hIcon:=DisabledIcon.Handle;<br> Status:=False;<br> AppItem.Enabled:=False;<br> end<br> else //允许使用<br> begin<br> IconData.hIcon:=Normalicon.Handle;<br> Status:=True;<br> AppItem.Enabled:=True;<br> end;<br> IconData.uFlags:=NIF_ICON;<br> Shell_NotifyIcon(NIM_MODIFY,@IconData);<br>end;<br><br><br>procedure TFrmTemp.InstallIcon;<br>//安装图标<br>var<br> IconData:TNotifyIconData;<br>begin<br> NormalIcon:=TIcon.Create;<br> DisabledIcon:=TIcon.Create;<br> NormalIcon.LoadFromFile(ExtractFilePath(Paramstr(0))+'ico/0++.ico');<br>//允许使用状态下的图标<br> DisabledIcon.LoadFromFile(ExtractFilePath(Paramstr(0))+'ico/1++.ico');<br>//禁止使用状态下的图标<br> IconData.cbSize:=SizeOf(IconData);<br> IconData.Wnd:=Handle;<br> IconData.uID:=ICON_ID;<br> IconData.uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;<br> IconData.uCallBackMessage:=MI_ICONEVENT;<br> IconData.hIcon:=NormalIcon.Handle;<br> IconData.szTip:='给指示状态栏加图标的例程';<br> //鼠标悬在状态指示栏对应的图标上时的提示信息<br> Shell_NotifyIcon(NIM_ADD,@IconData);<br>end;<br>procedure TFrmTemp.UnInstallIcon;<br>//卸载图标<br>var<br> IconData:TNotifyIconData;<br>begin<br> IconData.cbSize:=SizeOf(IconData);<br> IconData.Wnd:=Handle;<br> IconData.uID:=ICON_ID;<br> Shell_NotifyIcon(NIM_DELETE,@IconData);<br>end;<br><br><br><br>procedure TFrmTemp.FormCreate(Sender: TObject);<br>begin<br> Status:=True;<br> InstallIcon;<br> //安装图标<br> ChangeIcon(True);<br> //改变图标为normal状态<br> ShowWindow(Application.Handle,SW_HIDE);<br> application.ShowMainForm := false;<br> //隐藏主窗体不显示<br> SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);<br> //在切换状态栏上不显示程序图标<br>end;<br><br><br>procedure TFrmTemp.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br> UnInstallIcon;<br> //卸载图标<br> NormalIcon.Free;<br> DisabledIcon.Free;<br> //释放两个图标资源<br>end;<br><br>procedure TFrmTemp.FormActivate(Sender: TObject);<br>begin<br> FrmTemp.width:=0;<br> FrmTemp.Height:=0;<br> //ShowWindow(application.Handle,SW_HIDE);<br> //隐藏Form1<br>end;<br><br>procedure TFrmTemp.AppItemClick(Sender: TObject);<br>begin<br> FrmMain.WindowState := wsnormal;<br> FrmMain.Show;<br> //显示给用户的主窗体<br>end;<br><br>procedure TFrmTemp.EnableOrDisableClick(Sender: TObject);<br>begin<br> if (Status=True) then<br> //根据状态更改菜单项标题<br> begin<br> ChangeIcon(False);<br> EnableOrDisable.Caption:='允许使用(&Y)';<br> end<br> else<br> begin<br> ChangeIcon(True);<br> EnableOrDisable.Caption:='禁止使用(&D)';<br> end;<br>end;<br><br><br>procedure TFrmTemp.ExitItemClick(Sender: TObject);<br>begin<br> Close;<br> //关闭窗体Form1,退出程序 <br>end;<br><br>end.<br><br>unit UntMain;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls, Buttons;<br><br>type<br> TFrmMain = class(TForm)<br> BitBtn1: TBitBtn;<br> procedure BitBtn1Click(Sender: TObject);<br> private<br> <br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> FrmMain: TFrmMain;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TFrmMain.BitBtn1Click(Sender: TObject);<br>begin<br> Close;<br>end;<br><br>end.