{$R *.dfm}
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 message:TMessage); //处理鼠标在指示状态栏对应的图标上的单击事件
var
p:TPoint;
begin //如果按下的是鼠标左键,并且允许显示,则显示FORM2
if((message.lParam=WM_LBUTTONDOWN) and (Status=True)) then
Form2.Show;
//如果按下的是鼠标右键,则显示弹出菜单
if(message.LParam=WM_RBUTTONDOWN) then
begin
GetCursorPos(p);
PopupMenu1.Popup(p.x,p.y);
end;
end;
procedure TForm1.InstallIcon; //安装图标
var
IconData:TNotifyIconData;
begin
NormalIcon:=TIcon.Create;
DisabledIcon:=TIcon.Create;
NormalIcon.LoadFromFile('norma1.ico');//允许使用图标
DisabledIcon.LoadFromFile('norma1.ico');//禁止使用图标
IconData.cbSize:=SizeOf(IconData);
IconData.Wnd:=Handle;
IconData.uID:=ICON_ID;
IconData.uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;
IconData.uCallBackMessage:=MI_ICONEVENT;
IconData.hIcon:=NormalIcon.Handle;
IconData.szTip:='这只是一个例子';
Shell_NotifyIcon(NIM_ADD,@IconData);
end;
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.add1Click(Sender:TObject);
begin
end; }
{procedure TForm1.enable1Click(Sender:TObject);
begin
if (Status=True) then
begin
ChangeIcon(false);
end
else
begin
ChangeIcon(True);
end;
end; }
procedure TForm1.FormActivate(Sender: TObject);
begin
Form1.width:=0;
Form1.Height:=0;
ShowWindow(Application.Handle,SW_HIDE);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
UnInstallIcon;
NormalIcon.Free;
DisabledIcon.Free;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Status:=True;
InstallIcon;//安装图标
ChangeIcon(True);//改变图标为normal状态
ShowWindow(Application.Handle,SW_HIDE);
SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);
end;
procedure TForm1.N1Click(Sender: TObject);
begin
Form2.Show;
end;
procedure TForm1.N2Click(Sender: TObject);
begin
if (Status=True) then
begin
ChangeIcon(false);
end
else
begin
ChangeIcon(True);
end;
end;