【问题标题】:WPF Move two windows togetherWPF 将两个窗口一起移动
【发布时间】:2018-01-18 06:41:59
【问题描述】:

我有两种类型的窗口:MainChild。当我移动主窗口时,所有子窗口也必须移动。我所有的窗口都有样式None,所以我使用DragMove。对于移动的孩子,我使用 MainLocationChange。如果我开始快速移动主窗口,孩子会延迟一些移动。我的窗口是一个接一个的,所以当快速移动主窗口时,会出现一些间隙。 我用这个问题 Move two WPF windows at once?

我怎样才能减少这种差距?

一些代码:

    private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        StickyWindow.ParentWndStartMove(this);
        this.DragMove();
    }

这里我把所有的孩子都搬走了

    public static void ParentWndMove(Window parentWindow)
    {
        for (int i = 0; i < windowsToMove.Length; i++)
        {
            if (windowsToMove[i])
            {
                windows[i].Top += -(parentWindowPosition.Y - parentWindow.Top);
                windows[i].Left += -(parentWindowPosition.X - parentWindow.Left);
            }
        }

        parentWindowPosition.X = parentWindow.Left;
        parentWindowPosition.Y = parentWindow.Top;
    }

【问题讨论】:

  • 可以分享代码吗?

标签: c# .net wpf


【解决方案1】:

我将根据您的代码片段做出一个假设,即 parentWindowPosition 是将 X 和 Y 初始化为 MainWindow 的 Left 和 Top 值的结构或类。

如果是这种情况,那么您只需要在 MouseLeftButtonDown 处理程序中做的就是调用 DragMove()

private void MainWindow_OnMouseLeftButtonDown(object sender, 
                                              MouseButtonEventArgs e)
{
    DragMove();
}

在 MainWindow 的 LocationChanged 事件上注册一个处理程序。

LocationChanged += MainWindow_OnLocationChanged;

调用您的 ParentWndMove() 方法

private void MainWindow_OnLocationChanged(object sender, EventArgs e)
{
     ParentWndMove(sender as Window)
}

无论我拖动主窗口多快,此代码都可以在我的系统上正常运行,并且窗口位置永远不会不同步。

注意:您发布的 ParentWndMove() 方法存在一些编译时错误。我发布一个固定版本供参考

public static void ParentWndMove(Window parentWindow)
{
    for (int i = 0; i < windowsToMove.Length; i++)
    {
        if (windowsToMove[i] != null)
        {
            windowsToMove[i].Top += -(parentWindowPosition.Y - parentWindow.Top);
            windowsToMove[i].Left += -(parentWindowPosition.X - parentWindow.Left);
        }
    }

    parentWindowPosition.X = parentWindow.Left;
    parentWindowPosition.Y = parentWindow.Top;
}

【讨论】:

  • 在此处添加示例项目:sourse 如果我移动得快,我可以在两个窗口之间看到手机图片。
【解决方案2】:

这是我所做的:

我首先将 Parent 的一个 Instance 设置为 Child 窗口的所有者,(通过在 ParentWindow 类public static ParentWindow instance; 然后instance = this; 中设置它来创建一个实例):

public ChildWindow()
{
    Owner = ParentWindow.instance;
    InitializeComponent();
}

然后我在父类中添加一个事件处理程序以在父类移动时触发:

public ParentWindow()
{
    InitializeComponent();
    LocationChanged += new EventHandler(Window_LocationChanged);
}

现在我遍历 ParentWindow 拥有的所有窗口以重置它们的边距:

private void Window_LocationChanged(object sender, EventArgs e)
{
    foreach (Window win in this.OwnedWindows)
    {
        win.Top = this.Top + ((this.ActualHeight - win.ActualHeight) / 2);
        win.Left = this.Left + ((this.ActualWidth - win.ActualWidth) / 2);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2022-01-05
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多