Reputation: 229
I have two types of windows: Main and Child. When I move main, all child windows must move also. All my windows have style None
, so I use DragMove
. For moving child I use LocationChange
of Main. And if I start moving main window fast, child move with some delay. My windows are stick one to another, so when moving main window fast, some gaps appears.
I use this question
Move two WPF windows at once?
How can I reduce this gaps?
some code:
private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
StickyWindow.ParentWndStartMove(this);
this.DragMove();
}
here I move all children
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;
}
Upvotes: 1
Views: 2649
Reputation: 2525
Here is what I did:
I first set an Instance of the Parent as the owner of the Child window, (make an instance by setting it in the ParentWindow class public static ParentWindow instance;
then instance = this;
) :
public ChildWindow()
{
Owner = ParentWindow.instance;
InitializeComponent();
}
Then I add an event handler in the Parent Class to fire when the Parent is moving:
public ParentWindow()
{
InitializeComponent();
LocationChanged += new EventHandler(Window_LocationChanged);
}
Now I loop through all the windows owened by the ParentWindow to reset their margin:
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);
}
}
Upvotes: 1
Reputation: 316
I'm going to make an assumption based on your code fragment that parentWindowPosition is a struct or class that has X and Y initialized to the Left and Top values of your MainWindow.
If that is the case then only thing you need to do in your MouseLeftButtonDown handler is call DragMove()
private void MainWindow_OnMouseLeftButtonDown(object sender,
MouseButtonEventArgs e)
{
DragMove();
}
Register a handler on the LocationChanged event of your MainWindow.
LocationChanged += MainWindow_OnLocationChanged;
which calls your ParentWndMove() method
private void MainWindow_OnLocationChanged(object sender, EventArgs e)
{
ParentWndMove(sender as Window)
}
This code works on my system with no delays no matter how quickly I drag the main window and the window positions never get out of sync.
NOTE: The ParentWndMove() method you posted has some compile time errors. I'm posting a fixed version for reference
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;
}
Upvotes: 1