【发布时间】:2018-10-03 14:29:33
【问题描述】:
我面临以下问题: 在 WPF 中,我有一个 WindowStyle="None" 的窗口,因此我添加了一个按钮来使用 DragMove() 方法移动窗口。这部分工作正常。我还想要的是,当窗口到达某个位置时,它会停止 DragMove。 我的想法是通过提高 MouseLeftButtonLeft 来实现它,认为它会中断 DragMove,但事实并非如此。
移动窗口的按钮:
<Button Grid.Column="0" x:Name="MoveButton" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="3" Cursor="Hand">
<Image x:Name="MoveImage" Source="images/move.png" MouseLeftButtonDown="MoveWindow" MouseLeftButtonUp="Poney" />
</Button>
移动窗口的方法:
// Move the window with drag of the button. Ensure that we are not over the taskbar
private void MoveWindow(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
DragMove();
// https://stackoverflow.com/questions/48399180/wpf-current-screen-dimensions
System.Drawing.Rectangle workingArea = Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(this).Handle).WorkingArea;
//The left property of the window is calculated on the width of all screens, so use VirtualScreenWidth to have the correct width
if (Top > (workingArea.Height - Height))
{
Top = (workingArea.Height - Height);
}
else if (Left > (SystemParameters.VirtualScreenWidth - Width))
{
Left = (SystemParameters.VirtualScreenWidth - Width);
}
else if (Left < 0)
{
Left = 0;
}
}
引发事件的方法:
private void MainWindow_LocationChanged(object sender, EventArgs e)
{
// https://stackoverflow.com/questions/48399180/wpf-current-screen-dimensions
System.Drawing.Rectangle workingArea = Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(this).Handle).WorkingArea;
if(Left > 2000)
{
MouseButtonEventArgs mouseButtonEvent = new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
{
RoutedEvent = MouseLeftButtonUpEvent,
Source = MoveImage
};
MoveImage.RaiseEvent(mouseButtonEvent);
//InputManager.Current.ProcessInput(mouseButtonEvent);
}
}
检查是否引发了事件:
public void Poney(object sender, MouseButtonEventArgs e)
{
Console.WriteLine("Poney");
}
我的控制台中显示“小马”,所以我猜引发事件的代码有效?
简而言之,我需要一种方法来中断 DragMove,以便我可以进行一些更改并重新启动 DragMove。
谢谢:)
PS : 2000 值用于测试,在“真实”中计算位置。
【问题讨论】: