You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Windows通知显示应用标题而非EXE文件名?

问题描述

我用以下代码发送Windows通知,功能正常,但通知窗口的标题显示的是EXE文件名,而非我在Application.Title中定义的应用名称。我希望通知标题显示'My application',但实际显示的是'notif'。

项目主程序源码

program notif;

uses
  Vcl.Forms,
  unit1 in '..\unit1.pas' {Form1};

{$R *.res}

begin
  Application.Initialize;
  Application.Title := 'My application';
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

单元文件源码(unit1)

unit unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    NotificationCenter1: TNotificationCenter;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  MyNotification: TNotification; //Defines a TNotification variable
  begin
  MyNotification := NotificationCenter1.CreateNotification; //Creates the notification
  try
    MyNotification.Name := 'My application name'; //Defines the name of the notification.
    MyNotification.Title := 'Title'; //Defines the name that appears when the notification is presented.
    MyNotification.AlertBody := 'Hello, im here'; //Defines the body of the notification that appears below the title.

    NotificationCenter1.PresentNotification(MyNotification); //Presents the notification on the screen.
  finally
    MyNotification.Free; //Frees the variable
  end;
end;

end.
解决方法

Windows通知的应用标题不会直接读取Application.Title,而是优先读取程序版本资源中的描述信息,或者TNotificationCenter的指定属性。以下是两种可行方案:

方案1:修改程序版本资源

Windows通知会自动读取程序版本资源里的FileDescription或ProductName作为应用标题,步骤如下:

  1. 打开Delphi菜单 Project > Resources and Images
  2. 添加一个版本资源文件(比如version.rc),内容示例如下:
1 VERSIONINFO
FILEVERSION 1,0,0,0
PRODUCTVERSION 1,0,0,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
#else
FILEFLAGS 0x0L
#endif
FILEOS 0x4L
FILETYPE 0x1L
FILESUBTYPE 0x0L
BEGIN
    BLOCK "StringFileInfo"
    BEGIN
        BLOCK "040904B0"
        BEGIN
            VALUE "CompanyName", "你的公司名称"
            VALUE "FileDescription", "My application"
            VALUE "ProductName", "My application"
            VALUE "ProductVersion", "1.0.0.0"
        END
    END
    BLOCK "VarFileInfo"
    BEGIN
        VALUE "Translation", 0x409, 1200
    END
END
  1. 将该资源文件添加到项目中,重新编译程序即可。

方案2:设置TNotificationCenter的ApplicationName属性

如果你的Delphi版本支持,直接设置NotificationCenter1的ApplicationName属性,就能指定通知显示的应用名称:
在Form的创建事件中添加代码:

procedure TForm1.FormCreate(Sender: TObject);
begin
  NotificationCenter1.ApplicationName := 'My application';
end;

设置后,通知的应用标题就会显示为你指定的内容,无需修改资源文件。

注意:旧版本Delphi的TNotificationCenter可能没有ApplicationName属性,此时只能使用方案1。

内容的提问来源于stack exchange,提问作者delphirules

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 11:10:31