我写了这段代码,但只有我的多显示器设置的顶部和左侧正在捕捉。屏幕的底部和右侧边缘以及显示器之间的中间边缘被忽略。我如何捕捉到这些边缘?
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using System.Runtime.InteropServices;
namespace MyTimer
{
public partial class MainWindow
{
Point LastLeftClickPosition = new Point ( );
int SnapThresholdInPixels = 20;
void Window_MouseLeftButtonDown ( object sender, MouseButtonEventArgs e )
{
this.LastLeftClickPosition = e.GetPosition ( this );
Mouse.Capture ( this );
}
void Window_MouseLeftButtonUp ( object sender, MouseButtonEventArgs e )
{
Mouse.Capture ( null );
}
void Window_MouseMove ( object sender, MouseEventArgs e )
{
if ( e.LeftButton == MouseButtonState.Pressed )
{
var currentPosition = e.GetPosition ( this );
var deltaX = currentPosition.X - this.LastLeftClickPosition.X;
var deltaY = currentPosition.Y - this.LastLeftClickPosition.Y;
Left += deltaX;
Top += deltaY;
this.SnapWindow ( );
}
}
void SnapWindow ( )
{
var screens = System.Windows.Forms.Screen.AllScreens;
foreach ( var screen in screens )
{
var workingArea = screen.WorkingArea;
if ( Math.Abs ( this.Left - workingArea.Left ) < this.SnapThresholdInPixels )
this.Left = workingArea.Left;
if ( Math.Abs ( this.Top - workingArea.Top ) < this.SnapThresholdInPixels )
this.Top = workingArea.Top;
if ( Math.Abs ( this.Left + this.ActualWidth - workingArea.Right ) < this.SnapThresholdInPixels )
this.Left = workingArea.Right - ActualWidth;
if ( Math.Abs ( this.Top + this.ActualHeight - workingArea.Bottom ) < this.SnapThresholdInPixels )
this.Top = workingArea.Bottom - ActualHeight;
}
}
}
1条答案
按热度按时间uttx8gqw1#
从你发布的代码中,我们还不清楚为什么行为不像预期的那样。我们只能假设。例如,交换屏幕对齐。
您可以尝试以下代码来查看它是否修复了该问题。该示例显式检测
Window
被拖动到的当前屏幕。它还补偿了主屏幕和辅助屏幕顺序交换的情况(这将导致负屏幕位置以指示交换的顺序)。您还应该考虑覆盖从
UIElement
继承的输入事件处理程序,而不是将自定义事件处理程序附加到事件: