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. |
About the author
I'm currently working on short form content at ClickHouse. I publish short 5 minute videos showing how to solve data problems on YouTube @LearnDataWithMark. I previously worked on graph analytics at Neo4j, where I also co-authored the O'Reilly Graph Algorithms Book with Amy Hodler.