9

I want to maximize a random window on the left side of my screen. Can I use Windows Aero functions from my code ? This window can be maximized like that with the mouse. I just want to do that programmatically.

I use C# and I can get the IntPtr of the window.

If possible without faking mouse or keyboard input.

Bitterblue
  • 13,162
  • 17
  • 86
  • 124
  • Unfortunately not, Microsoft in their wisdom did not provide any public API for the aero snap functionality. – Jonathan Potter Aug 19 '13 at 12:23
  • 1
    This can be done without p/invoke, Just not with ease, – string.Empty Aug 19 '13 at 12:24
  • Can't you mimick this behavior by setting the window position and size, based on the desktop size? Should be easy enough, just not very "clean" – Kippie Aug 19 '13 at 12:26
  • Just pinvoke MoveWindow(). – Hans Passant Aug 19 '13 at 13:16
  • @HansPassant Did that already combining it with the answer of Nicolas. But I'm still kinda interested in using Aero. – Bitterblue Aug 19 '13 at 13:18
  • There is no Aero specific function for this. There just isn't any need when you can use MoveWindow. – Hans Passant Aug 19 '13 at 13:23
  • Not to argue with @HansPassant or anything, but `MoveWindow` is not exactly functionally equivalent to aero snap. If you aero snap a window to the side of the screen, you can double-click its title bar to restore it. If you `MoveWindow` a window to the side of the screen, double-clicking its title bar will maximize it. – Jonathan Potter Aug 19 '13 at 19:33
  • You could fake that as well by storing the locations before MoveWindow, then perform another MoveWindow when the window is double clicked. – string.Empty Aug 21 '13 at 05:53

3 Answers3

7

This can be done without p/invoke.

Try this:

Rectangle rect = Screen.PrimaryScreen.WorkingArea;
rect.Width = rect.Width / 2;
Bounds = rect;

This will put the current window on the left of the primary screen.

Then just add this to put it on the right of the screen.

Location = new Point(rect.Width, 0);
string.Empty
  • 10,393
  • 4
  • 39
  • 67
  • I didn't accept because the window state isn't maximized. I tried it without WinApi (setting FormWindowState etc.) but it doesn't work out for some reason. – Bitterblue Dec 10 '14 at 15:38
4

It's not exactly the same but fakes it well:

ShowWindow(handle, SW_MAXIMIZE);
// for a split second you might see a maximized window here
MoveWindow(handle, 0, 0, Screen.PrimaryScreen.WorkingArea.Width / 2, Screen.PrimaryScreen.WorkingArea.Height, true);
Bitterblue
  • 13,162
  • 17
  • 86
  • 124
0

Will require heavy lifting for real-time placements, especially for non-child processes. Example - www.ishadow.com/vdm. For manual "fixing" of maximized windows position MoveWindow(hWnd, startPos, 0, winWidth, winHeight, true) after ShowWindow(hWnd, SW_MAXIMIZE) usually (try Task Manager on Windows 10) works as pointed above.

Alex
  • 1
  • 2