Delphi programming status area of the taskbar

On the taskbar Windows desktop has a recessed area, which displays the system clock, and some icons, the rectangular area is the Windows taskbar status area (taskbar status area). This article will introduce programming using Borland Delphi task bar status area, that is how the application icon appears in the taskbar status area.

---- The principle

---- icon in the taskbar status area to add, delete, and modify through the Windows API function Shell_NotifyIcon implemented, this function is provided by the SHELL32.DLL Windows dynamic link library. In Delphi, the Shell_NotifyIcon ShellAPI cell function is a statement which

---- function prototype is as follows:

---- function Shell_NotifyIcon(dwMessage: DWORD; lpData: PNotifyIconData): BOOL; stdcall;

---- wherein the type of the parameter value is determined dwMessage Shell_NotifyIcon operation function to be performed, which can be one of three values:

---- NIM_ADD (value 0): Insert an icon in the taskbar status area.

---- NIM_DELETE (value of 1): delete an icon from the taskbar status area.

---- NIM_MODIFY (value of 2): Modify the icon in the taskbar status area, message or notification message.

---- lpData parameter is a type (type structure) of the pointer record, record type NotifyIconData defined as follows:

= Record the NOTIFYICONDATA
the cbSize: DWORD;
Wnd: the HWND;
the uID: UINT;
the uFlags: UINT;
uCallbackMessage: UINT;
the hIcon: HICON;
szTip: Array [0..63] of AnsiChar;
End;
---- the cbSize: recording the NOTIFYICONDATA size.
---- Wnd: the window handle this status area associated icon, this window will handle uCallbackMessage messages.

---- uID: the program icon from the status area defined identifier.

---- uFlags: This field indicates NOTIFYICONDATA record members uCallbackMessage, the value of which items of hIcon and szTip these three valid. Its value may be a combination of the following three (or operation):

---- NIF_MESSAGE (value 1): uCallbackMessage entry contains valid information.

---- NIF_ICON (value 2): hIcon entry contains valid information.

---- NIF_TIP (value 4): szTip entry contains valid information.

---- uCallbackMessage: (32-bit integer) program defines the message identifier. When the mouse moves over the icon or click the status area (ie, a mouse event occurs), the operating system that will send the specified window Wnd uCallbackMessage messages. In uCallbackMessage message, lParam parameter contains a type of mouse Windows message, and wParam parameter contains the icon identification (ie uID). Effective mouse messages include the following: WM_LBUTTONDOWN, WM_RBUTTONDOWN, WM_MBUTTONDOWN, WM_LBUTTONUP, WM_RBUTTONUP, WM_MBUTTONUP, WM_MOUSEMOVE, WM_LBUTTONDBLCLK, WM_RBUTTONDBLCLK and WM_MBUTTONDBLCLK.

---- hIcon: specify an icon handle.

---- szTip: message appears on the icon (less than 63 characters).

---- Delphi to achieve in

---- Through the above description, we can easily see that the programming taskbar status area is mainly deal with two aspects: add, delete, modify icons; and processing the notification message. For adding icons, delete, modify operation can be done by calling Shell_NotifyIcon function. For a customized notification message, we should be dealt with in the message loop.

---- The following example shows the status area is added icons, modify, and delete operations examples, as well as the basic framework for handling notification message icon.

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics,
Controls, Forms, Dialogs, ExtCtrls, ShellAPI;

const
WM_TRAYNOTIFY of WM_USER + =. 1; // custom notification message

type
TForm1 = class(TForm)
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure WndProc(var Msg: TMessage); override;
end;

var
Form1: TForm1;
nd0, nd1:NotifyIconData;
hs:array[0..9]of LongWord;

implementation
{$R *.DFM}

TForm1.FormCreate Procedure (Sender: TObject);
the begin
// load Icon0..Icon9 this 10 icon resource,
and save their handles.
// 0..9 icon Icon0..Icon9 correspond to the nine digits.
HS [0]: = the LoadIcon (hInstance, 'Icon0');
HS [. 1]: = the LoadIcon (hInstance, 'Icon1');
HS [2]: = the LoadIcon (hInstance, 'Icon2');
HS [. 3]: = the LoadIcon (hInstance, 'ICON3');
HS [. 4]: = the LoadIcon (hInstance, 'Icon4');
HS [. 5]: = the LoadIcon (hInstance, 'Icon5');
HS [. 6]: = the LoadIcon (hInstance, 'Icon6');
HS [. 7]: = the LoadIcon (hInstance, 'Icon7');
HS [. 8]: = the LoadIcon (hInstance, 'Icon8');
HS [. 9]: = the LoadIcon (hInstance, 'Icon9');

//填充NotifyIconData记录型变量nd0
nd0.cbSize := sizeof(NotifyIconData);
nd0.Wnd := handle;
nd0.uID := 0;
nd0.uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
nd0.uCallbackMessage := WM_TRAYNOTIFY;
nd0.hIcon := hs[0];
StrPLCopy(nd0.szTip, 'Hello, World!', 63);

//填充NotifyIconData记录型变量nd1
nd1.cbSize := sizeof(NotifyIconData);
nd1.Wnd := handle;
nd1.uID := 1;
nd1.uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
nd1.uCallbackMessage := WM_TRAYNOTIFY;
nd1.hIcon := hs[0];
StrPLCopy(nd1.szTip, 'Simon Loves Daisy', 63);

// add an icon in the taskbar status area
Shell_NotifyIcon (NIM_ADD, @ ND0);
Shell_NotifyIcon (NIM_ADD, @ ND1);
End;

TForm1.Timer1Timer Procedure (Sender: TObject);
var
ST: SystemTime;
the begin
// updated every second icon: the icon displayed ten seconds to 0,
the icon displays a bit number of seconds.
GetLocalTime (ST);
nd0.hIcon: = hs [st.wSecond div 10];
nd1.hIcon: = hs [st.wSecond MOD 10];
// modify the icon in the taskbar status area
Shell_NotifyIcon (NIM_MODIFY, @ nd0);
Shell_NotifyIcon (NIM_MODIFY, @ ND1);
End;

TForm1.FormDestroy Procedure (Sender: TObject);
the begin
// removes the icon from the taskbar status area
Shell_NotifyIcon (NIM_DELETE, @ ND0);
Shell_NotifyIcon (NIM_DELETE, @ ND1);
End;

// processing notification message
Procedure TForm1.WndProc (var Msg: TMessage);
var
IconId: Integer;
Pt: TPoint;
the begin
IF msg.Msg = WM_TRAYNOTIFY the then
the begin
{
in the notification message, wParam parameter uID icon,
the lParam parameter the type of mouse event.
}
The iconID: = msg.wParam;
// Get the mouse position on the screen
GetCursorPos (pt);

// basic framework notification message processing is as follows:
Case msg.lParam of
the WM_LBUTTONDOWN:
the begin
right mouse button is pressed //
End;
WM_RBUTTONDOWN:
the begin
// left mouse button is pressed
End;
the WM_LBUTTONUP:
the begin
// releasing the left mouse key
End;
WM_RBUTTONUP:
the begin
// release the mouse right-
End;
WM_MOUSEMOVE:
the begin
// move the mouse over the icon
End;
WM_LBUTTONDBLCLK:
the begin
// double click
End;
WM_RBUTTONDBLCLK:
the begin
// Double-click the right mouse button
End;
End; // Case End
End
the else // call the parent class WndProc processing other messages
Inherited;
End;

end.

Guess you like

Origin www.cnblogs.com/blogpro/p/11446676.html