Delphi 7下最小化到系统托盘

在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
{------------------------------------------------------------------------------- 
 ……以下代码与上面例子一样 
-------------------------------------------------------------------------------} 

运行结果如下:

 

更多阅读:
1.http://www.swissdelphicenter.ch/torry/showcode.php?id=1164
2.http://blog.csdn.net/kvs112219/archive/2010/12/11/6069936.aspx
3.http://www.delphibbs.com/delphibbs/dispq.asp?lid=3122188

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DELPHI7下好用的托盘控件,安装简单,版本进度如下:TCoolTrayIcon, ver. 2.3.0 - ver. 2.3.0: Various minor bugs fixed: 1) Calling the ShowMainForm and HideMainForm methods before the main form was created would crash the app. Fixed thanks to James Legg. 2) The tooltip would give trouble if it was 64 chars. Thanks to Toms Baugis and Teus de Jong. 3) The popup menu would not close itself auto- matically if the StartMinimized property was true. Thanks to Ingo Krueger, Chris Witt, and Reister HansJoerg. - ver. 2.2.2: When there was more than one form visible the taskbar icon would not hide when the main form was minimized. Fixed. - ver. 2.2.1: Fixed a popup menu bug. Also fixed a bug which meant D2 and D3 users could not compile the component. Added more icon animations in the demo. Thanks to Daniele Gervasoni for the "tray graph". - ver. 2.2.0: IconIndex is now a property, allowing you to specify an icon at run-time. The OnCycle event is changed so it returns the index to the next icon that will display when cycling. Finally, I fixed a bug that caused the taskbar icon not to display itself when MinimizeToTray was true. Thanks to Lorenz Graf for pointing it out to me. - ver. 2.1.4: The main form will no longer show itself untimely after the form's BorderStyle property is changed. Thanks to Thomas Reimann. - ver. 2.1.3: Fixed a bug that caused the main form not to display itself (how embarassing). - ver. 2.1.2: I *finally* found the bug that would cause some compound controls (like controls on notebook pages or tab sheets) not to display properly when StartMinimized was true. Incidently, this also means that TForm.Position now works. Also fixed a minor bug that caused modal forms owned by the main form to appear behind the main form when the popup menu appeared (thanks to Arash Ramin). - ver. 2.1.1: Added notification method to properly detect whether the associated popup menu and imagelist are deleted. Thanks to
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值