Ensuring that the WPF window is on top all the time, even when the user clicks on another application with maximum impact

I am trying to ensure that the WPF window remains on top while it is open. It acts as a popup menu TopMost = true, and the call to win32 SetWindowPos is TOPMOST. When you first open it, it appears on top of another running application on the desktop - maximized or not.

If the user activates or uses the window in the application, mine will lose focus and disappear.

I thought about manipulating another application window by setting it to the z subscript. How to find the application window? How to sort through all windows? (This question still stands, even if this is not the right approach).

I would use SetWindowPos, GetForegroundWindow, GetForegroundWindow, GetDesktopWindow and so on.

I suspect that as soon as the user clicks in his application, he will still focus it independently, and I bark the wrong tree.

Currently, my application is a black box, and I cannot deal with it in another way, for example, periodically forwarding my application for focusing.

I also thought about having a long background thread that periodically focuses the WPF popup, but I need to look at the resources and the processor.

Yours faithfully,

+3
source share
1 answer

Try this approach:


   public partial class Window1 : System.Windows.Window
   {
      private System.Windows.Forms.NotifyIcon trayNotifyIcon;
      private System.Windows.Forms.ContextMenuStrip contextMenuStrip;
      private System.Windows.Forms.ToolStripMenuItem exitMenu;
      private bool isAppExiting = false;



      public Window1()
      {        
         InitializeComponent();    

         this.Topmost = true;
         //Create an instance of the NotifyIcon Class
         trayNotifyIcon = new System.Windows.Forms.NotifyIcon();

         // This icon file needs to be in the bin folder of the application
         trayNotifyIcon.Icon = Properties.Resources.MagicRoler_Application;

         //show the Tray Notify Icon
         trayNotifyIcon.Visible = true;
         trayNotifyIcon.DoubleClick += new EventHandler(trayNotifyIcon_DoubleClick);

         //Create a object for the context menu
         contextMenuStrip = new System.Windows.Forms.ContextMenuStrip();

         //Add the Menu Item to the context menu
         System.Windows.Forms.ToolStripMenuItem mnuExit = new System.Windows.Forms.ToolStripMenuItem();
         mnuExit.Text = "Exit";
         mnuExit.Click += new EventHandler(mnuExit_Click);
         contextMenuStrip.Items.Add(mnuExit);

         //Add the Context menu to the Notify Icon Object
         trayNotifyIcon.ContextMenuStrip = contextMenuStrip;



      }    




       void mnuExit_Click(object sender, EventArgs e)
        {
            isAppExiting = true;
            this.Close();
            trayNotifyIcon.Visible = false;
        } 

       private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //When the application is closed, check wether the application is 
            //exiting from menu or forms close button
            if (!isAppExiting)
            {
                //if the forms close button is triggered, cancel the event and hide the form
                //then show the notification ballon tip
                e.Cancel = true;
                this.Hide();
                trayNotifyIcon.BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info;
                trayNotifyIcon.BalloonTipTitle = "Application";
                trayNotifyIcon.BalloonTipText = "Application is accessible through System Tray";
                trayNotifyIcon.ShowBalloonTip(400);
            }
        }

       private void Window_StateChanged(object sender, EventArgs e)
       {
          switch (this.WindowState)
          {
             case WindowState.Maximized:               
                this.Visibility = Visibility.Visible;               
                break;
             case WindowState.Normal:
                this.Visibility = Visibility.Visible;               
                break;
             case WindowState.Minimized:
                this.WindowState = WindowState.Normal;
                this.Visibility = Visibility.Hidden;                
                break;
          } 
       }

       void trayNotifyIcon_DoubleClick(object sender, EventArgs e)
       {
          switch (this.Visibility)
          {
             case Visibility.Collapsed:
             case Visibility.Hidden:
                this.Visibility = Visibility.Visible;               
                break;
             case Visibility.Visible:
                this.Visibility = Visibility.Hidden;               
                break;
          }
       }


   }

0
source

Source: https://habr.com/ru/post/1736591/


All Articles