本篇文章属于《518抽奖软件开发日志》系列文章的一部分。
我在开发《518抽奖软件》(www.518cj.net)的时候,需要对话框居中的功能。对话框默认是居中的,但是要移动对话框到第二屏并居中,所以要自己实现代码。通过关键函数SystemParametersInfo~SPI_GETWORKAREA获得屏幕除任务栏外的区域。具体代码如下。
void Tfuns::center_box(HWND hDlg)
{
RECT desk = { 0 };
SystemParametersInfo(SPI_GETWORKAREA, 0, &desk, 0);
int cx = desk.right - desk.left;
int cy = desk.bottom - desk.top;
RECT rc = { 0 };
GetWindowRect(hDlg, &rc);
int wnd_w = rc.right - rc.left;
int wnd_h = rc.bottom - rc.top;
int x = (cx - wnd_w) / 2 + desk.left;
if (x < 0) x = 0;
int y = (cy - wnd_h) / 2 + desk.top;
if (y < 0) y = 0;
SetWindowPos(hDlg, NULL, x, y, 0, 0, SWP_NOSIZE);
}