1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87:
| function PositionWindow(AHandle: HWND; AMonNum: Byte; ACenter, AFit: Boolean; AFullscreen: Boolean = false): Boolean;
function Diff(const a, b: Integer) : Integer; begin if a > b then Result := a - b else Result := b - a; end;
var loc_rect: TRect; loc_x, loc_y, loc_width, loc_height : Integer; loc_CurrScreen, loc_NewScreen: TMonitor;
loc_monitor: Byte; begin Result := False;
loc_monitor := AMonNum; if (loc_monitor >= Screen.MonitorCount) then begin loc_monitor := 0; while (loc_monitor < Screen.MonitorCount) and (not Screen.Monitors[loc_monitor].Primary) do inc(loc_monitor); end;
if GetWindowRect(AHandle, loc_rect) then begin loc_CurrScreen := Screen.Monitors[Screen.MonitorFromWindow(AHandle).MonitorNum]; loc_NewScreen := Screen.Monitors[loc_monitor]; if AFullscreen then with loc_NewScreen.BoundsRect do begin loc_x := Left; loc_y := Top; loc_width := abs(Diff(Left, Right)); loc_height := abs(Diff(Top, Bottom)); end else if ACenter then with loc_NewScreen.BoundsRect do begin loc_x := Left + (abs(Diff(Left, Right)) - Diff(loc_rect.Left, loc_rect.Right)) div 2; loc_y := Top + (abs(Diff(Top, Bottom)) - Diff(loc_rect.Top, loc_rect.Bottom)) div 2; loc_width := Diff(loc_rect.Left, loc_rect.Right); loc_height := Diff(loc_rect.Top, loc_rect.Bottom); end else if (Screen.MonitorFromWindow(AHandle).MonitorNum <> loc_monitor) then with loc_NewScreen.BoundsRect do begin loc_x := Left + abs(loc_CurrScreen.BoundsRect.Left - loc_rect.Left); loc_y := Top + abs(loc_CurrScreen.BoundsRect.Top - loc_rect.Top); loc_width := Diff(loc_rect.Left, loc_rect.Right); loc_height := Diff(loc_rect.Top, loc_rect.Bottom); end else begin loc_x := loc_rect.Left; loc_y := loc_rect.Top; loc_width := Diff(loc_rect.Left, loc_rect.Right); loc_height := Diff(loc_rect.Top, loc_rect.Bottom); end;
if AFit and not AFullscreen then begin if (loc_height >= abs(Diff(loc_NewScreen.BoundsRect.Top, loc_NewScreen.BoundsRect.Bottom))) then loc_height := abs(Diff(loc_NewScreen.BoundsRect.Top, loc_NewScreen.BoundsRect.Bottom)); if (loc_width >= abs(Diff(loc_NewScreen.BoundsRect.Left, loc_NewScreen.BoundsRect.Right))) then loc_width := abs(Diff(loc_NewScreen.BoundsRect.Left, loc_NewScreen.BoundsRect.Right));
if (loc_y < loc_NewScreen.BoundsRect.Top) then loc_y := loc_NewScreen.BoundsRect.Top else if ((loc_y + loc_height) > loc_NewScreen.BoundsRect.Bottom) then loc_y := loc_NewScreen.BoundsRect.Bottom - loc_height;
if (loc_x < loc_NewScreen.BoundsRect.Left) then loc_x := loc_NewScreen.BoundsRect.Left else if ((loc_x + loc_width) > loc_NewScreen.BoundsRect.Right) then loc_x := loc_NewScreen.BoundsRect.Right - loc_width; end;
if MoveWindow(AHandle, loc_x, loc_y, loc_width, loc_height, true) then Result := true; end end; |