gpt4 book ai didi

c# - WPF DragMove() 导致问题

转载 作者:行者123 更新时间:2023-11-30 21:32:18 26 4
gpt4 key购买 nike

我想弄清楚是否有一个优雅的解决方案来解决我一直面临的问题。

所以基本上,我设计了一个无边框的加载启动画面,它可以通过拖动完全移动。我发现如果初始屏幕通过 Hide() 隐藏,然后通过 ShowDialog() 显示一个窗口,所有者设置为初始屏幕,就会发生这种情况。事情变得非常错误,但前提是你处于中间拖动状态(按下鼠标左键)。您将无法单击或移动任何内容,甚至 Visual Studio 也会变得无响应,除非您明确地按 alt-tab 退出应用程序。

考虑到我知道何时生成窗口,我在想也许有一种方法可以取消 DragMove 操作,但我没有运气。我发现 DragMove 是同步的,所以我猜它必须从不同的线程或事件回调中取消。

编辑:

public partial class Window_Movable : Window
{
public Window_Movable()
{
InitializeComponent();
}

public Boolean CanMove { get; set; } = true;

private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (CanMove == false)
return;

Task.Factory.StartNew(() => {
System.Threading.Thread.Sleep(1000);

Dispatcher.Invoke(() => {
Hide();
new Window_Movable() {
Title = "MOVABLE 2",
CanMove = false,
Owner = this
}.ShowDialog();
});
});

DragMove();
}

private void Button_Click(object sender, RoutedEventArgs e)
{
Console.WriteLine("DING");
}
}

最佳答案

我有同样的问题,发现 DragMove() 方法有问题。 https://groups.google.com/forum/#!topic/wpf-disciples/7OcuXrf2whc

为了解决我决定拒绝使用它并实现移动逻辑。我结合了一些解决方案 https://www.codeproject.com/Questions/284995/DragMove-problem-help-plsC# WPF Move the window

        private bool _inDrag;
private Point _anchorPoint;
private bool _iscaptured;

private void AppWindowWindowOnMouseMove(object sender, MouseEventArgs e)
{
if (!_inDrag)
return;

if (!_iscaptured)
{
CaptureMouse();
_iscaptured = true;
}

var mousePosition = e.GetPosition(this);
var mousePositionAbs = new Point
{
X = Convert.ToInt16(_appWindowWindow.Left) + mousePosition.X,
Y = Convert.ToInt16(_appWindowWindow.Top) + mousePosition.Y
};
_appWindowWindow.Left = _appWindowWindow.Left + (mousePositionAbs.X - _anchorPoint.X);
_appWindowWindow.Top = _appWindowWindow.Top + (mousePositionAbs.Y - _anchorPoint.Y);
_anchorPoint = mousePositionAbs;
}

private void AppWindowWindowOnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (_inDrag)
{
_inDrag = false;
_iscaptured = false;
ReleaseMouseCapture();
}
}

private void AppWindowWindowOnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_anchorPoint = e.GetPosition(this);
_anchorPoint.Y = Convert.ToInt16(_appWindowWindow.Top) + _anchorPoint.Y;
_anchorPoint.X = Convert.ToInt16(_appWindowWindow.Left) + _anchorPoint.X;
_inDrag = true;
}

我花了整个昨天晚上,找到了一个更 hacky 但更实用的解决方案。支持最大化状态,不需要手动坐标计算。

    private bool _mRestoreForDragMove;
private void OnAppWindowWindowOnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
{
if (_appWindowWindow.ResizeMode != ResizeMode.CanResize &&
_appWindowWindow.ResizeMode != ResizeMode.CanResizeWithGrip)
{
return;
}

_appWindowWindow.WindowState = _appWindowWindow.WindowState == WindowState.Maximized
? WindowState.Normal
: WindowState.Maximized;
}
else
{
_mRestoreForDragMove = _appWindowWindow.WindowState == WindowState.Maximized;

SafeDragMoveCall(e);
}
}

private void SafeDragMoveCall(MouseEventArgs e)
{
Task.Delay(100).ContinueWith(_ =>
{
Dispatcher.BeginInvoke((Action)
delegate
{
if (Mouse.LeftButton == MouseButtonState.Pressed)
{
_appWindowWindow.DragMove();
RaiseEvent(new MouseButtonEventArgs(e.MouseDevice, e.Timestamp, MouseButton.Left)
{
RoutedEvent = MouseLeftButtonUpEvent
});
}
});
});
}

private void OnMouseMove(object sender, MouseEventArgs e)
{
if (_mRestoreForDragMove)
{
_mRestoreForDragMove = false;

var point = PointToScreen(e.MouseDevice.GetPosition(this));

_appWindowWindow.Left = point.X - (_appWindowWindow.RestoreBounds.Width * 0.5);
_appWindowWindow.Top = point.Y;

_appWindowWindow.WindowState = WindowState.Normal;

_appWindowWindow.DragMove();

SafeDragMoveCall(e);
}
}

private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
_mRestoreForDragMove = false;
}

事情是在短暂延迟后发送 LeftMouseButtonUp 事件以避免被 DragMove() 阻塞

上次求解的来源: DragMove() and Maximize C# WPF - DragMove and click

关于c# - WPF DragMove() 导致问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52336874/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com