首页 > 代码库 > Delphi 7下最小化到系统托盘(主要是WM_TRAYMSG和WM_SYSCOMMAND消息)
Delphi 7下最小化到系统托盘(主要是WM_TRAYMSG和WM_SYSCOMMAND消息)
在Delphi 7下要制作系统托盘,只能制作一个比较简单的系统托盘,因为ShellAPI文件定义的TNotifyIconData结构体是比较早的版本。定义如下:
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.运行结果如下:
参考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, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ShellAPI, Menus; //---------------------开始:Delphi 7下添加此声明------------------------------- const NIF_INFO = $00000010; //气泡显示标志 NIIF_NONE = $00000000; //无图标 NIIF_INFO = $00000001; //信息图标 NIIF_WARNING = $00000002; //警告图标 NIIF_ERROR = $00000003; //错误图标 NIIF_USER = $00000004; //XP使用hIcon图标 type TNotifyIconDataEx = record cbSize: DWORD; Wnd: HWND; uID: UINT; uFlags: UINT; uCallbackMessage: UINT; hIcon: HICON; szTip: array [0..127] of AnsiChar; dwState: DWORD; dwStateMask: DWORD; szInfo: array [0..255] of AnsiChar; case Integer of 0: ( uTimeout: UINT); 1: (uVersion: UINT; szInfoTitle: array [0..63] of AnsiChar; dwInfoFlags: DWORD); end; //---------------------开始:Delphi 7下添加此声明------------------------------- 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: TNotifyIconDataEx; //定义托盘图标结构体 implementation {$R *.dfm} {------------------------------------------------------------------------------- Description: 窗体创建时,即创建托盘 -------------------------------------------------------------------------------} procedure TForm1.FormCreate(Sender: TObject); begin with NotifyIcon do begin cbSize := SizeOf(TNotifyIconDataEx); Wnd := Self.Handle; uID := 1; uFlags := NIF_ICON + NIF_MESSAGE + NIF_TIP + NIF_INFO; //图标、消息、提示信息 uCallbackMessage := WM_TRAYMSG; hIcon := Application.Icon.Handle; szTip := ‘托盘测试‘; szInfo := ‘提示内容‘; szInfoTitle := ‘气泡标题‘; dwInfoFlags := NIIF_USER; end; Shell_NotifyIcon(NIM_ADD,@NotifyIcon); end; {------------------------------------------------------------------------------- ……以下代码与上面例子一样 -------------------------------------------------------------------------------} |
运行结果如下:
http://blog.csdn.net/zisongjia/article/details/68067370
Delphi 7下最小化到系统托盘(主要是WM_TRAYMSG和WM_SYSCOMMAND消息)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。