delphi trayIcon控件,如何实现窗口最小化的时候到系统托盘?


本文整理自网络,侵删。

 trayIcon控件,如何实现窗口最小化的时候到系统托盘?
就象QQ的那样,一点最小化就到右下脚的图标里,点击右下脚图标,又能打开窗体。
应该如何设置属性,或者事件呢?拜托各位大哥……指教一二

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,shellapi, Menus, ExtCtrls;

const
WM_TRAYNOTIFY=10;

type
TForm1 = class(TForm)
Button1: TButton;
PopupMenu1: TPopupMenu;
N1: TMenuItem;
N2: TMenuItem;
Timer1: TTimer;
Button2: TButton;
Button3: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure wmTrayNotify(var Msg:TMessage);message WM_TRAYNOTIFY;
procedure N1Click(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
ChangeFlag: Boolean;
A:Boolean;
Icon: TIcon;
Nid:TNotifyIconData;
{ Private declarations }
public

{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
ChangeFlag := false;
A:= True;
{ Nid.cbSize:=sizeof(TNotifyIconData);
Nid.Wnd:=Handle;
Nid.uID:=1000;
Nid.uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;
Nid.uCallbackMessage:=WM_TRAYNOTIFY;
Nid.hIcon:=Application.Icon.Handle;
Nid.szTip:='我隐藏在系统托盘里了';
Shell_NotifyIcon(NIM_ADD,@Nid); }
Icon:= TIcon.Create;
Icon.LoadFromFile('C:\Program Files\Microsoft Office\OFFICE11\MSN.ICO');

end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
Shell_NotifyIcon(NIM_DELETE,@Nid);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Nid.cbSize:=sizeof(TNotifyIconData);
Nid.Wnd:=Handle;
Nid.uID:=1000;
Nid.uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;
Nid.uCallbackMessage:=WM_TRAYNOTIFY;
Nid.hIcon:=Application.Icon.Handle;
Nid.szTip:='我隐藏在系统托盘里了';
Shell_NotifyIcon(NIM_ADD,@Nid);
ShowWindow(Application.Handle,SW_SHOWMINIMIZED);
ShowWindow(Application.Handle,SW_HIDE);
Timer1.Enabled:= True;
end;

procedure TForm1.wmTrayNotify(var Msg: TMessage);
var
p:TPoint;
begin
if(Msg.lparam=WM_LBUTTONDBLCLK) then
begin
ShowWindow(Application.Handle,SW_SHOW);
Shell_NotifyIcon(NIM_DELETE,@Nid);
Timer1.Enabled:= False;
Application.Restore;
end
else if(Msg.LParam=WM_RButtonUp) then
begin
GetCursorPos(P);//获得鼠标坐标
PopupMenu1.Popup(P.X, P.Y);//在鼠标光标处显示弹出菜单
end;
end;

procedure TForm1.N1Click(Sender: TObject);
begin
ShowWindow(Application.Handle,SW_SHOW);
Shell_NotifyIcon(NIM_DELETE,@Nid);
Application.Restore;
Timer1.Enabled:= False;
end;

procedure TForm1.N2Click(Sender: TObject);
begin
Application.Terminate;
end;

procedure TForm1.FormShow(Sender: TObject);
begin

Timer1.Enabled:= False;
end;

procedure TForm1.Button2Click(Sender: TObject);

begin
ChangeFlag:= True;
{ Icon:= TIcon.Create;
Icon.LoadFromFile('C:\Program Files\Microsoft Office\OFFICE11\MSN.ICO');
Nid.cbSize:=sizeof(TNotifyIconData);
Nid.Wnd:=Handle;
Nid.uID:=1000;
Nid.uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;
Nid.uCallbackMessage:=WM_TRAYNOTIFY;
Nid.hIcon:=Icon.Handle;
Shell_NotifyIcon(NIM_MODIFY,@Nid);}
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
if not ChangeFlag then
Exit;
if A then
begin
Nid.hIcon:= Icon.Handle;
Shell_NotifyIcon(NIM_MODIFY,@Nid);
A:= False;
end
else begin
Nid.hIcon:= Application.Icon.Handle;
Shell_NotifyIcon(NIM_MODIFY,@Nid);
A:= True;
end;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
ChangeFlag:= False;
end;

end.

相关阅读 >>

Delphi利用系统时间产生随机数的函数

Delphi 10中实现android的广播接收器

Delphi 程序嵌入桌面效果的实现

android : assets与res/raw资源目录的区别

Delphi搜索字符串在流中的位置

Delphi ^ 符号的特殊用法

Delphi执行js

Delphi firedac 另存json

Delphi 向windows窗口发送alt组合键的问题

Delphi xe5 android 使用system.zip单元释放资源文件

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...