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

时间:2012-08-05 21:09:56

标签: delphi delphi-xe2

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

Solution 01

Solution 02

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

4 个答案:

答案 0 :(得分:20)

这应该让你去。在表单上放置TTrayIconTApplicationEvents。以下代码来自docwikiTTrayIcon - Delphi Example。使用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;

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

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

答案 1 :(得分:5)

我将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;

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

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

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

答案 2 :(得分:2)

...在Delphi 6中,不存在TTrayIcon,您可以使用这个简单的代码:

unit MainUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ShellAPI, StdCtrls, Menus;

const WM_ICONTRAY = WM_USER+1;

type
  TForm1 = class(TForm)
    PopupMenu1: TPopupMenu;
    ShowForm1: TMenuItem;
    HideForm1: TMenuItem;
    Exit1: TMenuItem;
    procedure TrayMessage(var Msg: TMessage); message WM_ICONTRAY;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ShowForm1Click(Sender: TObject);
    procedure HideForm1Click(Sender: TObject);
    procedure Exit1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
   TrayIconData: TNotifyIconData;
  end;

var
  Form1: TForm1;
  MustExit:boolean;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  MustExit:=false;
  TrayIconData.cbSize:=SizeOf(TrayIconData);
  TrayIconData.Wnd:=Handle;
  TrayIconData.uID:=0;
  TrayIconData.uFlags:=NIF_MESSAGE + NIF_ICON + NIF_TIP;
  TrayIconData.uCallbackMessage:=WM_ICONTRAY;
  TrayIconData.hIcon:=Application.Icon.Handle;
  StrPCopy(TrayIconData.szTip,Application.Title);
  Shell_NotifyIcon(NIM_ADD, @TrayIconData);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Shell_NotifyIcon(NIM_DELETE, @TrayIconData);
end;

procedure TForm1.TrayMessage(var Msg: TMessage);
var p:TPoint;
begin
  case Msg.lParam of
    WM_LBUTTONDOWN: begin
                     Form1.Show;
                     Application.Restore;
                    end;
    WM_RBUTTONDOWN: begin
                     GetCursorPos(p);
                     PopUpMenu1.Popup(p.x,p.y);
                    end;
  end;
end;

// Popup "Form Show" menu item OnClick 
procedure TForm1.ShowForm1Click(Sender: TObject);
begin
 Form1.Show;
end;

// Popup "Form Hide" menu item OnClick    
procedure TForm1.HideForm1Click(Sender: TObject);
begin
 Form1.Hide;
end;

// Popup "Exit" menu item OnClick
procedure TForm1.Exit1Click(Sender: TObject);
begin
 MustExit:=true;
 Close;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 if MustExit then exit;
 Form1.Hide;
 Action:=caNone;
end;

end.

答案 3 :(得分:1)

我已实施以下代码。除了一个,一切都很好。最小化Form后,它会转到“SystemTray”,但也可以在“TaskBar”中找到。对于我的应用程序,“Form001”的“AlphaBlend”属性为true,“AlphaBlendValue”为“0”。

unit KoushikHalder001;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, Vcl.ExtCtrls, Vcl.Imaging.pngimage,
  Vcl.AppEvnts, Vcl.ImgList, Vcl.Menus;

type
  TForm001 = class(TForm)
    Edit001: TEdit;
    Background: TImage;
    BitBtn001: TBitBtn;
    BitBtn002: TBitBtn;
    FadeInTimer: TTimer;
    FadeOutTimer: TTimer;
    FormMinimizeTimer: TTimer;
    FormRestoreTimer: TTimer;
    TrayIcon: TTrayIcon;
    PopupMenu: TPopupMenu;
    ImageList: TImageList;
    ApplicationEvents: TApplicationEvents;
    Form001Close: TMenuItem;
    Form001Hide: TMenuItem;
    Form001Show: TMenuItem;
    Form002Close: TMenuItem;
    Form002Hide: TMenuItem;
    Form002Show: TMenuItem;
    N01: TMenuItem;
    N02: TMenuItem;
    N03: TMenuItem;
    N04: TMenuItem;
    N05: TMenuItem;
    N06: TMenuItem;
    N07: TMenuItem;
    N08: TMenuItem;
    N09: TMenuItem;
    N10: TMenuItem;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormCreate(Sender: TObject);
    procedure FormHide(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure BitBtn001Click(Sender: TObject);
    procedure BitBtn002Click(Sender: TObject);
    procedure FadeInTimerTimer(Sender: TObject);
    procedure FadeOutTimerTimer(Sender: TObject);
    procedure FormMinimizeTimerTimer(Sender: TObject);
    procedure FormRestoreTimerTimer(Sender: TObject);
    procedure ApplicationEventsMinimize(Sender: TObject);
    procedure TrayIconDblClick(Sender: TObject);
    procedure Form001CloseClick(Sender: TObject);
    procedure Form001HideClick(Sender: TObject);
    procedure Form001ShowClick(Sender: TObject);
    procedure Form002CloseClick(Sender: TObject);
    procedure Form002HideClick(Sender: TObject);
    procedure Form002ShowClick(Sender: TObject);
  private
    { Private declarations }
    CrossButtonClick: Boolean;
    procedure WMNCHitTest(var Msg: TWMNCHitTest) ; message WM_NCHitTest;
    procedure WMSysCommand(var Msg: TWMSysCommand) ; message WM_SysCommand;
  public
    { Public declarations }
  end;

var
  Form001: TForm001;

implementation

{$R *.dfm}

uses KoushikHalder002;


procedure TForm001.WMNCHitTest(var Msg: TWMNCHitTest);
begin
  inherited;
  if ControlAtPos(ScreenToClient(Msg.Pos), True, True, True)= nil
    then
      begin
        if Msg.Result=htClient then Msg.Result := htCaption;
      end;
end;

procedure TForm001.WMSysCommand(var Msg: TWMSysCommand);
begin
  case Msg.CmdType of
    SC_MINIMIZE:
      begin
        if Form001.AlphaBlendValue > 0 then
          begin
            Form001.FormMinimizeTimer.Enabled := true;
            Exit;
          end;
      end;
    SC_RESTORE:
      begin
        if Form001.AlphaBlendValue < 220 then
          begin
            Form001.FormRestoreTimer.Enabled := True;
          end;
      end;
  end;
  inherited;
end;

procedure TForm001.ApplicationEventsMinimize(Sender: TObject);
begin
  Form001.FormMinimizeTimer.Enabled := true;
  TrayIcon.Visible := True;
  TrayIcon.Animate := True;
  TrayIcon.ShowBalloonHint;
end;

procedure TForm001.BitBtn001Click(Sender: TObject);
begin
  if Form002.WindowState = wsMinimized then
    begin
      Form002.Perform(WM_SYSCOMMAND, SC_RESTORE, 0);
    end
    else
    Form002.show;
end;

procedure TForm001.BitBtn002Click(Sender: TObject);
begin
  Form002.FadeOutTimer.Enabled := true;
  Form001.FadeOutTimer.Enabled := true;
end;

procedure TForm001.Form001CloseClick(Sender: TObject);
begin
  Form002.FadeOutTimer.Enabled := true;
  Form001.FadeOutTimer.Enabled := true;
end;

procedure TForm001.Form001HideClick(Sender: TObject);
begin
Form001.FormMinimizeTimer.Enabled := true;
end;

procedure TForm001.Form001ShowClick(Sender: TObject);
begin
  if Form001.WindowState = wsMinimized then
    begin
      Form001.Perform(WM_SYSCOMMAND, SC_RESTORE, 0);
    end
    else
    Form001.show;
end;

procedure TForm001.Form002CloseClick(Sender: TObject);
begin
Form002.FadeOutTimer.Enabled := true;
end;

procedure TForm001.Form002HideClick(Sender: TObject);
begin
Form002.FormMinimizeTimer.Enabled := true;
end;

procedure TForm001.Form002ShowClick(Sender: TObject);
begin
  if Form002.WindowState = wsMinimized then
    begin
      Form002.Perform(WM_SYSCOMMAND, SC_RESTORE, 0);
    end
    else
    Form002.show;
end;

procedure TForm001.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Form001.FadeOutTimer.Enabled := true;
end;

procedure TForm001.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  if CrossButtonClick = true
    then
      begin
        CanClose := true;
        Exit;
      end;
  CanClose := false;
  Form001.FadeOutTimer.Enabled := true;
end;

procedure TForm001.FormCreate(Sender: TObject);
begin
  Form001.FadeInTimer.Enabled := true;
end;

procedure TForm001.FormHide(Sender: TObject);
begin
  Form001.FadeOutTimer.Enabled := true;
end;

procedure TForm001.FormShow(Sender: TObject);
begin
  Form001.FadeInTimer.Enabled := true;
end;
procedure TForm001.TrayIconDblClick(Sender: TObject);
begin
  Form001.FormRestoreTimer.Enabled := true;
  TrayIcon.Visible := False;
  WindowState := wsNormal;
  Application.BringToFront();
end;

procedure TForm001.FadeInTimerTimer(Sender: TObject);
begin
  if Form001.AlphaBlendValue >= 220
    then
      begin
        Form001.FadeInTimer.Enabled := false;
      end
    else
      begin
        Form001.AlphaBlendValue := Form001.AlphaBlendValue + 10;
        CrossButtonClick := false;
      end;
end;

procedure TForm001.FadeOutTimerTimer(Sender: TObject);
begin
  if Form001.AlphaBlendValue <= 0
    then
      begin
        Form001.FadeOutTimer.Enabled := false;
        CrossButtonClick := true;
        Self.Close;
      end
    else
      begin
        Form001.AlphaBlendValue := Form001.AlphaBlendValue - 10;
        CrossButtonClick := true;
      end;
end;

procedure TForm001.FormMinimizeTimerTimer(Sender: TObject);
begin
  if Form001.AlphaBlendValue > 0 then
    begin
      Form001.AlphaBlendValue := Form001.AlphaBlendValue - 10;
    end
  else
    begin
      Form001.FormMinimizeTimer.Enabled := false;
      Perform(WM_SYSCOMMAND, SC_MINIMIZE, 0);
    end;
end;

procedure TForm001.FormRestoreTimerTimer(Sender: TObject);
begin
  if Form001.AlphaBlendValue < 220 then
    begin
      Form001.AlphaBlendValue := Form001.AlphaBlendValue + 10;
    end
  else
    begin
      Form001.FormRestoreTimer.Enabled := false;
    end;
end;

end.

如果我执行以下操作

  Application.MainFormOnTaskbar := false;

表格完全无形。我认为应该有一个bug。但我无法找到它。