1、添加背景圖片到“Bitmap”資源裡。方法為“插入——資源”,如果還沒有Bitmap,則建立一個Bitmap,否則選擇資源類型為“Bitmap”後“引入”想要插入的背景圖片。
2、定位到 void CXXXDlg::OnPaint(),在if()...else()中的else()下添加如下代碼:
else
{
//CDialog::OnPaint();//要禁止這個調用
CPaintDC dc(this);
CRect rect;
GetClientRect(&rect);
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
CBitmap bmpBackground;
bmpBackground.LoadBitmap(IDB_BITMAP); //IDB_BITMAP是你自己的圖對應的ID ,由於我剛剛加入的位元影像資源
//被我命名成了IDB_Bg,因而我這句就是bmpBackground.LoadBitmap(IDB_Bg);
BITMAP bitmap;
bmpBackground.GetBitmap(&bitmap);
CBitmap *pbmpOld=dcMem.SelectObject(&bmpBackground);
dc.StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,
bitmap.bmWidth,bitmap.bmHeight,SRCCOPY);
}
void C學習Dlg::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{ //CDialogEx::OnPaint();//要禁止這個調用 CPaintDC dc(this); CRect rect; GetClientRect(&rect); CDC dcMem; dcMem.CreateCompatibleDC(&dc); CBitmap bmpBackground; bmpBackground.LoadBitmap(IDB_FM); //IDB_FM是你自己的圖對應的ID ,由於我剛剛加入的位元影像資源 //被我命名成了IDB_FM,因而我這句就是bmpBackground.LoadBitmap(IDB_Bg); BITMAP bitmap; bmpBackground.GetBitmap(&bitmap); CBitmap *pbmpOld=dcMem.SelectObject(&bmpBackground); dc.StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0, bitmap.bmWidth,bitmap.bmHeight,SRCCOPY); } }
3、編譯運行結果