Create popup “toaster” notifications in Windows with .NET

后端 未结 6 1145
清歌不尽
清歌不尽 2020-12-02 04:12

I am using .NET and am creating a desktop app/service that will display notifications in the corner of my Desktop when certain events are triggered. I don\'t want to use a r

6条回答
  •  情话喂你
    2020-12-02 04:50

    WPF makes this absolutely trivial: It would proably take ten minutes or less. Here are the steps:

    1. Create a Window, set AllowsTransparency="true" and add a Grid to it
    2. Set the Grid's RenderTransform to a ScaleTransform with origin of 0,1
    3. Create an animation on the grid that animates the ScaleX 0 to 1 then later animates the Opacity from 1 to 0
    4. In the constructor calculate Window.Top and Window.Left to place the window in the lower right-hand corner of the screen.

    That's all there is to it.

    Using Expression Blend it took about 8 minutes me to generate the following working code:

    
    
      
    
        
        
          
            
              Notification data
              Something just happened and you are being notified of it.
            
            
            

    With code behind:

    using System;
    using System.Windows;
    using System.Windows.Threading;
    
    public partial class NotificationWindow
    {
      public NotificationWindow()
      {
        InitializeComponent();
    
        Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() =>
        {
          var workingArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
          var transform = PresentationSource.FromVisual(this).CompositionTarget.TransformFromDevice;
          var corner = transform.Transform(new Point(workingArea.Right, workingArea.Bottom));
    
          this.Left = corner.X - this.ActualWidth - 100;
          this.Top = corner.Y - this.ActualHeight;
        }));
      }
    }
    

    Since WPF is one of the regular .NET libraries, the answer is yes, it is possible to accomplish this with the "regular .NET libraries".

    If you're asking if there is a way to do this without using WPF the answer is still yes, but it is extremely complex and will take more like 5 days than 5 minutes.

提交回复
热议问题