To do this you have to go outside of the modern VS Tools mindset and invoke some legacy code. The good news is that this is still part of Windows XP and Vista so this works on just about every system out there. Most people have just forgotten or assume this method doesn't work anymore. So this may seem familiar to many of you...
- As a start, you need to import and declare some legacy Win32 API functions so you can make use of them later. Add this bit of code to include the functions as well as a couple of additional wrappers I added:
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string strClass, string strTitle);
public static IntPtr FindWindowByTitle(string strTitle)
{
return FindWindow(null, strTitle);
}
public static IntPtr FindWindowByClass(string strClass)
{
return FindWindow(strClass, null);
}
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
public static IntPtr FindChildWindow(IntPtr hwndParent, string strClass, string strTitle)
{
return FindWindowEx(hwndParent, IntPtr.Zero, strClass, strTitle);
}
public static IntPtr FindChildWindowByTitle(IntPtr hwndParent, string strTitle)
{
return FindWindowEx(hwndParent, IntPtr.Zero, null, strTitle);
}
public static IntPtr FindChildWindowByClass(IntPtr hwndParent, string strClass)
{
return FindWindowEx(hwndParent, IntPtr.Zero, strClass, null);
}
[DllImport("user32.dll", SetLastError = true)]
private static extern bool GetWindowRect(IntPtr hWindow, ref Rectangle rWindow);
public static Rectangle GetWindowRect(IntPtr hWindow)
{
Rectangle rWindow = new Rectangle();
GetWindowRect(hWindow, ref rWindow);
return rWindow;
} - The next step is to get the handle to the main Dynamics window. Do this by invoking one of the these legacy functions "FindWindowByTitle" and looking for the window title "Microsoft Dynamics GP".
hwndMainWindow = FindWindowByTitle("Microsoft Dynamics GP") - (Optional) If you want to position your window relative to the main Dynamics window, you can skip this step. Otherwise, repeat and vary this process slightly to get the handle to the desktop (child) window. Call "FindWindowByClass" with the class name "toolStripContainer1".
hwndDesktopWindow = FindChildWindowByClass(hwndMainWindow,"toolStripContainer1") - Then you can run your code to position your window. I put this in the OnLoad event handler for my window.
Rectangle rClient = GetWindowRect(hwndDesktopWindow);
if (rClient != Rectangle.Empty)
{
this.Location = new Point(rClient.X + rClient.Width/2 - this.Width/2, rClient.Y + rClient.Height - this.Height - 140);
}
(if you want to position relative to the main window just substitute the main window handle instead of the desktop window handle)
You can roll steps 2-4 together if all you want to do is position your window. Just put this code into the OnLoad event for your window.
private void OnLoad(object sender, EventArgs ea)
{
Rectangle rClient = GetWindowRect(FindChildWindowByClass(FindWindowByTitle("Microsoft Dynamics GP"),"toolStripContainer1");
if (rClient != Rectangle.Empty)
{
this.Location = new Point(rClient.X + rClient.Width/2 - this.Width/2, rClient.Y + rClient.Height - this.Height - 140);
}
}
That's all there is to it. Enjoy!