将Delphi表单最小化到系统托盘

Rub*_*der 12 delphi delphi-xe2

我是德尔福学习者.我正在寻找解决方案,以便将Delphi MainForm最小化到系统托盘而不是任务栏.在右键单击系统托盘图标应该有一些菜单,如"恢复"和"关于"和"帮助"等.系统托盘图标将从Imagelis1加载,它将动画.单击"恢复"将恢复MainForm,点击"关于""Form2"将被恢复,点击"帮助""Foprm3"将被恢复.我在互联网上找到了这么多解决方案:

解决方案01

解决方案02

但每种解决方案都有一些缺点.有些可以做一次.有些人在Windows7中模糊了图标.有人可能会告诉我没有人为我编写代码,我必须显示我的代码.Plaese原谅我这个问候.请给我具体的解决方案,它可以在没有Windows版本依赖的情况下实现.它会帮助每一个人.请帮我.

Ken*_*ite 23

这应该让你去.删除表单上的a TTrayIcon和a TApplicationEvents.以下代码TTrayIcon - Delphi Example来自docwiki.使用IDE主菜单,然后选择Project->View Source,以及读取Application.ShowMainFormOnTaskbar := True;到"Application.ShowMainFormOnTaskbar:= False;"的行.保持应用程序的按钮不会出现在Windows任务栏上.

此示例在窗体上使用托盘图标和应用程序事件组件.当应用程序运行时,它会加载托盘图标,动画时显示的图标,还会设置提示气球.最小化窗口时,窗体将隐藏,显示提示气球,并显示托盘图标并设置动画.双击系统托盘图标可恢复窗口.

// Add this to the `TApplicationEvents.OnMinimize` event handler
procedure TForm1.ApplicationEvents1Minimize(Sender: TObject);
begin
  { Hide the window and set its state variable to wsMinimized. }
  Hide();
  WindowState := wsMinimized;

  { Show the animated tray icon and also a hint balloon. }
  TrayIcon1.Visible := True;
  TrayIcon1.Animate := True;
  TrayIcon1.ShowBalloonHint;
end;

// Add this to the `TForm.OnCreate` event handler
procedure TForm1.FormCreate(Sender: TObject);
var
  MyIcon : TIcon;
begin
  { Load the tray icons. }
  TrayIcon1.Icons := TImageList.Create(Self);
  MyIcon := TIcon.Create;
  MyIcon.LoadFromFile('icons/earth1.ico');
  TrayIcon1.Icon.Assign(MyIcon);
  TrayIcon1.Icons.AddIcon(MyIcon);

  MyIcon.LoadFromFile('icons/earth2.ico');
  TrayIcon1.Icons.AddIcon(MyIcon);
  MyIcon.LoadFromFile('icons/earth3.ico');
  TrayIcon1.Icons.AddIcon(MyIcon);
  MyIcon.LoadFromFile('icons/earth4.ico');
  TrayIcon1.Icons.AddIcon(MyIcon);

  { Set up a hint message and the animation interval. }
  TrayIcon1.Hint := 'Hello World!';
  TrayIcon1.AnimateInterval := 200;

  { Set up a hint balloon. }
  TrayIcon1.BalloonTitle := 'Restoring the window.';
  TrayIcon1.BalloonHint :=
    'Double click the system tray icon to restore the window.';
  TrayIcon1.BalloonFlags := bfInfo;
end;

// Add this to the `TTrayIcon.OnDoubleClick` event handler
procedure TForm1.TrayIcon1DblClick(Sender: TObject);
begin
  { Hide the tray icon and show the window,
  setting its state property to wsNormal. }
  TrayIcon1.Visible := False;
  Show();
  WindowState := wsNormal;
  Application.BringToFront();
end;
Run Code Online (Sandbox Code Playgroud)

对于右键单击的菜单TPopupMenu,在表单中添加一个,在其上添加所需的项目,像往常一样为这些项目编写事件处理程序,然后将其分配PopupMenuTrayIcon.PopupMenu属性.

"模糊图标"是由于您没有使用正确的图标大小而导致Windows被迫缩放(拉伸)它们.使用图标编辑器为每个图标创建多个大小的图像(一个图标文件中可以有多个大小).


小智 7

我将TrayIcon放到myForm上,然后添加这个简单的代码:

type
  TmyForm = class(TForm)
    ...
    TrayIcon: TTrayIcon;
    procedure FormCreate(Sender: TObject);
    ...
    procedure TrayIconClick(Sender: TObject);
    ...
  private
    { Private declarations }
    procedure OnMinimize(Sender:TObject);
  public
    { Public declarations }
  end;

procedure TmyForm.FormCreate(Sender: TObject);
begin // When form is created
     Application.OnMinimize:=OnMinimize; // Set the event handler for application minimize
end;

procedure TmyForm.OnMinimize(Sender:TObject);
begin // When application is minimized by user and/or by code
     Hide; // This is to hide it from taskbar
end;

procedure TmyForm.TrayIconClick(Sender: TObject);
begin // When clicking on TrayIcon
     if Visible
     then begin // Application is visible, so minimize it to TrayIcon
               Application.Minimize; // This is to minimize the whole application
          end
     else begin // Application is not visible, so show it
               Show; // This is to show it from taskbar
               Application.Restore; // This is to restore the whole application
          end;
end;
Run Code Online (Sandbox Code Playgroud)

这会创建一个可见的TrayIcon,当您点击它时:

  • 如果应用程序是可见的,它将是隐藏形式任务栏和屏幕
  • 如果应用程序是隐藏的,它将显示为表单任务栏和屏幕

换句话说,单击TrayIcon应用程序将改变其可见性; 就像将它最小化到TrayIcon吧.