无边界WPF自定义窗口的DragMove上的闪烁

时间:2013-04-19 06:59:52

标签: wpf drag borderless

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

这就像一个魅力,除了我沿着Windows任务栏拖动标题栏时闪烁。我可以看到它试图重新绘制它本来的窗口,从下面开始,然后立即在预定的位置重新绘制窗口。

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


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

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();
        }
    }
}

0 个答案:

没有答案