Home > Back-end >  How to move another window inside of WPF, when one is being moved?
How to move another window inside of WPF, when one is being moved?

Time:01-05

I have found a solution that works with a process, but I want to move the MainWindow when my other window is being moved.

The solution below works with processes ex. notepad and moves it correctly, but How would I apply this to MainWindow?

private Process AppToMove = Process.Start("notepad.exe");

        private void Window_LocationChanged(object sender, EventArgs e)
        {
            if (AppToMove != null)
            {
                AppToMove.WaitForInputIdle();
            }
            var xx = Application.Current.MainWindow.Top;
            var yy = Application.Current.MainWindow.Left;
            var zz = Application.Current.MainWindow.Width;
            var zzz = Application.Current.MainWindow.Height;

            var answer = yy   zz   ((yy   zz) * 0);
            var answer2 = xx   zzz   ((xx   zzz) * .0) - zzz;
            
            int IntY = (int)answer;
            int IntX = (int)answer2;
            CustomMove(AppToMove, IntY, IntX, 375, 812);
        }

        public void CustomMove(Process process, int x, int y, int width, int height)
        {
            MoveWindow(process.MainWindowHandle, x, y, width, height, true);
        }

        //DLL call to make 'moveWindow' functional
        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);

MainWindow can be shown with

MainWindow MW = new MainWindow();
MW.Show();

but how can it be moved from the other window?

Or is it simpler to create a secondary project, separating the two and calling the process/exe file from the second project after compilation?

CodePudding user response:

Replace the Process parameter with a Window parameter and set its properties instead of calling the external MoveWindow method.

Something like this, where MW is a System.Windows.Window:

private void Window_LocationChanged(object sender, EventArgs e)
{
    var xx = Application.Current.MainWindow.Top;
    var yy = Application.Current.MainWindow.Left;
    var zz = Application.Current.MainWindow.Width;
    var zzz = Application.Current.MainWindow.Height;

    var answer = yy   zz   ((yy   zz) * 0);
    var answer2 = xx   zzz   ((xx   zzz) * .0) - zzz;

    int IntY = (int)answer;
    int IntX = (int)answer2;
    CustomMove(MW, IntY, IntX, 375, 812);
}

public static void CustomMove(Window window, int x, int y, int width, int height)
{
    window.Left = x;
    window.Top = y;
    window.Width = width;
    window.Height = height;
}
  •  Tags:  
  • Related