MFC對話框背景圖片的設置
1.插入一個Bitmap的資源圖片,假設資源名稱爲:IDC_BITMAP1(一般默認的第一個BMP文件是它,接下來的爲IDC_BITMAP2,以此類推)
2.在CXXXDialog::OnPaint()中實現:
void CMyDialogDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用於繪製的設備上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使圖標在工作區矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 繪製圖標
dc.DrawIcon(x, y, m_hIcon);
}
else
{
//
// 給窗體添加背景
//
CPaintDC dc(this);
CRect rc;
GetClientRect(&rc);
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
CBitmap bmpBackground;
bmpBackground.LoadBitmap(IDB_BITMAP1);
BITMAP bitmap;
bmpBackground.GetBitmap(&bitmap);
CBitmap* pbmpPri = dcMem.SelectObject(&bmpBackground);
dc.StretchBlt(0,0,rc.Width(), rc.Height(), &dcMem,0,0,bitmap.bmWidth, bitmap.bmHeight, SRCCOPY);
}
}