type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormPaint(Sender: TObject);
private
procedure OnIconNotify(var Message: TMessage);
message MY_MESSAGE;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
{当小图标捕捉到鼠标事件时进入此过程}
{当小图标捕捉到鼠标事件时进入此过程}
procedure TForm1.OnIconNotify(var Message: TMessage);
const
Busy: Boolean = false;
begin
if not Busy then begin
Busy := true;
if Message.LParam=WM_LBUTTONDOWN then
if Application.MessageBox('Are you sure',
'Exit', MB_YESNO)=IDYES then Close;
Busy := false;
end;
end;
nid.szTip := 'This is a test application'; // 提示字符串
nid.uCallbackMessage := MY_MESSAGE; // 回调函数消息
nid.uFlags := NIF_ICON or NIF_TIP or NIF_MESSAGE; // 指明哪些字段有?
if not Shell_NotifyIcon(NIM_ADD, @nid) then begin
ShowMessage('Failed!');
Application.Terminate;
end;
{将程序的窗口样式设为TOOL窗口,可避免在任务条上出现}
SetWindowLong(Application.Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW);
end;
首先安装图标到任务区,需要先定义好任务区图表结构,再用shell_NotifyIcon(nim_add,@IconData);进行安装,这是个例子:
procedure TMainForm.SetupTray;
begin
with IconData do
begin
cbsize:=sizeof(IconData);
Wnd:=self.Handle;
uId:=0;
uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;
uCallBackMessage:=WM_TrayCallBack;
hIcon:=loadIcon(hInstance,'MAINICON');
szTip:='资料收集器';
end;
shell_NotifyIcon(nim_add,@IconData);
end;
另外你为了让它只出现在任务栏还需要隐藏下面的标题栏
隐藏窗口就用form.visible:=false就可以了,除此之外还有
ShowWindow(application.Handle,SW_HIDE);以彻底隐藏整个程序
至于怎样在最小化的时候实现,你需要拦截WM_SYSCOMMAND消息进行处理,下面是一个例子:
procedure TMainForm.OnTrayForm(var msg: Tmessage);//message WM_SYSCOMMAND
begin
if (msg.WParam=SC_MINIMIZE) and (MIN_TOTRAY in TrayIconWay) then
begin
MainForm.Hide;
ShowWindow(application.Handle,SW_HIDE);//隐藏任务拦
TrayPopMenu.Items.Items[0].Checked:=false;
end
else if (msg.WParam=SC_CLOSE) and (CLOSE_TOTRAY in TrayIconWay) then
begin
MainForm.Hide;
ShowWindow(application.Handle,SW_HIDE);
TrayPopMenu.Items.Items[0].Checked:=false;
end
else inherited;
end;
说的已经够详细了,可以看看书