Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 如何使用ToastGeneric在delphi中创建toast通知_Windows_Delphi_Notifications_Delphi 10.1 Berlin - Fatal编程技术网

Windows 如何使用ToastGeneric在delphi中创建toast通知

Windows 如何使用ToastGeneric在delphi中创建toast通知,windows,delphi,notifications,delphi-10.1-berlin,Windows,Delphi,Notifications,Delphi 10.1 Berlin,我正在用delphi在桌面上开发。我想用toast泛型通知创建toast通知 LToastFactory.CreateToastNotification(LXMLTemplate) 此外,我正在使用xml,如中所示 我的问题是如何让delphi接受这个xml,我还没有找到一种方法将这个字符串转换成xml\u Dom\u IXmlDocument类型。我也有同样的问题 我的目标是从Delphi创建一个使用toastGeneric的Toast通知。然而,我在任何语言中都找不到任何通过操纵xml来实

我正在用delphi在桌面上开发。我想用toast泛型通知创建toast通知 LToastFactory.CreateToastNotification(LXMLTemplate)

此外,我正在使用xml,如中所示


我的问题是如何让delphi接受这个xml,我还没有找到一种方法将这个字符串转换成xml\u Dom\u IXmlDocument类型。

我也有同样的问题

我的目标是从Delphi创建一个使用toastGeneric的Toast通知。然而,我在任何语言中都找不到任何通过操纵xml来实现这一点的示例——所有示例都使用了似乎无法从Delphi访问的类

我的解决方案是创建一个标准模板,然后用自定义模板所需的xml覆盖该标准模板中的xml。下面是一些Delphi代码,它应该会给你一些想法。这是一个完整的控制台应用程序。此代码在Delphi10.2下编译。早期版本可能需要一些调整。您将对
OverwriteToastTemplateXML
函数感兴趣

我的代码基于Marco Cantu博客文章最后一条评论中引用的标准toast模板示例:

注意我的XML中对“英雄”图像jpg文件的引用。要使通知完全工作,请确保在c:\notifications\hero.jpg上有一个jpg,或者在xml中注释掉该行

除了将XML字符串转换为自定义toast模板外,该代码还将toast模板转换回字符串,这对于调试非常有用-这是
ToastTemplateToString
函数。这些是我对原始示例的主要功能修改。根据我自己的理解,我还更改了示例代码的结构,因此变量范围以及每行代码与其他代码的关系也更加明显

让我知道这是否适合你-我发现toast通知是Delphi的一项艰苦工作

干杯

史蒂夫

程序控制台;
{$APPTYPE控制台}
{$R*.res}
使用
System.SysUtils,
//创建Toast通知所需
WinAPI.WinRT,
WinAPI.DataRT,
WinAPI.UI.Notifications,
WinAPI.ActiveX,
WinAPI.CommonType,
//创建桌面Shell链接时需要此选项
WinAPI.PropKey,
WinAPI.PropSys,
WinAPI.ShlObj,
System.Win.ComObj,
窗户
;
函数CreateDesktopShellLink(const TargetName:string):布尔值;
函数GetStartMenuFolder:字符串;
变量
缓冲区:字符的数组[0..MAX_PATH-1];
开始
结果:='';
GetEnvironmentVariable(PChar('APPDATA'),缓冲区,最大路径-1);
结果:=缓冲区+'\Microsoft\Windows\Start Menu\Programs\Desktop Delphi Toasts App.lnk';
结束;
变量
研究对象:IUnknown;
ISLink:IShellLink;
IPFile:IPersistFile;
LinkName:string;
LStore:WinAPI.PropSys.IPropertyStore;
左值:TPropVariant;
开始
结果:=假;
IOObject:=CreateComObject(CLSID_外壳链接);
ISLink:=作为IShellLink的IOObject;
IPFile:=作为IPersistFile的IOObject;
LStore:=IOObject作为WinAPI.PropSys.IPropertyStore;
与艾斯林
一定要开始
SetPath(PChar(ParamStr(0));
结束;
ISLink.SetArguments(PChar(“”));
如果成功(InitPropVariantFromStringAsVector(PWideChar('Delphi.DesktopNotification.Sample'),左值))
然后开始
如果成功(LStore.SetValue(PKEY_AppUserModel_ID,左值))
然后是托尔。承诺;
结束;
LinkName:=GetStartMenuFolder;
如果文件不存在(LinkName)
然后
如果IPFile.Save(PWideChar(LinkName),则为True)=S_确定
然后结果:=真;
结束;
函数HStr(值:String):HString;
开始
如果没有成功(
WindowsCreateString(PWideChar(值)、长度(值)、结果)
)
然后引发异常。CreateFmt('无法为%s创建hs字符串',[Value]);
结束;
函数ToastTemplateToString(Const-Template:Xml\u-Dom\u-IXmlDocument):字符串;
函数HStringToString(Src:HSTRING):字符串;
变量
c:红衣主教;
开始
c:=WindowsGetStringLen(Src);
结果:=WindowsGetStringRawBuffer(Src,@c);
结束;
开始
结果:=HStringToString(
(Template.DocumentElement作为Xml_Dom_IXmlNodeSerializer)
);
结束;
函数GetFactory(常量名称:String;常量GUID:String):IInspectable;
变量
工厂字符串:hs字符串;
FactoryGUID:TGUID;
开始
FactoryHString:=HStr(名称);
尝试
FactoryGUID:=TGUID.Create(GUID);
如果没有成功(
RoGetActivationFactory(FactoryHString、FactoryGUID、结果)
)
然后引发异常。CreateFmt('创建工厂时出错:%s%s',[Name,GUID]);
最后
WindowsDeleteString(FactoryHString);
结束;
结束;
过程OverwriteToastTemplateXML(常量模板:Xml\u Dom\u IXmlDocument;常量Xml:String);
变量
hXML:HSTRING;
开始
hXML:=HStr(XML);
尝试
(模板为Xml_Dom_IXmlDocumentIO);
最后
WindowsDeleteString(hXML);
结束;
结束;
过程通知(Const-AppID:String;Const-XML:String);
变量
ToaNotificationManagerStatics:ItaNotificationManagerStatics;
toast模板:Xml\u Dom\u IXmlDocument;
LToastNotification:IToastNotification;
ToastNotificationManagerFactory:不可检测;
ToastNotificationFactory:不可检测;
hAppID:HString;
开始
ToastNotificationManagerFactory:=GetFactory(ToAstNotificationManager,{50AC103F-D235-4598-BBEF-98FE4D1A3AD4});
ToastNotificationManagerStatics:=IToastNotificationManagerStatics(ToastNotificationManagerFactory);
ToastTemplate:=ToastNotificationManagerStatics.GetTemplateContent(ToastTemplateType.ToastText01);
重写astTemplateXML(ToastTemplate,XML);
WriteLn('XML:',ToastTemplateToString(ToastTemplate));
ToastNotificationFactory:=GetFactory(SToastNotification,{04124B20-82C6-4229-B109-FD9ED4662B53});
LToastNotification:=IToastNotificationFactory(ToastNotificationFactory)。CreateToastNotification(ToastTemplate);
program ConsoleNotifier;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,

  // Required to create the Toast Notification
  WinAPI.WinRT,
  WinAPI.DataRT,
  WinAPI.UI.Notifications,
  WinAPI.ActiveX,
  WinAPI.CommonTypes,

  // Required for creating the Desktop Shell Link
  WinAPI.PropKey,
  WinAPI.PropSys,
  WinAPI.ShlObj,
  System.Win.ComObj,
  Windows
  ;

function CreateDesktopShellLink(const TargetName: string): Boolean;

  function GetStartMenuFolder: string;
  var
    Buffer: array [0 .. MAX_PATH - 1] of Char;
  begin
    Result := '';
    GetEnvironmentVariable(PChar('APPDATA'), Buffer, MAX_PATH - 1);
    Result := Buffer + '\Microsoft\Windows\Start Menu\Programs\Desktop Delphi Toasts App.lnk';
  end;

var
  IObject: IUnknown;
  ISLink: IShellLink;
  IPFile: IPersistFile;
  LinkName: string;

  LStore: WinAPI.PropSys.IPropertyStore;
  LValue: TPropVariant;
begin
  Result := False;

  IObject := CreateComObject(CLSID_ShellLink);
  ISLink := IObject as IShellLink;
  IPFile := IObject as IPersistFile;
  LStore := IObject as WinAPI.PropSys.IPropertyStore;

  with ISLink
  do begin
    SetPath(PChar( ParamStr(0) ));
  end;
  ISLink.SetArguments(PChar(''));

  if Succeeded(InitPropVariantFromStringAsVector(PWideChar('Delphi.DesktopNotification.Sample'), LValue))
  then begin
    if Succeeded(LStore.SetValue(PKEY_AppUserModel_ID, LValue))
    then LStore.Commit;
  end;

  LinkName := GetStartMenuFolder;

  if not FileExists(LinkName)
  then
    if IPFile.Save(PWideChar(LinkName), True) = S_OK
    then Result := True;
end;

function HStr( Value:String ): HString;
begin
  if NOT Succeeded(
    WindowsCreateString(PWideChar(Value), Length(Value), Result)
  )
  then raise Exception.CreateFmt('Unable to create HString for %s', [ Value ] );
end;

function ToastTemplateToString( Const Template:Xml_Dom_IXmlDocument ): String;

  function HStringToString(Src: HSTRING): String;
  var
    c: Cardinal;
  begin
    c := WindowsGetStringLen(Src);
    Result := WindowsGetStringRawBuffer(Src, @c);
  end;

begin
  Result := HStringToString(
    ( Template.DocumentElement as Xml_Dom_IXmlNodeSerializer ).GetXml
  );
end;

function GetFactory( Const Name:String; Const GUID:String ): IInspectable;
var
  FactoryHString : HString;
  FactoryGUID    : TGUID;
begin
  FactoryHString := HStr( Name );
  try
    FactoryGUID := TGUID.Create(GUID);

    if NOT Succeeded(
      RoGetActivationFactory(FactoryHString, FactoryGUID, Result)
    )
    then raise Exception.CreateFmt('Error creating factory: %s %s', [ Name, GUID ] );
  finally
    WindowsDeleteString( FactoryHString );
  end;
end;

procedure OverwriteToastTemplateXML( Const Template: Xml_Dom_IXmlDocument; Const XML:String );
var
  hXML: HSTRING;
begin
  hXML := HStr( XML );
  try
    (Template as Xml_Dom_IXmlDocumentIO).LoadXml( hXML );
  finally
    WindowsDeleteString( hXML );
  end;
end;

procedure SteveNotification( Const AppID:String; Const XML:String );
var
  ToastNotificationManagerStatics : IToastNotificationManagerStatics;
  ToastTemplate                   : Xml_Dom_IXmlDocument;
  LToastNotification              : IToastNotification;
  ToastNotificationManagerFactory : IInspectable;
  ToastNotificationFactory        : IInspectable;
  hAppID                          : HString;
begin
  ToastNotificationManagerFactory := GetFactory( sToastNotificationManager, '{50AC103F-D235-4598-BBEF-98FE4D1A3AD4}' );
  ToastNotificationManagerStatics := IToastNotificationManagerStatics(ToastNotificationManagerFactory);
  ToastTemplate := ToastNotificationManagerStatics.GetTemplateContent(ToastTemplateType.ToastText01);

  OverwriteToastTemplateXML( ToastTemplate, XML );

  WriteLn( 'XML: ', ToastTemplateToString( ToastTemplate ) );

  ToastNotificationFactory := GetFactory( SToastNotification, '{04124B20-82C6-4229-B109-FD9ED4662B53}' );

  LToastNotification := IToastNotificationFactory(ToastNotificationFactory).CreateToastNotification(ToastTemplate);

  hAppID := HStr( AppID );
  try
    ToastNotificationManagerStatics
    .CreateToastNotifier( hAppID )
    .Show(LToastNotification);
  finally
    WindowsDeleteString( hAppID );
  end;
end;

Const
  AppID = 'My Application ID';
  XML   = '<toast activationType="protocol" launch="http://www.ecutek.com" >'
        + '  <visual>'
        + '    <binding template="ToastGeneric">'
        + '      <text>Body Text ABC</text>'
        + '      <text>More Text</text>'
        + '      <image placement="hero" src="file:///c:\notifications\hero.jpg"/>'
        + '    </binding>'
        + '  </visual>'
        + '  <actions>'
        + '    <action content="Open Google" activationType="protocol" arguments="http://www.google.com" />'
        + '  </actions>'
        + '</toast>';

var
  c : char;
begin
  try
    if TOSVersion.Major < 10
    then raise Exception.Create('Windows 10 Required');

    RoInitialize(RO_INIT_MULTITHREADED);

    CreateDesktopShellLink( ParamStr(0) );

    SteveNotification( AppID, XML );

    // Wait for a KeyPress
    Read( c ); write( c );
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.