title: c# How to make window follow another?
body: <p>Question: How do i get notepad to follow my main window when it moves? Running the program yields two windows side by side, yet as soon as the mainwindow moves by click-hold, notepad is left in the dust, im trying to find a way for notepad to follow it but keep it tightly next to the main window as how the program ran.
  I had success with making notepad move if mainwindow moves, but it's just not proportional because notepad would move to another direction at different speeds so it was not enough.
  summary: i need a function that makes notepad constantly move (update) it's position relative to mainwindows position as a user is click-moving the window. </p>
  
  <p><strong>MainWindow.xaml</strong></p>
  
  <pre><code>&lt;Window x:Class="MoveWindow.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" WindowStartupLocation="Manual" Height="500" Width="500"&gt;
  
   &lt;/Window&gt;
  </code></pre>
  
  <p><strong>MainWindow.xaml.cs</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.Linq;
  using System.Runtime.InteropServices;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  
  namespace MoveWindow
  {
  /// &lt;summary&gt;
  /// Interaction logic for MainWindow.xaml
  /// &lt;/summary&gt;
  public partial class MainWindow : Window
  {
      private readonly Process notepadProcess = null;
  
      public MainWindow()
      {
          InitializeComponent();
  
          Left = 0;
          Top = 0;
          //these variables are to find the location of mainWindow and add it to it's width --&gt; (x) so that notepad knows where to position itself next to mainwindow
  
          var xx = Application.Current.MainWindow.Top;
          var yy = Application.Current.MainWindow.Left;
          var zz = Application.Current.MainWindow.Width;
          var answer = ((yy + zz) + ((yy + zz)*.5));
  
          int myInt = (int)answer;
  
  
          var notepadProcess = Process.Start("notepad.exe");
          if (notepadProcess != null)
          {
              notepadProcess.WaitForInputIdle();
  
              // This is the function that sets location of notepad, myInt = 750(x)
              CustomMove(notepadProcess, myInt, 0, 500, 500);
          }
      }
  
      public void CustomMove(Process process, int x, int y, int width, int height)
      {
          var ok = MoveWindow(process.MainWindowHandle, x, y, width, height, true);
          if (ok == false)
              MessageBox.Show("Couldn't move your window!");
  
  
  
      }
      //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);
  }
  }
  </code></pre>