Created
June 16, 2015 23:30
-
-
Save tom-seddon/8792dd9c0cd32ffc8de7 to your computer and use it in GitHub Desktop.
GetWindowPlacement and SetWindowPlacement code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This code is in the public domain. | |
using System; | |
using System.Collections.Generic; | |
using System.Runtime.InteropServices; | |
using System.Text; | |
using System.Windows.Forms; | |
namespace Utils | |
{ | |
////////////////////////////////////////////////////////////////////////// | |
[StructLayout(LayoutKind.Sequential, Pack = 1)] | |
public class WindowPlacement | |
{ | |
////////////////////////////////////////////////////////////////////////// | |
[StructLayout(LayoutKind.Sequential, Pack = 1)] | |
public struct POINTAPI | |
{ | |
public int x; | |
public int y; | |
} | |
////////////////////////////////////////////////////////////////////////// | |
[StructLayout(LayoutKind.Sequential, Pack = 1)] | |
public struct RECT | |
{ | |
public int left; | |
public int top; | |
public int right; | |
public int bottom; | |
} | |
////////////////////////////////////////////////////////////////////////// | |
public int length; | |
public int flags; | |
public int showCmd; | |
public POINTAPI ptMinPosition; | |
public POINTAPI ptMaxPosition; | |
public RECT rcNormalPosition; | |
////////////////////////////////////////////////////////////////////////// | |
public WindowPlacement() | |
{ | |
this.length = 0; | |
} | |
////////////////////////////////////////////////////////////////////////// | |
public static WindowPlacement Get(Form form) | |
{ | |
WindowPlacement placement = new WindowPlacement(); | |
placement.length = Marshal.SizeOf(placement); | |
if (!GetWindowPlacement(form.Handle, placement)) | |
return null; | |
return placement; | |
} | |
////////////////////////////////////////////////////////////////////////// | |
public static void Set(Form form, WindowPlacement placement) | |
{ | |
if (placement != null) | |
SetWindowPlacement(form.Handle, placement); | |
} | |
////////////////////////////////////////////////////////////////////////// | |
[DllImport("user32.dll")] | |
[return: MarshalAs(UnmanagedType.Bool)] | |
private static extern bool GetWindowPlacement(IntPtr hWnd, WindowPlacement lpwndpl); | |
////////////////////////////////////////////////////////////////////////// | |
[DllImport("user32.dll")] | |
private static extern bool SetWindowPlacement(IntPtr hWnd, [In] WindowPlacement lpwndpl); | |
////////////////////////////////////////////////////////////////////////// | |
} | |
////////////////////////////////////////////////////////////////////////// | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment