在执行程序之前,首先要改变Form1的Icon属性,给Form1装入一个图标,否则在任务栏上会出现一块空白。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,ShellAPI;
const
WM_BARICON=WM_USER+200;
private
procedure WMSysCommand(var
Message: TMessage); message WM_SYSCOMMAND;
procedure WMBarIcon(var
Message:TMessage);message WM_BARICON;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.WMSysCommand
(var Message:TMessage);
var
lpData
NotifyIconData;
begin
if Message.WParam = SC_ICON then
begin
//如果用户最小化窗口则将窗口
隐藏并在任务栏上添加图标
lpData := new(PNotifyIconDataA);
lpData.cbSize := 88;
//SizeOf(PNotifyIconDataA);
lpData.Wnd := Form1.Handle;
lpData.hIcon := Form1.Icon.Handle;
lpData.uCallbackMessage := WM_BARICON;
lpData.uID :=0;
lpData.szTip := 'Samples';
lpData.uFlags := NIF_ICON
or NIF_MESSAGE or NIF_TIP;
Shell_NotifyIcon(NIM_ADD,lpData);
dispose(lpData);
Form1.Visible := False;
end
else
begin
//如果是其它的SystemCommand
消息则调用系统缺省处理函数处理之。
DefWindowProc(Form1.Handle,Message.
Msg,Message.WParam,Message.LParam);
end;
//
end;
procedure TForm1.WMBarIcon(var Message:TMessage);
var
lpData
NotifyIconData;
begin
if (Message.LParam = WM_LBUTTONDOWN) then
begin
//如果用户点击任务栏图标则将图标删除并回复窗口。
lpData := new(PNotifyIconDataA);
lpData.cbSize := 88;//SizeOf(PNotifyIconDataA);
lpData.Wnd := Form1.Handle;
lpData.hIcon := Form1.Icon.Handle;
lpData.uCallbackMessage := WM_BARICON;
lpData.uID :=0;
lpData.szTip := 'Samples';
lpData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;
Shell_NotifyIcon(NIM_DELETE,lpData);
dispose(lpData);
Form1.Visible := True;
end;
end;
end.