Initializing a Dialog Box

You initialize the dialog box and its contents while processing the WM_INITDIALOG message. The most common task is to initialize the controls to reflect the current dialog box settings. Another common task is to center a dialog box on the screen or within its owner window. A useful task for some dialog boxes is to set the input focus to a given control rather than accept the default input focus.

In the following example, the dialog box procedure centers the dialog box and sets the input focus while processing the WM_INITDIALOG message. To center the dialog box, the procedure retrieves the window rectangles for the dialog box and the owner window and calculates a new position for the dialog box. To set the input focus, the procedure checks the wParam parameter to determine the identifier of the default input focus:

HWND hwndOwner;

RECT rc, rcDlg, rcOwner;

case WM_INITDIALOG:

// Get the owner window and dialog box rectangles.

if ((hwndOwner = GetParent(hwndDlg)) == NULL)

hwndOwner = GetDesktopWindow();

GetWindowRect(hwndOwner, &rcOwner);

GetWindowRect(hwndDlg, &rcDlg);

CopyRect(&rc, &rcOwner);

// Offset the owner and dialog box rectangles so that

// right and bottom values represent the width and

// height, and then offset the owner again to discard

// space taken up by the dialog box.

OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);

OffsetRect(&rc, -rc.left, -rc.top);

OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);

// The new position is the sum of half the remaining

// space and the owner's original position.

SetWindowPos(hwndDlg,

HWND_TOP,

rcOwner.left + (rc.right / 2),

rcOwner.top + (rc.bottom / 2),

0, 0, // ignores size arguments

SWP_NOSIZE);

if (GetDlgCtrlID((HWND) wParam) != ID_ITEMNAME) {

SetFocus(GetDlgItem(hwndDlg, ID_ITEMNAME));

return FALSE;

}

return TRUE;

In the preceding statements, the procedure uses the GetParent function to retrieve the owner window handle for a dialog box. The function returns the owner window handle for dialog boxes, and the parent window handle for child windows. Because an application can create a dialog box that has no owner, the procedure checks the returned handle and uses the GetDesktopWindow function to retrieve the desktop window handle, if necessary. After calculating the new position, the procedure uses the SetWindowPos function to move the dialog box, specifying the HWND_TOP value to ensure that the dialog box remains on top of the owner window.

Before setting the input focus, the procedure checks the control identifier of the default input focus. Windows passes the window handle of the default input focus in the wParam parameter. The GetDlgCtrlID function returns the identifier for the control identified by the window handle. If the identifier does not match the correct identifier, the procedure uses the SetFocus function to set the input focus. The GetDlgItem function is required to retrieve the window handle of the desired control.

Software for developers
Delphi Components
.Net Components
Software for Android Developers
More information resources
MegaDetailed.Net
Unix Manual Pages
Delphi Examples
Databases for Amazon shops developers
Amazon Categories Database
Browse Nodes Database