首先在Form上放置一个TTrayIcon的控件,Lazarus的话还需要应用windows单元(Linux需要引用lclintf单元),记得给TrayIcon的Icon指定一个图标。其它废话不多说具体核心代码如下:
procedure TForm1.TrayIcon1DblClick(Sender: TObject);
begin
if Application.MainForm <> nil then
begin
Application.Restore;
// Show the form itself
if Application.MainForm.WindowState = wsMinimized then
Application.MainForm.WindowState := wsNormal;
Application.MainForm.Visible := True;
// Bring the main form (or its modal dialog) to the foreground
//这句很重要,不然显示后是最小化在任务栏的,还需要点击一下才能正常显示到屏幕上
SetForegroundWindow(Application.Handle);
TrayIcon1.Visible := False;
end;
end;
procedure TForm1.FormWindowStateChange(Sender: TObject);
begin
if WindowState = TWindowState.wsMinimized then
begin
if Application.MainForm <> nil then
begin
Application.MainForm.Visible := False;
HideTaskbarIcon;
end;
TrayIcon1.Visible := True;
end;
end;
procedure TForm1.HideTaskbarIcon;
begin
if IsWindowVisible(Application.Handle) then
ShowWindow(Application.Handle, SW_HIDE);
end;
procedure TForm1.ShowTaskbarIcon;
begin
if not IsWindowVisible(Application.Handle) then
ShowWindow(Application.Handle, SW_SHOW);
end;
还不快抢沙发