C#:Window Location
ウィンドウの「標準時」の大きさを取得するAPI。
最大化してたり最小化してたりするのに惑わされずに済みます。
これで取得した位置をリストアするときはForm.Locationに代入すると良いです。
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Std
{
static partial class WinForms
{
/// <summary>
/// ウィンドウの標準時の大きさを取得します。
/// </summary>
/// <param name="form">取得する元のウィンドウ</param>
/// <returns>標準時のサイズ</returns>
public static Rectangle GetNormalWindowLocation(Form form)
{
//WinAPI GetWindowPlacement関数を使うと、ウィンドウの状態に関係なく、
//「通常時」のウィンドウのサイズを取得できます。
WINDOWPLACEMENT wndpl = new WINDOWPLACEMENT();
wndpl.Length = Marshal.SizeOf(wndpl);
GetWindowPlacement((int)form.Handle, ref wndpl);
return new System.Drawing.Rectangle(
wndpl.rcNormalPosition.left,
wndpl.rcNormalPosition.top,
wndpl.rcNormalPosition.right - wndpl.rcNormalPosition.left,
wndpl.rcNormalPosition.bottom - wndpl.rcNormalPosition.top);
}
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int x, y;
public POINT(int x, int y) { this.x = x; this.y = y; }
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left; public int top; public int right; public int bottom;
public RECT(int left, int top, int right, int bottom) { this.left = left; this.top = top; this.right = right; this.bottom = bottom; }
}
[StructLayout(LayoutKind.Sequential)]
struct WINDOWPLACEMENT { public int Length; public int flags; public int showCmd; public POINT ptMinPosition; public POINT ptMaxPosition; public RECT rcNormalPosition; }
[DllImport("user32.dll")]
extern static bool GetWindowPlacement(int hWnd, ref WINDOWPLACEMENT lpwndpl);
}
}

