软件开发中,我们通常需要设置对话框到我们需要的大小,并且希望能在我们希望的位置显示,那么就需要设置对话框的大小和位置了。
步骤:
1.新建对话框
新建对话框应用程序,为了方便对比,我们还另外新建两个对话框。
2.设置对话框大小和位置
为了方便对比,一个对话框使用默认效果,另一个对话框重写OnInitDialog函数。
BOOL CDialog2::OnInitDialog()
{
CDialog::OnInitDialog();
//对话框背景图片宽和高
const int nBkBmpWidth = 480;
const int nBkBmpHeight = 300;
int xPos = 0;
int yPos = 0;
//获得电脑显示器的像素宽度和像素高度
int ax = GetDC()->GetDeviceCaps(HORZRES) - nBkBmpWidth;
int ay = GetDC()->GetDeviceCaps(VERTRES) - nBkBmpHeight;
int nWidth = 0;
int nHeight = 0;
if(ax <= 0)
{ ax = 0; }
else
{ ax = ax/2; }
if(ay <= 0)
{ ay = 0; }
else
{ ay = ay/2; }
RECT clientRect;
RECT rt;
clientRect.left = ax;
clientRect.top = ay;
clientRect.right = clientRect.left + nBkBmpWidth;
clientRect.bottom = clientRect.top + nBkBmpHeight;
MoveWindow(&clientRect);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
设置后效果