Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Library: Windows API / MS.NET Framework API (https://www.delphipraxis.net/20-library-windows-api-ms-net-framework-api/)
-   -   Delphi Childfenster anhand von Parent zentrieren (https://www.delphipraxis.net/55468-childfenster-anhand-von-parent-zentrieren.html)

Rastaman 21. Okt 2005 14:28


Childfenster anhand von Parent zentrieren
 
Diese Funktion zentriert ein Childfenster anhand ihres Parents (z.B Form1 und dem Desktop).
Da die Funktion mit SetWindowPos arbeitet kann man auch hier den Z-Achsen Wert des Fensters angeben (z.B HWND_TOPMOST).
Einfach mal F1 in Delphi drücken ;)

Delphi-Quellcode:
function CenterWindow(WndParent, WndChild: HWND; WndPos: Cardinal): Bool;
var
  PRec,
  CRec: TRect;
begin
  // Fensterkoordinaten ins TRect laden
  GetClientRect(WndParent, PRec);
  GetClientRect(WndChild, CRec);
  // Parent muss größer sein als Child
  if ((PRec.Right - PRec.Left) < (CRec.Right - CRec.Left)) or
     ((PRec.Bottom - PRec.Top) < (CRec.Bottom - CRec.Top)) then
  begin
    Result := False;
    Exit;
  end;

  // Position setzen
  SetWindowPos(WndChild, WndPos,
               (PRec.Right - CRec.Right) div 2,
               (PRec.Bottom - CRec.Bottom) div 2,
               0, 0,
               SWP_NOSIZE or SWP_NOACTIVATE);
  Result := True;
end;
Beispielaufruf:
Delphi-Quellcode:
var
  wnd: HWND;
begin
  wnd := FindWindow('ProgMan', nil);
  if CenterWindow(wnd, Form1.Handle, HWND_TOP) then
    ShowMessage('Zentriert')
  else
    ShowMessage('Zentrieren fehlgeschlagen');
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 14:54 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz