/// <summary> /// ウインドウを指定された位置と大きさに配置 /// </summary> /// <param name="window"></param> /// <param name="positon"></param> public static void Relocate(this WindowInformation window, Rectangle positon) { var placement = new WindowPlacement.WINDOWPLACEMENT(); placement.Length = Marshal.SizeOf(typeof(WindowPlacement.WINDOWPLACEMENT)); WindowPlacement.GetWindowPlacement(window.Handle, ref placement); int width = placement.NormalPosition.Right - placement.NormalPosition.Left; int height = placement.NormalPosition.Bottom - placement.NormalPosition.Top; placement.ShowCmd = WindowPlacement.ShowWindowCommands.Restore; placement.NormalPosition.Top = positon.Top; placement.NormalPosition.Left = positon.Left; if (positon.Top >= positon.Bottom || positon.Right <= positon.Left) { placement.NormalPosition.Bottom = placement.NormalPosition.Top + height; placement.NormalPosition.Right = placement.NormalPosition.Left + width; } else { placement.NormalPosition.Bottom = positon.Bottom; placement.NormalPosition.Right = positon.Right; } placement.Flags = 0; //最小化されていたら元に戻す placement.ShowCmd = (placement.ShowCmd == WindowPlacement.ShowWindowCommands.ShowMinimized) ? WindowPlacement.ShowWindowCommands.Normal : placement.ShowCmd; WindowPlacement.SetWindowPlacement(window.Handle, ref placement); //最前面に表示 WindowPlacement.SetForegroundWindow(window.Handle); }
/// <summary> /// ウインドウの位置と大きさを取得 /// </summary> /// <param name="window"></param> /// <returns></returns> public static Rectangle GetPlacement(this WindowInformation window) { var placement = new WindowPlacement.WINDOWPLACEMENT(); placement.Length = Marshal.SizeOf(typeof(WindowPlacement.WINDOWPLACEMENT)); WindowPlacement.GetWindowPlacement(window.Handle, ref placement); var position = placement.NormalPosition; return new Rectangle(position.Left, position.Top, position.Right - position.Left, position.Bottom - position.Top); }