托盘编程问题(50分)

  • 主题发起人 主题发起人 守望天使
  • 开始时间 开始时间

守望天使

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> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, Menus, ExtCtrls, &nbsp;shellapi, StdCtrls;<br>const<br> &nbsp;ICON_ID = 1; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 图标在本应用程序中的编号<br> &nbsp;MI_ICONEVENT = WM_USER + 1; &nbsp; &nbsp;// 图标上的鼠标事件<br><br><br>type<br> &nbsp;TFrmTemp = class(TForm)<br> &nbsp; &nbsp;PopupMenu1: TPopupMenu;<br> &nbsp; &nbsp;EnableOrDisable: TMenuItem;<br> &nbsp; &nbsp;AppItem: TMenuItem;<br> &nbsp; &nbsp;ExitItem: TMenuItem;<br> &nbsp; &nbsp;procedure FormCreate(Sender: TObject);<br> &nbsp; &nbsp;procedure FormClose(Sender: TObject; var Action: TCloseAction);<br> &nbsp; &nbsp;procedure FormActivate(Sender: TObject);<br> &nbsp; &nbsp;procedure AppItemClick(Sender: TObject);<br> &nbsp; &nbsp;procedure EnableOrDisableClick(Sender: TObject);<br> &nbsp; &nbsp;procedure ExitItemClick(Sender: TObject);<br><br> &nbsp;private<br> &nbsp; &nbsp;MainHandle:THandle;<br> &nbsp; &nbsp;NormalIcon,DisabledIcon:TIcon;<br> &nbsp; &nbsp;//正常和失效两种情况下的图标<br> &nbsp; &nbsp;Status:Boolean;<br> &nbsp; &nbsp;//标志&quot;允许使用&quot;还是&quot;禁止使用&quot;<br> &nbsp; &nbsp;procedure InstallIcon;<br> &nbsp; &nbsp;procedure ChangeIcon(s:Boolean);<br> &nbsp; &nbsp;procedure UnInstallIcon; <br> &nbsp; &nbsp;procedure IconOnClick(var message:TMessage); message MI_ICONEVENT;<br> &nbsp; &nbsp;//捕捉自定义消息MI_ICONEVENT的过程IconOnClick的声明<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;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> &nbsp;p:TPoint;<br>begin<br> &nbsp;//如果按下的是鼠标左键,并且允许显示,则显示Form2<br> &nbsp;if((message.lParam=WM_LBUTTONDOWN) and (Status=True)) then<br> &nbsp;begin &nbsp;<br> &nbsp; &nbsp;FrmMain.WindowState := wsnormal;<br> &nbsp; &nbsp;FrmMain.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;PopupMenu1.Popup(p.x,p.y);<br> &nbsp;end;<br>end;<br><br>procedure TFrmTemp.ChangeIcon(s:Boolean);<br>//自定义过程ChangeIcon,改变本程序在指示状态栏的图标<br>var<br> &nbsp;IconData:TNotifyIconData;<br>begin<br> &nbsp;IconData.cbSize:=SizeOf(IconData);<br> &nbsp;IconData.Wnd:=Handle;<br> &nbsp;IconData.uID:=ICON_ID;<br> &nbsp;if s=False then //禁止使用<br> &nbsp;begin<br> &nbsp; &nbsp;IconData.hIcon:=DisabledIcon.Handle;<br> &nbsp; &nbsp;Status:=False;<br> &nbsp; &nbsp;AppItem.Enabled:=False;<br> &nbsp;end<br> &nbsp;else //允许使用<br> &nbsp;begin<br> &nbsp; &nbsp;IconData.hIcon:=Normalicon.Handle;<br> &nbsp; &nbsp;Status:=True;<br> &nbsp; &nbsp;AppItem.Enabled:=True;<br> &nbsp;end;<br> &nbsp;IconData.uFlags:=NIF_ICON;<br> &nbsp;Shell_NotifyIcon(NIM_MODIFY,@IconData);<br>end;<br><br><br>procedure TFrmTemp.InstallIcon;<br>//安装图标<br>var<br> &nbsp;IconData:TNotifyIconData;<br>begin<br> &nbsp;NormalIcon:=TIcon.Create;<br> &nbsp;DisabledIcon:=TIcon.Create;<br> &nbsp;NormalIcon.LoadFromFile(ExtractFilePath(Paramstr(0))+'ico/0++.ico');<br>//允许使用状态下的图标<br> &nbsp;DisabledIcon.LoadFromFile(ExtractFilePath(Paramstr(0))+'ico/1++.ico');<br>//禁止使用状态下的图标<br> &nbsp;IconData.cbSize:=SizeOf(IconData);<br> &nbsp;IconData.Wnd:=Handle;<br> &nbsp;IconData.uID:=ICON_ID;<br> &nbsp;IconData.uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;<br> &nbsp;IconData.uCallBackMessage:=MI_ICONEVENT;<br> &nbsp;IconData.hIcon:=NormalIcon.Handle;<br> &nbsp;IconData.szTip:='给指示状态栏加图标的例程';<br> &nbsp;//鼠标悬在状态指示栏对应的图标上时的提示信息<br> &nbsp;Shell_NotifyIcon(NIM_ADD,@IconData);<br>end;<br>procedure TFrmTemp.UnInstallIcon;<br>//卸载图标<br>var<br> &nbsp;IconData:TNotifyIconData;<br>begin<br> &nbsp;IconData.cbSize:=SizeOf(IconData);<br> &nbsp;IconData.Wnd:=Handle;<br> &nbsp;IconData.uID:=ICON_ID;<br> &nbsp;Shell_NotifyIcon(NIM_DELETE,@IconData);<br>end;<br><br><br><br>procedure TFrmTemp.FormCreate(Sender: TObject);<br>begin<br> &nbsp;Status:=True;<br> &nbsp;InstallIcon;<br> &nbsp;//安装图标<br> &nbsp;ChangeIcon(True);<br> &nbsp;//改变图标为normal状态<br> &nbsp;ShowWindow(Application.Handle,SW_HIDE);<br> &nbsp;application.ShowMainForm := false;<br> &nbsp;//隐藏主窗体不显示<br> &nbsp;SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);<br> &nbsp;//在切换状态栏上不显示程序图标<br>end;<br><br><br>procedure TFrmTemp.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br> &nbsp;UnInstallIcon;<br> &nbsp;//卸载图标<br> &nbsp;NormalIcon.Free;<br> &nbsp;DisabledIcon.Free;<br> &nbsp;//释放两个图标资源<br>end;<br><br>procedure TFrmTemp.FormActivate(Sender: TObject);<br>begin<br> &nbsp;FrmTemp.width:=0;<br> &nbsp;FrmTemp.Height:=0;<br> &nbsp;//ShowWindow(application.Handle,SW_HIDE);<br> &nbsp;//隐藏Form1<br>end;<br><br>procedure TFrmTemp.AppItemClick(Sender: TObject);<br>begin<br> &nbsp;FrmMain.WindowState := wsnormal;<br> &nbsp;FrmMain.Show;<br> //显示给用户的主窗体<br>end;<br><br>procedure TFrmTemp.EnableOrDisableClick(Sender: TObject);<br>begin<br> &nbsp;if (Status=True) then<br> &nbsp;//根据状态更改菜单项标题<br> &nbsp;begin<br> &nbsp; &nbsp;ChangeIcon(False);<br> &nbsp; &nbsp;EnableOrDisable.Caption:='允许使用(&Y)';<br> &nbsp;end<br> &nbsp;else<br> &nbsp;begin<br> &nbsp; &nbsp;ChangeIcon(True);<br> &nbsp; &nbsp;EnableOrDisable.Caption:='禁止使用(&D)';<br> &nbsp;end;<br>end;<br><br><br>procedure TFrmTemp.ExitItemClick(Sender: TObject);<br>begin<br> &nbsp;Close;<br> &nbsp;//关闭窗体Form1,退出程序 &nbsp; &nbsp; <br>end;<br><br>end.<br><br>unit UntMain;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls, Buttons;<br><br>type<br> &nbsp;TFrmMain = class(TForm)<br> &nbsp; &nbsp;BitBtn1: TBitBtn;<br> &nbsp; &nbsp;procedure BitBtn1Click(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;FrmMain: TFrmMain;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TFrmMain.BitBtn1Click(Sender: TObject);<br>begin<br> &nbsp;Close;<br>end;<br><br>end.
 
找到一点内容,和大家分享一下<br>期望高手出现解答<br><br>托盘程序的缩放(托盘程序的最大化/最小化) <br>我们经常看到窗口最大化/最小化时的缩放效果。不幸的是这种效果只能把应用程序缩成应用程序工作栏中的图标,而且表面上似乎没有方法为最小化到托盘区中的程序添加相似的效果。<br><br>使用Windows API 函数DrawAnimatedRects可以做到。<br><br>这个函数需要窗口的句柄和两个标明起始和结束屏幕坐标的矩形区域。<br><br>以下代码说明如何使用这个API函数:<br><br><br>unit TestForm;<br><br>interface<br><br>uses<br>Windows, Classes, Forms, Controls, StdCtrls, ExtCtrls;<br><br>type<br><br>TZoomAction = (zaMinimize, zaMaximize);<br><br>TfrmTest = class(TForm)<br>procedure FormClose(Sender: TObject; var Action: TCloseAction);<br>procedure FormShow(Sender: TObject);<br>private<br>{ private declaration }<br>public<br>{ public declaration }<br>end;<br><br>var<br>frmTest: TfrmTest;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure ZoomEffect(theForm: TForm; theOperation: TZoomAction);<br>var<br>rcStart: TRect;<br>rcEnd: TRect;<br>rcTray: TRect;<br>hwndTray : hWnd;<br>hwndChild: hWnd;<br>begin<br>{ 寻找系统托盘区的位置}<br>hwndTray := FindWindow('Shell_TrayWnd', nil);<br>hwndChild := FindWindowEx(hwndTray, 0, 'TrayNotifyWnd', nil);<br>GetWindowRect(hwndChild, rcTray);<br><br>{点击用于最大化/最小化,并切换起始/结束}<br>if theOperation = zaMinimize then<br>begin<br>rcStart := theForm.BoundsRect;<br>rcEnd := rcTray;<br>end<br>else<br>begin<br>rcEnd := theForm.BoundsRect;<br>rcStart := rcTray;<br>end;<br><br>{ 以下是关键的部分... }<br>DrawAnimatedRects(theForm.Handle, IDANI_CAPTION, rcStart, rcEnd)<br>end;<br><br>procedure TfrmTest.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br>ZoomEffect(Self, zaMinimize);<br>end;<br><br>procedure TfrmTest.FormShow(Sender: TObject);<br>begin<br>ZoomEffect(Self, zaMaximize);<br>end;<br><br>end.
 
1。如何实现最小化和关闭都是隐藏窗体到托盘中,就如flashget功能;<br>2。在切换状态栏上的程序图标能否根据窗体的显示而显示,窗体不显示也隐藏(如果程序开始运行时不隐藏窗体,用一个窗体来实现托盘的话,用showWindow就可以实现);<br><br>问题一已经解决,解决代码如下<br>问题二不知道有没有解决方法,期待解答<br><br>unit UntMain;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls, Buttons;<br><br>type<br> &nbsp;TZoomAction = (zaMinimize, zaMaximize);<br><br> &nbsp;TFrmMain = class(TForm)<br><br> &nbsp; &nbsp;procedure FormClose(Sender: TObject; var Action: TCloseAction);<br> &nbsp; &nbsp;procedure FormShow(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;procedure WMSysCommand(var Msg: TWMSysCommand); message WM_SYSCOMMAND;<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br> &nbsp;procedure ZoomEffect(theForm: TForm; theOperation: TZoomAction);<br>var<br> &nbsp;FrmMain: TFrmMain;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TFrmMain.WMSysCommand(var Msg: TWMSysCommand);<br>begin<br> &nbsp;if (Msg.CmdType = SC_MINIMIZE) or (Msg.CmdType=SC_CLOSE) then<br> &nbsp;FrmMain.close<br> &nbsp;else<br> &nbsp;DefaultHandler(Msg);<br>end;<br><br><br>procedure ZoomEffect(theForm: TForm; theOperation: TZoomAction);<br>var<br> &nbsp;rcStart: TRect;<br> &nbsp;rcEnd: TRect;<br> &nbsp;rcTray: TRect;<br> &nbsp;hwndTray : hWnd;<br> &nbsp;hwndChild: hWnd;<br>begin<br> &nbsp;//寻找系统托盘区的位置<br> &nbsp;hwndTray := FindWindow('Shell_TrayWnd', nil);<br> &nbsp;hwndChild := FindWindowEx(hwndTray, 0, 'TrayNotifyWnd', nil);<br> &nbsp;GetWindowRect(hwndChild, rcTray);<br><br> &nbsp;//点击用于最大化/最小化,并切换起始/结束<br> &nbsp;if theOperation = zaMinimize then<br> &nbsp;begin<br> &nbsp; &nbsp;rcStart := theForm.BoundsRect;<br> &nbsp; &nbsp;rcEnd := rcTray;<br> &nbsp;end<br> &nbsp;else<br> &nbsp;begin<br> &nbsp; &nbsp;rcEnd := theForm.BoundsRect;<br> &nbsp; &nbsp;rcStart := rcTray;<br> &nbsp;end;<br> &nbsp;//以下是关键的部分...<br> &nbsp;DrawAnimatedRects(theForm.Handle, IDANI_CAPTION, rcStart, rcEnd)<br>end; &nbsp;<br><br>procedure TFrmMain.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br> &nbsp;ZoomEffect(Self, zaMinimize);<br>end;<br><br>procedure TFrmMain.FormShow(Sender: TObject);<br>begin<br> &nbsp;ZoomEffect(Self, zaMaximize);<br>end;<br>end.
 
以前写过,太麻烦了。就直接用了 CoolTrayIcon
 
结了,自己解决
 
后退
顶部