/// <summary> /// Set position and size to window /// </summary> /// <param name="windowHandle"></param> /// <param name="position"> /// Position and size. If width < 0 maximized, if height < 0 minimized. If height and width are 0 ignore size /// </param> public static void Relocate(IntPtr windowHandle, Rectangle position) { var placement = new WindowPlacement.WINDOWPLACEMENT(); placement.Length = Marshal.SizeOf(typeof(WindowPlacement.WINDOWPLACEMENT)); // Get current placement WindowPlacement.GetWindowPlacement(windowHandle, ref placement); var normalWidth = placement.NormalPosition.Width; var normalHeight = placement.NormalPosition.Height; placement.ShowCmd = WindowPlacement.ShowWindowCommands.Restore; placement.Flags = 0; placement.NormalPosition.Top = position.Top; placement.NormalPosition.Left = position.Left; ////Restore if minimized //if (placement.ShowCmd == WindowPlacement.ShowWindowCommands.ShowMinimized) //{ // placement.ShowCmd = WindowPlacement.ShowWindowCommands.Normal; //} var width = position.Width; var height = position.Height; if (width == 0 || height == 0) { // ignoring size width = normalWidth; height = normalHeight; } else if (width < 0 && height < 0) { width *= -1; height *= -1; } else if (width < 0) { width *= -1; placement.ShowCmd = WindowPlacement.ShowWindowCommands.Maximize; } else if (height < 0) { height *= -1; placement.ShowCmd = WindowPlacement.ShowWindowCommands.ShowMinimized; } placement.NormalPosition.Bottom = placement.NormalPosition.Top + height; placement.NormalPosition.Right = placement.NormalPosition.Left + width; WindowPlacement.SetWindowPlacement(windowHandle, ref placement); // Show at top WindowPlacement.SetForegroundWindow(windowHandle); }
/// <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)); }
/// <summary> /// Get position and size of window /// </summary> /// <param name="windowHandle"></param> /// <returns></returns> public static Rectangle GetPlace(IntPtr windowHandle) { var placement = new WindowPlacement.WINDOWPLACEMENT(); placement.Length = Marshal.SizeOf(typeof(WindowPlacement.WINDOWPLACEMENT)); WindowPlacement.GetWindowPlacement(windowHandle, ref placement); var position = placement.NormalPosition; var minimized = placement.ShowCmd == WindowPlacement.ShowWindowCommands.ShowMinimized; var maximized = placement.ShowCmd == WindowPlacement.ShowWindowCommands.Maximize; return(new Rectangle( position.Left, position.Top, (maximized ? -1 : 1) * (position.Right - position.Left), (position.Bottom - position.Top))); }
/// <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); }