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.