DlgCenter, DlgCenterHook()

Requires: GfaWinx.lg32

Purpose

Ensure Dialog boxes are centered on top of their owner.

Syntax

DlgCenter
Set Obj = DlgCenterHook()

Obj: a local Object variable

Description

These commands will center all of GFA-BASIC’s dialog boxes of the current thread on top of their owner.

DlgCenter only works for the program’s main thread. DlgCenterHook is used in additional threads the program creates. The dialogs that are centered include all dialogs that have an owner, like MsgBox (but not MsgBox0), Alert, Prompt, Dlg Open/Save and others, and the CommDlg Ocx dialogs.

DlgCenter installs a thread-local hook to intercept the creation of dialogs for the main thread. It creates a hidden global AutoFreePtr Object that unhooks the thread’s hook when the object variable goes out of scope (after the application is closed). DlgCenter calls DlgCenterHook to obtain the object and stores it in the hidden global variable.

To center dialogs created in an additional thread use DlgCenterHook. The object returned by DlgCenterHook must be stored in a thread local variable. The local object variable must be declared in the ThreadProc and is then assigned the object returned from DlgCenterHook. When the thread ends the local thread object variable is freed and the thread’s hook is unhooked.

Example

Example 1 - Single thread

$Library "gfawinx"

$Library "UpdateRT"

UpdateRuntime      ' Patches GfaWin23.Ocx

OpenW 1, 0, 0, 300, 300

DlgCenter

Alert 1, "Centered AlertBox", 1, "Ok|Cancel"

CloseW 1

Example 2 - Multiple threads

$Library "gfawinx"

$Library "UpdateRT"

UpdateRuntime                           ' Patches GfaWin23.Ocx

OpenW 1

DlgCenter                               ' for main thread

Local threadID As Long

Global hThread As Handle, oThread As Object

Global Handle hThread = CreateThread(Null, 0, ProcAddr(ThreadProc), 0, 0, V:threadID)

Set oThread = AutoFreePtr(hThread, AfpCloseHandle)      ' API function, handle must be closed when finished

Do

Sleep

Until Me Is Nothing

 

Procedure ThreadProc(lp%)

Local Object oDlgCtrHk                ' Local!!

Set oDlgCtrHk = DlgCenterHook()       ' center dialogs in this thread

// Code for new thread

EndProcedure                            ' unhooks the hook saved in oDlgCtrHk

Remarks

See Also

MsgBox, Message, Alert, Prompt, CommDlg.

{Created by Sjouke Hamstra; Last updated: 23/02/2022 by James Gaite}