1 2 3 4 5 6 7 8 9 | | _NOTIFYICONDATAA = record cbSize: DWORD; Wnd: HWND; uID: UINT; uFlags: UINT; uCallbackMessage: UINT; hIcon: HICON; szTip: array [0..63] of AnsiChar; end; |
下面开始实现下最小化到系统托盘功能:
1.新建应用程序,然后在“菜单栏”→“Project”→“Options”→”Application“为程序设定下标题和图标,一定得设置图标,不让显示系统托盘的时候就会空白;
2.在窗体上放置一个右键菜单,添加两个菜单项,如下图所示:
3.窗体单元文件代码如下:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | | unit Unit1; interface uses windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ShellAPI, Menus; const WM_TRAYMSG = WM_USER + 101; //自定义托盘消息 type TForm1 = class(TForm) pm1: TPopupMenu; mniN1: TMenuItem; mniwo1: TMenuItem; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure mniN1Click(Sender: TObject); private procedure WMTrayMsg(var Msg: TMessage);message WM_TRAYMSG; //声明托盘消息 procedure WMSysCommand(var Msg: TMessage);message WM_SYSCOMMAND; public { Public declarations } end; var Form1: TForm1; NotifyIcon: TNotifyIconData; //定义托盘图标结构体 implementation {$R *.dfm} {------------------------------------------------------------------------------- Description: 窗体创建时,即创建托盘 -------------------------------------------------------------------------------} procedure TForm1.FormCreate(Sender: TObject); begin with NotifyIcon do begin cbSize := SizeOf(TNotifyIconData); Wnd := Self.Handle; uID := 1; uFlags := NIF_ICON + NIF_MESSAGE + NIF_TIP; //图标、消息、提示信息 uCallbackMessage := WM_TRAYMSG; hIcon := Application.Icon.Handle; szTip := '托盘测试'; end; Shell_NotifyIcon(NIM_ADD,@NotifyIcon); end; {------------------------------------------------------------------------------- Description: 窗体销毁时,卸载托盘 -------------------------------------------------------------------------------} procedure TForm1.FormDestroy(Sender: TObject); begin Shell_NotifyIcon(NIM_DELETE,@NotifyIcon); end; {------------------------------------------------------------------------------- Description: 截获窗体最小化消息,最小化到托盘 -------------------------------------------------------------------------------} procedure TForm1.WMSysCommand(var Msg: TMessage); begin if Msg.WParam = SC_ICON then Self.Visible := False else DefWindowProc(Self.Handle,Msg.Msg,Msg.WParam,Msg.LParam); end; {------------------------------------------------------------------------------- Description: 自定义的托盘消息 -------------------------------------------------------------------------------} procedure TForm1.WMTrayMsg(var Msg: TMessage); var p: TPoint; begin case Msg.LParam of WM_LbuttonDOWN: Self.Visible := True; //显示窗体 WM_RbuttonDOWN: begin SetForegrounDWindow(Self.Handle); //把窗口提前 GetCursorPos(p); pm1.Popup(p.X,p.Y); end; end; end; {------------------------------------------------------------------------------- Description: 测试菜单项 -------------------------------------------------------------------------------} procedure TForm1.mniN1Click(Sender: TObject); begin ShowMessage('One'); end; end. |
4.运行结果如下:
@H_267_419@
参考MSDN(http://msdn.microsoft.com/en-us/library/bb773352(VS.85).aspx)对NOTIFYICONDATA结构体的介绍却是:
typedef struct _NOTIFYICONDATA { DWORD cbSize; //结构体的大小,以字节为单位 HWND hWnd; //窗口的句柄 UINT uID; //应用程序定义的任务栏图标的标识符 UINT uFlags; //此成员表明具体哪些其他成员为合法数据 UINT uCallbackMessage; //应用程序定义的消息标示 HICON hIcon; //增加、修改或删除的图标的句柄 TCHAR szTip[64]; //指向一个以/0结束的字符串的指针 DWORD DWState; //Version 5.0,图标的状态 DWORD DWStateMask; //Version 5.0. 指明DWState成员的那些位可以被设置或者访问 TCHAR szInfo[256]; //指向一个以/0结束的字符串的指针,字符串的内容为气球提示内容 union { UINT uTimeout; //表示气球提示超时的时间,单位为毫秒,此时间后气球提示将消失 UINT uVersion; //用来设置使用windows 95 还是 windows 2000风格的图标消息接口 }; TCHAR szInfoTitle[64]; //指向一个以/0结束的字符串的指针。字符串的内容为气球提示的标题 DWORD DWInfoFlags; //设置此成员用来给气球提示框增加一个图标,增加的图标出现在气球提示标题的左侧 GUID guIDItem; //保留 HICON hBalloonIcon; //用于windows Vista或更高版本的自定义气球图标 } NOTIFYICONDATA, *PNOTIFYICONDATA; |
那么为了在windows XP下也有气泡提示,那么我们手动添加几个类型,大概代码如下:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | | unit Unit1; interface uses windows, Menus; //---------------------开始:Delphi 7下添加此声明------------------------------- const NIF_INFO = //气泡显示标志 000010; //无图标 000000 NIIF_NONE = //信息图标 000001; //警告图标 000002 NIIF_INFO = //错误图标 000003; //XP使用hIcon图标 000004 NIIF_WARNING = type; record NIIF_ERROR = array; [0..127] NIIF_USER = of; array [0..255] TNotifyIconDataEx = of cbSize: DWORD; Wnd: HWND; uID: UINT; uFlags: UINT; uCallbackMessage: UINT; hIcon: HICON; szTip: case of 0 AnsiChar; DWState: DWORD; DWStateMask: DWORD; szInfo: 1 array [0..63] AnsiChar; of Integer end: ( uTimeout: UINT); //---------------------开始:Delphi 7下添加此声明------------------------------- : (uVersion: UINT; szInfoTitle: const 101 //自定义托盘消息 AnsiChar; DWInfoFlags: DWORD); type class; procedure procedure WM_TRAYMSG = WM_USER + procedure; private procedure TForm1 = var(TForm) pm1: TPopupMenu; mniN1: TMenuItem; mniwo1: TMenuItem; message FormCreate(Sender: TObject); //声明托盘消息 FormDestroy(Sender: TObject); procedure mniN1Click(Sender: TObject); var message WMTrayMsg(public Msg: TMessage);{ Public declarations } WM_TRAYMSG; end var WMSysCommand(//定义托盘图标结构体 Msg: TMessage);implementation WM_SYSCOMMAND; {$R *.dfm} {------------------------------------------------------------------------------- Description: 窗体创建时,即创建托盘 ; -------------------------------------------------------------------------------} Form1: TForm1; NotifyIcon: TNotifyIconDataEx; procedure begin with do begin 1 //图标、消息、提示信息 TForm1.FormCreate(Sender: TObject); '托盘测试' '提示内容' NotifyIcon '气泡标题' end cbSize := SizeOf(TNotifyIconDataEx); Wnd := Self.Handle; uID := end; uFlags := NIF_ICON + NIF_MESSAGE + NIF_TIP + NIF_INFO; {------------------------------------------------------------------------------- uCallbackMessage := WM_TRAYMSG; hIcon := Application.Icon.Handle; szTip := ……以下代码与上面例子一样 ; szInfo := -------------------------------------------------------------------------------}; szInfoTitle := ; DWInfoFlags := NIIF_USER; ; Shell_NotifyIcon(NIM_ADD,@NotifyIcon); ; |
运行结果如下:
总结以上是内存溢出为你收集整理的Delphi 7下最小化到系统托盘全部内容,希望文章能够帮你解决Delphi 7下最小化到系统托盘所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)