In Delphi 7 to make the system tray, can only make a relatively simple system tray, because the Shellapi file definition of the TNOTIFYICONDATA structure is a relatively early version. Defined as follows:
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 |
The following is implemented to minimize the functionality of the system tray:
1. Create a new application, and then in the "menu bar" → "Project" → "Options" → "Application" for the program to set the title and icon, you must set the icon, not to display the system tray will be blank;
2. Place a right-click menu on the form and add two menu items as shown:
3. The form unit file code is as follows:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21st
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; Customizing pallet messages
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; Declaring pallet messages
Procedure Wmsyscommand (var msg:tmessage); message wm_syscommand;
Public
{Public declarations}
End
Var
Form1:tform1;
Notifyicon:tnotifyicondata; Define the tray icon structure body
Implementation
{$R *.DFM}
{-------------------------------------------------------------------------------
Description: Create a pallet when a form is created
-------------------------------------------------------------------------------}
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; icon, message, prompt information
Ucallbackmessage: = wm_traymsg;
Hicon: = Application.Icon.Handle;
Sztip: = ' pallet test ';
End
Shell_NotifyIcon (Nim_add, @NotifyIcon);
End
{-------------------------------------------------------------------------------
Description: Unload the pallet when the form is destroyed
-------------------------------------------------------------------------------}
Procedure Tform1.formdestroy (Sender:tobject);
Begin
Shell_NotifyIcon (Nim_delete, @NotifyIcon);
End
{-------------------------------------------------------------------------------
Description: Intercept form minimized message, minimized to pallet
-------------------------------------------------------------------------------}
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: Customized Pallet messages
-------------------------------------------------------------------------------}
Procedure Tform1.wmtraymsg (var msg:tmessage);
Var
P:tpoint;
Begin
Case Msg.lparam of
WM_LBUTTONDOWN:Self.Visible: = True; Show form
Wm_rbuttondown:
Begin
SetForegroundWindow (Self.handle); Advance the window
GetCursorPos (P);
PM1. Popup (P.X,P.Y);
End
End
End
{-------------------------------------------------------------------------------
Description: Test menu item
-------------------------------------------------------------------------------}
Procedure Tform1.mnin1click (Sender:tobject);
Begin
ShowMessage (' one ');
End
End. |
4. The results of the operation are as follows:
Referring to MSDN (http://msdn.microsoft.com/en-us/library/bb773352 (vs.85). aspx), the NOTIFYICONDATA structure is described as:
typedef struct _NOTIFYICONDATA {
DWORD cbsize; The size of the struct, in bytes
HWND hwnd; Handle to Window
UINT UID; Identifier of the application-defined taskbar icon
UINT uflags; This member indicates which other members are valid data
UINT Ucallbackmessage; Application-defined message labeling
Hicon Hicon; Handle of an icon added, modified, or deleted
TCHAR sztip[64]; Pointer to a string that ends with/0
DWORD dwstate; Version 5.0, status of the icon
DWORD Dwstatemask; Version 5.0. The bits that indicate the Dwstate member can be set or accessed
TCHARSZINFO[256]; Pointer to a string ending with/0, with the contents of the string as the balloon tip
Union {
UINT utimeout; Indicates when the balloon tip times out, in milliseconds, after which the balloon tip disappears
UINT uversion; Used to set an icon message interface that uses Windows 95 or Windows 2000 style
};
TCHARszinfotitle[64]; A pointer to a string that ends with/0. The contents of the string are the caption of the balloon tip
DWORD Dwinfoflags; Set this member to add an icon to the balloon tip box, and the added icon appears to the left of the balloon tip title
GUID Guiditem; Keep
Hicon Hballoonicon; Custom balloon icons for Windows Vista or later
} notifyicondata, *pnotifyicondata; |
So in order to have bubble tips under Windows XP, then we manually add several types, presumably the code is as follows:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21st
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;
---------------------Start: Add this declaration under Delphi 7-------------------------------
Const
Nif_info = $00000010; Bubble Display Flag
Niif_none = $00000000; No icon
Niif_info = $00000001; Information icon
niif_warning = $00000002; Warning icon
Niif_error = $00000003; Error icon
Niif_user = $00000004; XP using the Hicon icon
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
---------------------Start: Add this declaration under Delphi 7-------------------------------
Const
Wm_traymsg = Wm_user + 101; Customizing pallet messages
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; Declaring pallet messages
Procedure Wmsyscommand (var msg:tmessage); message wm_syscommand;
Public
{Public declarations}
End
Var
Form1:tform1;
Notifyicon:tnotifyicondataex; Define the tray icon structure body
Implementation
{$R *.DFM}
{-------------------------------------------------------------------------------
Description: Create a pallet when a form is created
-------------------------------------------------------------------------------}
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; icon, message, prompt information
Ucallbackmessage: = wm_traymsg;
Hicon: = Application.Icon.Handle;
Sztip: = ' pallet test '; Szinfo: = ' hint content '; szinfotitle: = ' bubble title ';
Dwinfoflags: = Niif_user;
End
Shell_NotifyIcon (Nim_add, @NotifyIcon);
End
{-------------------------------------------------------------------------------
...... The following code is the same as the example above
-------------------------------------------------------------------------------} |
The results of the operation are as follows:
http://blog.csdn.net/zisongjia/article/details/68067370
The Delphi 7 is minimized to the system tray (mainly wm_traymsg and wm_syscommand messages)