Create popup “toaster” notifications in Windows with .NET

后端 未结 6 1146
清歌不尽
清歌不尽 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:44

    Note that the calling thread must be sta because many ui components require this, while writing following code under system.timers.timer elapsed event

    Window1 notifyWin = new Window1();
    bool? isOpen = notifyWin.ShowDialog();
    if (isOpen != null && isOpen == true)
    {
         notifyWin.Close();
    }
    System.Threading.Thread.Sleep(1000);
    notifyWin.ShowDialog();
    

    under window1 constructor:

    public Window1()
    {
        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; 
        })); 
    }
    

提交回复
热议问题