这里给出的方法是在 WM_INITDIALOG 事件里实现居中处理。
下面是对话框窗口过程以及主函数:
// EncryptMain.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
BOOL CALLBACK MainDialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG_MAIN), NULL, MainDialogProc);
return 0;
}
BOOL CALLBACK MainDialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_INITDIALOG:
{
HWND hwndOwner = NULL;
RECT rcOwner, rcDlg, rc;
// Get the owner window and dialog box rectangles.
if ((hwndOwner = GetParent(hDlg)) == NULL)
{
hwndOwner = GetDesktopWindow();
}
GetWindowRect(hwndOwner, &rcOwner);
GetWindowRect(hDlg, &rcDlg);