Mirror

How to center a TOpenDialog on a form (Views: 100)


Problem/Question/Abstract:

How to center a TOpenDialog on a form

Answer:

{ ... }
type
  TForm1 = class(TForm)
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    procedure Button1Click(Sender: TObject);
    procedure OpenDialog1Show(Sender: TObject);
  private
    { Private declarations }
    procedure MoveDialog(var Msg: TMessage); message WM_USER;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  OpenDialog1.Execute;
end;

procedure TForm1.OpenDialog1Show(Sender: TObject);
begin
  PostMessage(Self.Handle, WM_USER, 0, 0);
end;

procedure TForm1.MoveDialog(var Msg: TMessage);
var
  rec: TRect;
  wh: HWND;
  l, t, r, b: Integer;
begin
  if ofOldStyleDialog in OpenDialog1.Options then
    wh := OpenDialog1.Handle
  else
    wh := Windows.GetParent(OpenDialog1.Handle);
  if IsWindow(wh) then
    if GetWindowRect(wh, rec) then
    begin
      l := (Width - (rec.Right - rec.Left)) div 2 + Left;
      t := (Height - (rec.Bottom - rec.Top)) div 2 + Top;
      r := rec.Right - rec.Left;
      b := rec.Bottom - rec.Top;
      MoveWindow(wh, l, t, r, b, True);
    end;
end;

<< Back to main page