前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Windows 10 Notifications from a VCL app with the WinRT API

Windows 10 Notifications from a VCL app with the WinRT API

作者头像
战神伽罗
发布2021-03-08 15:23:44
7800
发布2021-03-08 15:23:44
举报
文章被收录于专栏:Eureka的技术时光轴

The WinRT API headers for Delphi are now on GetIt and here is a demo to try out using Windows 10 notifications in a VCL Win32 application.


The WinRT API headers for Delphi are now on GetIt and here is a demo to try out using Windows 10 notifications in a VCL Win32 application.

Windows 10 Webinar

Yesterday, we had a Windows 10 focused webinar, with a Microsoft guest and a few of us (me, David I, Jim McKeeth, and JT) talking about Windows 10 and the support RAD Studio XE8 offers for the coming versions of Windows. If you missed it, the replay will soon be available. What I showed in the webinar, beside some classic demos, was a VCL application using notifications on Windows 10, a feature not available in the traditional Win32 APIs but only using the new WinRT APIs.

How to do you get the headers for that API and how do you get a working demo? Keep reading...

Delphi WinRT Headers for Windows 10 on GetIt

First of all, today we made available the headers for the WinRT API on GetIt, to all developers with Delphi or RAD Studio XE8 (there is no direct support for C++, at this very moment). Just open GetIt in XE8 and click on the corresponding entry, shown below.

Notice that this code is for the Windows 10 preview, it is likely that the APIs and our header translations will change over the coming months. So you might have to change/clean up your code in the future, but it is great to be able to start target WinRT classes today with XE8.

The WinRT Notification Demo

As an example of an interesting feature you can use only from WinRT, this demo is focused on the new notifications, called "toast" notifications. Now, the API is available but not really encapsulated in ready-to-use components, so the code is far from simple and I won't list everything here, only the core elements. This is what we want to obtain:

This is a portion of the code used to create a notification. As you can guess below, WinRT classes are basically used like COM objects, and they share the core architecture. The objects creation doesn't use a CoClass (or class factory) but is a little more complex:

代码语言:javascript
复制
    if Succeeded(RoGetActivationFactory(LString, TGUID.Create('{50AC103F-D235-4598-BBEF-98FE4D1A3AD4}'), LCreated)) then
    begin
      LINotificationManagerStatics := LCreated as IToastNotificationManagerStatics;
      if Succeeded(WindowsCreateString(PWideChar(Edit1.Text), Length(Edit1.Text), LString2)) then
      begin
        LToastNotifier := LINotificationManagerStatics.CreateToastNotifier(LString2);
        LXMLTemplate := LINotificationManagerStatics.GetTemplateContent(ToastTemplateType.ToastText02);
        if Succeeded(WindowsCreateString(PWideChar(SToastNotification), Length(SToastNotification), LString)) then
        begin
          if Succeeded(RoGetActivationFactory(LString, TGUID.Create('{04124B20-82C6-4229-B109-FD9ED4662B53}'), LCreated)) then
          LToastFactory := LCreated as IToastNotificationFactory;
          LToast := LToastFactory.CreateToastNotification(LXMLTemplate);
          LAccepted := TAcceptedEventHandler.Create;
          LToast.add_Activated(LAccepted);
          LToastNotifier.Show(LToast);
        end;

The TAcceptedEventHandler class is the one that has an invoke callback method, that logs in the memo the fact taht the information was accepted (the dismiss portion I'll have to refine). This is the code:

代码语言:javascript
复制
procedure TAcceptedEventHandler.Invoke(sender: IToastNotification; args: IInspectable);
begin
  Form51.Memo1.Lines.Add ('You clicked on the notification!');
end; 

I know the reference to the global form is ugly, but the code is complex enough to avoid the extra indirection. Do you want to give it a go? You can download the complete demo at dl.dropboxusercontent.com/u/133855/WinRTCheck.zip. I'm probably going to update it, and possibly also write a couple more examples, covering other new areas in WinRT.

Windows 10 is Coming

...and it looks it will be a very important version of Windows, so get the Windows 10 Preview now, and start using XE8 to experiment the integration of your VCL (and FireMonkey) applications with it.

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Windows 10 Webinar
  • Delphi WinRT Headers for Windows 10 on GetIt
  • The WinRT Notification Demo
  • Windows 10 is Coming
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档