Я сталкиваюсь со следующей проблемой: в 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");
}
У меня в консоли отображается «poney», поэтому яугадайте, код для вызова события работает?
В короткой версии мне нужен метод для прерывания dragmove, чтобы я мог внести некоторые изменения и перезапустить DragMove.
Спасибо :))
PS: для теста используется значение 2000, в «реальном» вычисляется позиция.