Overview

In one of my applications (small light-weight text editor) I chose to display messages which required decisions using a TTaskDialog rather than use the MessageDlg() function. One of the irritations of this system dialogue (like MessageDlg ) is that it positions the dialogue in the middle of the primary monitor (I don’t have multiple monitors at home – yet, so that’s an assumption) rather than in the middle of the applications main form. So I wonder whether I could change this behaviour.

The Solution

So I first looked to see if the component TTaskDialog has any properties that would give me access to the size and position of the dialogue but unfortunately it didn’t however there are a number of event handler for the dialogue, one of them being OnDialogConstructed .

The event handler is a standard TNotifyEvent , i.e. just the Sender is provided. This is the TTaskDialog when it is called. I looked through the properties of the task dialogue and saw that it had a handle which I thought might be the handle of the actual dialogue so I used the WinAPI call GetWindowRect() to see if this was true and it was. So I took the information, modified it based on my main form position and then used SetWindowPos() to re-position the task dialogue and it worked.

So the below code will centre the TTaskDialog in the centre of the form when you call it.

Procedure TfrmGEMainForm . dlgTaskDialogConstructed ( Sender : TObject ) ; Var TD : TTaskDialog ; R : TRect ; Begin If Sender Is TTaskDialog Then Begin TD := Sender As TTaskDialog ; Win32Check ( GetWindowRect ( TD . Handle , R ) ) ; R . Left := Left + Width Div 2 - ( R . Right - R . Left ) Div 2 ; R . Top := Top + Height Div 2 - ( R . Bottom - R . Top ) Div 2 ; Win32Check ( SetWindowPos ( TD . Handle , HWND_TOP , R . Left , R . Top , R . Right - R . Left , R . Bottom - R . Top , SWP_SHOWWINDOW ) ) ; End ; End ;

Hope you find this useful.

D.

Update 12 Mar 2018