· win32 c net

Controlling window position with the win32 API

We’ve been doing a bit of work around controlling the state of the windows of applications launched programmatically.

The problem we were trying to solve is to launch an arbitrary application, move it around the screen and then save its window position on the screen so that next time it’s launched it loads in the same position.

There are some win32 APIs designed to do just this, although it took a fair bit of searching and trial and error to work out exactly how to use them.

Since the application we wrote to do this is in C# it was fairly easy to import the win32 APIs, the main method call being GetWindowInfo, which is imported like so:

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152~
</td>
[.dl]"[.k]#user32.dll[.dl]"#)]   private static extern bool GetWindowInfo(IntPtr hwnd, ref tagWINDOWINFO pwi);        public struct tagRECT    {        /// LONG->int        public int left;        /// LONG->int        public int top;        /// LONG->int        public int right;        /// LONG->int        public int bottom;    }        public struct tagWINDOWINFO    {        /// DWORD->unsigned int        public uint cbSize;        /// RECT->tagRECT        public tagRECT rcWindow;        /// RECT->tagRECT        public tagRECT rcClient;        /// DWORD->unsigned int        public uint dwStyle;        /// DWORD->unsigned int        public uint dwExStyle;        /// DWORD->unsigned int        public uint dwWindowStatus;        /// UINT->unsigned int        public uint cxWindowBorders;        /// UINT->unsigned int        public uint cyWindowBorders;        /// ATOM->WORD->unsigned short        public ushort atomWindowType;        /// WORD->unsigned short        public ushort wCreatorVersion;    }~
</td>
</tr>
</tbody></table>
We found out how to do this from here, but for the sake of explaining how it works I’ll keep it here too.

The GetWindowInfo’s first argument is a window handler. We launched our application using the .NET Process class so we can access this using the MainWindowHandle property. Don’t use WindowHandle as this doesn’t get the handle to the window itself - I think it gets the handle to the process which launched the window which is not what we want.

Therefore, to get the position of the window on the screen we can use the following code:

123~
</td>
tagWINDOWINFO info = new tagWINDOWINFO();info.cbSize = (uint)Marshal.SizeOf(info);GetWindowInfo(process.MainWindowHandle, ref info);~
</td>
</tr>
</tbody></table>
To put the window back in this position we first need to include the following API calls:
12345~
</td>
[.dl]"[.k]#user32.dll[.dl]")]private [.r]#static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);[.dl]"[.k]#user32.dll[.dl]")]private [.r]#static extern bool UpdateWindow(IntPtr hWnd);~
</td>
</tr>
</tbody></table>
Then call both of these methods like so:
12345~
</td>
process.Start();process.ForceWaitForInputIdle();MoveWindow(process.MainWindowHandle, left, top, width, height, true);UpdateWindow(process.MainWindowHandle);~
</td>
</tr>
</tbody></table>
UpdateWindow needs to be called otherwise the window will remain in its previous position.

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket