【问题标题】:Event for exiting the screen monitor (with WPF)退出屏幕监视器的事件(使用 WPF)
【发布时间】:2013-07-28 00:46:10
【问题描述】:

我正在编写一个使用 WPF 窗口的程序。我使用下面的代码来最大化窗口:

   this.WindowStyle = WindowStyle.None;            
   this.WindowState = WindowState.Maximized;      
   this.Topmost = true; 

当窗口最大化时,我想知道鼠标是否退出了我的显示器的边框,这样我就可以打开一个提供一些新控件的新窗口(就像在 BsPlayer 中一样:当鼠标退出屏幕时,会打开一个窗口让您可以访问播放、暂停、停止等按钮)。 我尝试使用 this.MouseLeave,但窗口最大化时似乎没有触发任何事件。经过一些测试,我发现问题可能是当我的窗口最大化时,它实际上大于显示器的分辨率。在一个基本示例中:如果您的显示器的分辨率为 1280 x 1024,则窗口的尺寸(根据 this.Width 和 this.Height)为 1294 x 1038。那我该怎么办?我应该如何解决这个问题?

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    您可以处理窗口的MouseMove 事件并检查鼠标的位置。

    private void Window_MouseMove(object sender, MouseEventArgs e)
    {
       //PrimaryScreenWidth - 1 to account for the cursor itself
       if (e.GetPosition(this).X >= SystemParameters.PrimaryScreenWidth - 1 || e.GetPosition(this).X <= 1)
           MessageBox.Show("Edge hit");
    }
    

    如果您正在运行多个监视器,您还可以处理MouseLeave 事件来处理鼠标移动到第二个监视器的情况。

    private void Window_MouseLeave(object sender, MouseEventArgs e)
    {
       MessageBox.Show("Edge hit");
    }
    

    【讨论】:

    • 好的,如果 (e.GetPosition(this).Y >= SystemParameters.PrimaryScreenHeight - 1) 是我需要的。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多