TrayIcon不会显示通知

时间:2015-10-15 00:42:12

标签: delphi delphi-xe8 trayicon

我使用此代码显示TrayIcon它有效且TrayIcon出现在Systray中,但它没有显示BalloonHint;我尝试更改某些设置,但仍然没有显示通知

procedure TMainForm.FormCreate(Sender: TObject);
begin
  TrayIcon1.Hint := 'Hint';
  TrayIcon1.AnimateInterval := 200;
  TrayIcon1.BalloonTitle := 'Hint';
  TrayIcon1.BalloonHint := 'Double click to restore Application';
  TrayIcon1.BalloonFlags := bfInfo;
end;

procedure TMainForm.ApplicationEvents1Minimize(Sender: TObject);
begin
  Hide();
  WindowState := wsMinimized;
  TrayIcon1.Visible := True;
  TrayIcon1.Animate := True;
  TrayIcon1.ShowBalloonHint;
end;

procedure TMainForm.TrayIcon1DblClick(Sender: TObject);
begin
  TrayIcon1.Visible := False;
  Show();
  WindowState := wsNormal;
  Application.BringToFront();
end;

这段代码有什么问题?

该项目最初是用Delphi7编写的,现在升级到DelphiXE8。

4 个答案:

答案 0 :(得分:3)

代码没有问题。 它似乎是Notification Area Icons的操作系统级设置错误(= Hide icon and notifications)。 打开Control Panel\All Control Panel Items\Notification Area Icons并为您的应用选择Only show notificationsShow icon and notifications

答案 1 :(得分:0)

你需要做一点不同的事情。首先,您需要检查表单何时最小化。这可以通过捕获消息WM_SIZE

来完成
procedure WMSize(var Msg: TMessage); message WM_SIZE;

然后:

procedure TForm1.WMSize(var Msg: TMessage);
begin
  if Msg.WParam  = SIZE_MINIMIZED then ....
end;

通常,您还希望关闭按钮以最小化应用程序。这可以通过实现CloseQuery事件来实现。

我根据您自己的代码为您做了一个小例子。

首先是DFM代码:

object Form11: TForm11
  Left = 0
  Top = 0
  Caption = 'Form11'
  ClientHeight = 299
  ClientWidth = 635
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCloseQuery = FormCloseQuery
  OnCreate = FormCreate
  OnHide = FormHide
  PixelsPerInch = 96
  TextHeight = 13
  object TrayIcon1: TTrayIcon
    OnDblClick = TrayIcon1DblClick
    Left = 312
    Top = 152
  end
  object PopupMenu1: TPopupMenu
    Left = 184
    Top = 112
    object Close1: TMenuItem
      Caption = 'Close'
      Default = True
      OnClick = Close1Click
    end
  end
end

然后是实际的源代码:

unit Unit11;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.Menus;

type
  TForm11 = class(TForm)
    TrayIcon1: TTrayIcon;
    PopupMenu1: TPopupMenu;
    Close1: TMenuItem;
    procedure FormCreate(Sender: TObject);
    procedure FormHide(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure Close1Click(Sender: TObject);
    procedure TrayIcon1DblClick(Sender: TObject);
  private
    AllowClose: Boolean;
  public
    procedure WMSize(var Msg: TMessage); message WM_SIZE;
  end;

var
  Form11: TForm11;

implementation

{$R *.dfm}

procedure TForm11.Close1Click(Sender: TObject);
begin
  AllowClose := True;
  Close;
end;

procedure TForm11.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := AllowClose;
  if not AllowClose then
    Hide;
end;

procedure TForm11.FormCreate(Sender: TObject);
begin
  AllowClose := false;
  TrayIcon1.Hint := 'Hint';
  TrayIcon1.AnimateInterval := 200;
  TrayIcon1.BalloonTitle := 'Hint';
  TrayIcon1.BalloonHint := 'Double click to restore Application';
  TrayIcon1.BalloonFlags := bfInfo;
end;

procedure TForm11.FormHide(Sender: TObject);
begin
  TrayIcon1.Visible := True;
  TrayIcon1.Animate := True;
  TrayIcon1.ShowBalloonHint;
end;

procedure TForm11.TrayIcon1DblClick(Sender: TObject);
begin
  TrayIcon1.Visible := false;
  Show();
  WindowState := wsNormal;
  Application.BringToFront();
end;

procedure TForm11.WMSize(var Msg: TMessage);
begin
  if Msg.WParam = SIZE_MINIMIZED then
    Close;
end;

end.

答案 2 :(得分:0)

为了当前和将来的Internet搜索者,我会说我的问题出在XE5和Windows 10上(在Windows XP中工作正常)。原来,我愚蠢地打开了优先级模式,该模式隐藏了Notifications ..以及trayicon的气球提示。

答案 3 :(得分:-1)

代码没有问题,它看起来有问题 重新安装Windows修复它