启动程序时如何才可以最小化到托盘区,单击窗体最小化时,可以最小化到托盘区(状态栏不显示).
下面的的一些代码:
unit FrmTest;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,ShellAPI;const
WM_NOTIFYICON=WM_USER+1;type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
// lN:TNotifyIcon;
FNotifyIcon :TIcon;
//系统最小化时显示小图标
procedure WMSysCommand(var Msg: TWMSysCommand); message WM_SYSCOMMAND;
//响应系统消息;
procedure WMNotifyIcon(var Msg: TMessage); message WM_NOTIFYICON;
public
Procedure AddNotifyIcon;
procedure DeleteNotifyIcon;
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.AddNotifyIcon;
var
lIconData:NotifyIconDataA;
begin
lIconData.cbSize :=SizeOf(lIconData);
lIconData.Wnd := self.Handle;
lIconData.hIcon := FNotifyIcon.Handle;
lIconData.uCallbackMessage := WM_NOTIFYICON;
lIconData.uID := 0;
lIconData.szTip := '短信发送' ;
lIconData.uFlags := NIF_MESSAGE OR NIF_ICON OR NIF_TIP;
Shell_NotifyIcon(NIM_ADD,@lIconData);
end;procedure TForm1.DeleteNotifyIcon;
var
lIconData:NotifyIconDataA;
begin
lIconData.cbSize :=SizeOf(lIconData);
lIconData.Wnd := self.Handle;
lIconData.uID := 0;
Shell_NotifyIcon(NIM_DELETE,@lIconData);
end;procedure TForm1.FormCreate(Sender: TObject);
begin
FNotifyIcon := TIcon.Create;
FNotifyIcon.LoadFromFile('TECHNLGY.ICO');
AddNotifyIcon;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
DeleteNotifyIcon;
FNotifyIcon.Free;
end;procedure TForm1.WMSysCommand(var Msg: TWMSysCommand);
begin
inherited;
if Msg.CmdType = SC_MINIMIZE then
begin
self.Visible:=false;
//AddNotifyIcon;
end;
end;procedure TForm1.WMNotifyIcon(var Msg: TMessage);
begin
if Msg.LParam=WM_LBUTTONDBLCLK then
begin
SELF.Visible:=TRUE;
Application.Restore;
Application.BringToFront;
end;
end;end.
下面的的一些代码:
unit FrmTest;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,ShellAPI;const
WM_NOTIFYICON=WM_USER+1;type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
// lN:TNotifyIcon;
FNotifyIcon :TIcon;
//系统最小化时显示小图标
procedure WMSysCommand(var Msg: TWMSysCommand); message WM_SYSCOMMAND;
//响应系统消息;
procedure WMNotifyIcon(var Msg: TMessage); message WM_NOTIFYICON;
public
Procedure AddNotifyIcon;
procedure DeleteNotifyIcon;
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.AddNotifyIcon;
var
lIconData:NotifyIconDataA;
begin
lIconData.cbSize :=SizeOf(lIconData);
lIconData.Wnd := self.Handle;
lIconData.hIcon := FNotifyIcon.Handle;
lIconData.uCallbackMessage := WM_NOTIFYICON;
lIconData.uID := 0;
lIconData.szTip := '短信发送' ;
lIconData.uFlags := NIF_MESSAGE OR NIF_ICON OR NIF_TIP;
Shell_NotifyIcon(NIM_ADD,@lIconData);
end;procedure TForm1.DeleteNotifyIcon;
var
lIconData:NotifyIconDataA;
begin
lIconData.cbSize :=SizeOf(lIconData);
lIconData.Wnd := self.Handle;
lIconData.uID := 0;
Shell_NotifyIcon(NIM_DELETE,@lIconData);
end;procedure TForm1.FormCreate(Sender: TObject);
begin
FNotifyIcon := TIcon.Create;
FNotifyIcon.LoadFromFile('TECHNLGY.ICO');
AddNotifyIcon;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
DeleteNotifyIcon;
FNotifyIcon.Free;
end;procedure TForm1.WMSysCommand(var Msg: TWMSysCommand);
begin
inherited;
if Msg.CmdType = SC_MINIMIZE then
begin
self.Visible:=false;
//AddNotifyIcon;
end;
end;procedure TForm1.WMNotifyIcon(var Msg: TMessage);
begin
if Msg.LParam=WM_LBUTTONDBLCLK then
begin
SELF.Visible:=TRUE;
Application.Restore;
Application.BringToFront;
end;
end;end.
ShowWindow(Application.handle,SW_HIDE);
在 dpr 中加一句 Application.ShowMainForm := false; 如:
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.ShowMainForm := false;
Application.Run;同时要求主窗体的Visible属性为 false。——————————————————————————————————
单击窗体最小化时,可以最小化到托盘区(状态栏不显示):
最小化事件就不说了,这条语句:
ShowWindow( Application.Handle, SW_HIDE );
可以让任务栏不显示。