0

我有一个自定义的无边框窗口,旨在作为未来 WPF 项目的基本表单。我已经设法模拟了几乎所有本机窗口行为,但是 DragMove 有问题。当用户将其标题栏拖到 Windows 任务栏后面时,我希望窗口重新定位在任务栏上方,以便整个窗口可见。我设法通过重写 OnLocationChanged 方法来做到这一点,因为在 DragMove 期间所有与鼠标相关的处理程序都被阻止。

这就像一个魅力,除了当我沿着 Windows 任务栏拖动标题栏时会闪烁。我可以看到它正在尝试在下面重绘窗口,然后它立即在预期的位置再次重绘窗口。

如何正确中断此重绘?非常感谢!


不幸的是,这个这个这个这个SO 问题根本没有帮助我。这是我的代码的摘录:

protected override void OnLocationChanged(EventArgs e)
{
    base.OnLocationChanged(e);

    var activeScreenHeight = System.Windows.Forms.Screen.FromHandle(new WindowInteropHelper(this).Handle).WorkingArea.Bottom - ToolbarSnapThreshold;
    if (activeScreenHeight < this.RestoreBounds.Top)
    {
        this.Top = activeScreenHeight - this.RestoreBounds.Height + ToolbarSnapThreshold;
    }
}

protected void MaximizeOrRestore()
{
    this.WindowState = (this.WindowState == WindowState.Normal) ? WindowState.Maximized : WindowState.Normal;
    this.lastNonMinimizedState = this.WindowState;
}

protected void RestoreAndDragMove(MouseButtonState leftMouseButtonState)
{
    if (leftMouseButtonState == MouseButtonState.Pressed)
    {
        if (this.WindowState == WindowState.Maximized)
        {
            var activeScreenWidth = System.Windows.Forms.Screen.FromHandle(new WindowInteropHelper(this).Handle).Bounds.Width;
            var windowCurrentWidth = this.RestoreBounds.Width;

            var windowPositionAdjustX = this.relativeMousePositionX - (windowCurrentWidth / 2);

            if (windowPositionAdjustX < 0)
            {
                windowPositionAdjustX = 0;
            }
            else if (windowPositionAdjustX + windowCurrentWidth > activeScreenWidth)
            {
                windowPositionAdjustX = activeScreenWidth - windowCurrentWidth;
            }

            this.WindowState = WindowState.Normal;
            this.lastNonMinimizedState = this.WindowState;
            this.Left = windowPositionAdjustX - this.relativeMousePositionX + this.originalMousePositionX;
            this.Top = 0 - this.relativeMousePositionY + this.originalMousePositionY;
        }

        this.DragMove();
    }
}

private void Window_TitleBarClicked(object sender, MouseButtonEventArgs e)
{
    if (e.LeftButton == MouseButtonState.Pressed)
    {
        this.relativeMousePositionX = e.GetPosition(this).X;
        this.relativeMousePositionY = e.GetPosition(this).Y;
        this.originalMousePositionX = System.Windows.Forms.Cursor.Position.X;
        this.originalMousePositionY = System.Windows.Forms.Cursor.Position.Y;

        if (e.ClickCount == 2)
        {
            this.MaximizeOrRestore();
        }
    }
}
4

0 回答 0