tags:

views:

43

answers:

1

Hi

I am trying to ensure my WPF window stays on top as long as it is open. It is acting as a pop-up set to TopMost=true, and a call to the win32 SetWindowPos to TOPMOST. When first opened it appears on top of another running application on the desktop- maximized or not.

If the user activates or uses a window in the application mine loses focus and disappears.

I thought of manipulating the other application window, setting it to a lower z index. How do I find the application window? How do I iterate through all windows? (This question still stands, even if it is not the correct approach).

I would be using SetWindowPos, GetForegroundWindow, GetForegroundWindow, GetDesktopWindow et cetera.

I suspect that as soon as the user clicks in their application that it will still focus it regardless and I am barking up the wrong tree.

At the moment, my application is a black box and I can’t handle it the other way, for example, periodically messaging my app to focus.

I also thought of having a long running background thread which periodically focuses my WPF popup, but need to watch resources and processor.

Kind regards,

A: 

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;
          }
       }


   }

Rakesh Gunijan
Do Refer System.Windows.Forms assembly of framework (Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll)
Rakesh Gunijan