WIN32创建居中对话框
程序员文章站
2022-03-02 14:52:49
这里给出的方法是在 WM_INITDIALOG 事件里实现居中处理。下面是对话框窗口过程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 wind...
这里给出的方法是在 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);
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(hDlg,
HWND_TOP,
rcOwner.left + (rc.right / 2),
rcOwner.top + (rc.bottom / 2),
0, 0, // Ignores size arguments.
SWP_NOSIZE);
return TRUE;
}
case WM_CLOSE:
{
EndDialog(hDlg, 0);
return TRUE;
}
}
return FALSE;
}
本文地址:https://blog.csdn.net/Kwansy/article/details/107266963