Delphi TTaskDialog position on open

384 views Asked by At

Via Delphi 10.2.3: TTaskDialog always opens positioned at screen center, and since it doesn't have a Position property, there doesn't appear to be a straightforward way to override that behavior. I want all my TTaskDialogs to position at poMainFormCenter. Short of writing a replacement for TTaskDialog, Is there a way to force this behavior?

1

There are 1 answers

1
Andreas Rejbrand On BEST ANSWER

You might not be aware of the Flags property and the tfPositionRelativeToWindow flag:

If set, [the] Task Dialog is centered with respect to [its] parent window.

with TTaskDialog.Create(Self) do
  try
    Caption := Self.Caption;
    MainIcon := tdiNone;
    Title := 'Do you want to create a new batch of frogs?';
    CommonButtons := [tcbYes, tcbNo];
    Flags := [tfPositionRelativeToWindow];
    Execute;
  finally
    Free
  end;

Strictly speaking, this positions the task dialog relative to the parent form, not the main form, but I suspect this is what you actually do want.

Screenshot of task dialog centred above its parent form

The tfPositionRelativeToWindow flag maps to the TDF_POSITION_RELATIVE_TO_WINDOW flag of the underlying API call to TaskDialogIndirect:

Indicates that the task dialog is positioned (centered) relative to the window specified by hwndParent. If the flag is not supplied (or no hwndParent member is specified), the task dialog is positioned (centered) relative to the monitor.