我已经写大部分代码了。不想再去下载控件!以下是我的代码,我想在点击窗口标题的‘X’按钮时,不关闭窗口,而最小化窗口。怎么搞呀?
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ShellApi, Menus;
const
ICON_ID = 100; //ICON的ID标志 (随便定义)
{自定义消息,当小图标捕捉到鼠标事件时Windows向回调函数发送此消息}
MY_ICONEVENT = WM_USER + 100;//自定义ICON事件消息
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
PopupMenu1: TPopupMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
Edit1: TEdit;
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure N1Click(Sender: TObject);
procedure N2Click(Sender: TObject);
private
{ Private declarations }
NormalIcon,DisabledIcon:TIcon; //正常和失效两种情况下图标
Status:Boolean;//定义标志是“允许使用”还是“禁止使用”
Procedure InstallIcon;//声明过程InstallIcon用来在托盘中安装图标
Procedure ChangeIcon(s:Boolean);//声明过程ChangeIcon用于改变本程序以托盘中的图标
Procedure UnInstallIcon;//声明过程UnInstallIcon用于卸载托盘中的图标
//捕捉自定义消息MY_ICONEVENT的过程IconOnClick的声明
Procedure IconOnClick(var Msg:Tmessage);message MY_ICONEVENT;
procedure OnMin(var msg : TMessage);message WM_SYSCOMMAND;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button2Click(Sender: TObject);
begin
Close;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
UnInstallIcon;
//NormalIcon.Free;
//DisabledIcon.Free;
Release;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage('系统托盘程序!');
//SendMessage(Edit1.Handle,EM_LINESCROLL,5,1);
end;
Procedure TForm1.ChangeIcon(s:Boolean);//过程ChangeIcon用于改变本程序以托盘中的图标
var
IconData:TNotifyIconData;
begin
IconData.cbSize := SizeOf(IconData);
IconData.Wnd := Handle;
IconData.uID := ICON_ID;
if s=False then //禁止使用
begin
IconData.hIcon := DisabledIcon.Handle;
Status := False;
end
else
begin
IconData.hIcon := Normalicon.Handle;
Status := True;
end;
IconData.uFlags := NIF_ICON;
Shell_NotifyIcon(NIM_MODIFY,@IconData);
end;
//处理鼠标在托盘中对应的图标上的单击事件
Procedure TForm1.IconOnClick(var Msg:Tmessage);
var
p:TPoint;
begin
if ((Msg.lParam=WM_LBUTTONDOWN) and (Status=True)) then
//如果按下的是鼠标左键,并且允许显示,则显示窗体
begin
Form1.Visible := True;
SetForegroundWindow(Handle);
end;
if (Msg.lParam=WM_RBUTTONDOWN) then
//如果按下的是鼠标右键,则显示弹出菜单
begin
GetCursorPos(p);
PopupMenu1.Popup(p.x,p.y);
//ShowWindow(Application.Handle,SW_SHOWNORMAL );
end;
end;
//过程InstallIcon用来在托盘中安装图标
Procedure TForm1.InstallIcon;
var
IconData:TNotifyIconData;
begin
NormalIcon := TIcon.Create;
DisabledIcon := TIcon.Create;
//允许状态下的图标
//NormalIcon := application.Icon;
NormalIcon.LoadFromFile('pictureg.ico');
//禁止状态下的图标
DisabledIcon.LoadFromFile('pictureg.ico');
//DisabledIcon := application.Icon;
IconData.cbSize := SizeOf(IconData);
IconData.Wnd := Handle;
IconData.UID := ICON_ID;
IconData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;
IconData.uCallBackMessage := MY_ICONEVENT;
//IconData.hIcon := NormalIcon.Handle;
IconData.hIcon := application.Icon.Handle;
//鼠标悬在托盘中对应的图标上时的提示信息
IConData.szTip := '测试';
Shell_NotifyIcon(NIM_ADD,@IconData);
end;
//过程UnInstallIcon用于卸载托盘中的图标
Procedure TForm1.UnInstallIcon;
var
Icondata : TNotifyIconData;
begin
IconData.cbSize := SizeOf(IconData);
IconData.Wnd := Handle;
IconData.uID := ICON_ID;
Shell_NotifyIcon(NIM_DELETE,@IconData);
end;
procedure TForm1.OnMin(var msg: TMessage);
begin
if (msg.WParam = SC_MINIMIZE) then
begin
Status := True;
//安装图标
InstallIcon;
//改变图标为Normal状态
//ChangeIcon(true);
//隐藏主窗体
ShowWindow(Application.Handle,SW_HIDE);
//在托盘中不显示程序图标
SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);
Form1.Visible := False;
end;
inherited;
end;
procedure TForm1.FormActivate(Sender: TObject);
begin
//Form1.width := 0;
//Form1.Height := 0;
//ShowWindow(Application.Handle,SW_HIDE);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Status := True;
//安装图标
InstallIcon;
//改变图标为Normal状态
//ChangeIcon(true);
//隐藏主窗体
ShowWindow(Application.Handle,SW_HIDE);
//在托盘中不显示程序图标
SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);
Form1.Visible := False;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
//Form1 := TForm1.CreateParented(GetDesktopWindow);
//form1.Brush.Style:=bsclear;
end;
procedure TForm1.N1Click(Sender: TObject);
begin
Close;
end;
procedure TForm1.N2Click(Sender: TObject);
begin
Form1.Visible := true;
//ShowWindow(Application.Handle,SW_RESTORE);
SetForegroundWindow(Handle);
end;
end.