首页 文章

即使用户单击另一个最大化的应用程序,也确保WPF窗口始终处于最佳状态

提问于
浏览
3

我试图确保我的WPF窗口保持在顶部,只要它是打开的 . 它作为一个弹出设置为TopMost = true,并调用win32 SetWindowPos为TOPMOST . 首次打开时,它会显示在桌面上另一个正在运行的应用程序的顶部 - 最大化与否 .

如果用户激活或使用应用程序中的窗口,则会失去焦点并消失 .

我想到了操纵其他应用程序窗口,将其设置为较低的z索引 . 如何找到应用程序窗口?如何遍历所有窗口? (这个问题仍然存在,即使它不是正确的方法) .

我将使用SetWindowPos,GetForegroundWindow,GetForegroundWindow,GetDesktopWindow等 .

我怀疑,只要用户点击他们的应用程序,它仍将无关紧要,我正在咆哮错误的树 .

目前,我的应用程序是一个黑盒子,我无法以其他方式处理它,例如,定期发送我的应用程序以进行聚焦 .

我还想过有一个长时间运行的后台线程,它定期关注我的WPF弹出窗口,但需要观察资源和处理器 .

亲切的问候,

1 回答

  • 0

    试试这种方法:

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

相关问题