Skip to content

Instantly share code, notes, and snippets.

@tom-seddon
Created June 16, 2015 23:30
Show Gist options
  • Save tom-seddon/8792dd9c0cd32ffc8de7 to your computer and use it in GitHub Desktop.
Save tom-seddon/8792dd9c0cd32ffc8de7 to your computer and use it in GitHub Desktop.
GetWindowPlacement and SetWindowPlacement code
// 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