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.
复制代码

 

posted on   癫狂编程  阅读(574)  评论(0编辑  收藏  举报

编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
历史上的今天:
2019-03-31 delphi中 dataset容易出错的地方
2019-03-31 Ehlib(Delphi控件) v9.2.024 D7-XE10.2 免费绿色特别版
2019-03-31 Delphi (Library Path Browsing Path)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
好的代码像粥一样,都是用时间熬出来的
点击右上角即可分享
微信分享提示