在對話框中設置背景的三種方法 .

方法一:

在OnPaint中StretchBlt

具體是:註釋掉CDialog::OnPaint()或放到結尾(原因何在呢?),並加入貼圖代碼

  1. void CqqqqqDlg::OnPaint()  
  2. {  
  3.     if (IsIconic())  
  4.     {  
  5.         CPaintDC dc(this); // device context for painting   
  6.   
  7.         SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);  
  8.   
  9.         // Center icon in client rectangle   
  10.         int cxIcon = GetSystemMetrics(SM_CXICON);  
  11.         int cyIcon = GetSystemMetrics(SM_CYICON);  
  12.         CRect rect;  
  13.         GetClientRect(&rect);  
  14.         int x = (rect.Width() - cxIcon + 1) / 2;  
  15.         int y = (rect.Height() - cyIcon + 1) / 2;  
  16.   
  17.         // Draw the icon   
  18.         dc.DrawIcon(x, y, m_hIcon);  
  19.     }  
  20.     else  
  21.     {  
  22.         //CDialog::OnPaint();//<span style="color:#6600cc;">註釋此句,如果不註釋的話,就放到結尾,原因何在呢?   
  23. </span>       //貼背景圖片   
  24.         CPaintDC dc(this);  
  25.         CBitmap bmpBk;  
  26.         bmpBk.LoadBitmapW(IDB_BITMAP_tempbk);  
  27.         //m_bmpBK.LoadBitmapW(IDB_BMPBK);   
  28.         BITMAP bmpSize;  
  29.         bmpBk.GetBitmap(&bmpSize);//獲取背景圖片尺寸   
  30.   
  31.         CRect rect;  
  32.         GetClientRect(&rect);//獲取客戶區尺寸   
  33.   
  34.         CDC dcMem;  
  35.         dcMem.CreateCompatibleDC(&dc);  
  36.         dcMem.SelectObject(&bmpBk);  
  37.         dc.StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,bmpSize.bmWidth,bmpSize.bmHeight,SRCCOPY);//將背景圖片拉伸或者壓縮copy至客戶區  
  38.         //貼背景圖片   
  39.     }  
  40. }  


執行效果截圖如下:

方法二:

在OnEraseBkgnd中StretchBlt

具體是:註釋掉return CDialog::OnEraseBkgnd(pDC);直接返回true(爲什麼不能返回這個要返回true呢?),代碼如下:

  1. BOOL CqqqqqDlg::OnEraseBkgnd(CDC* pDC)  
  2. {  
  3.     // TODO: Add your message handler code here and/or call default   
  4.     //貼背景圖片   
  5.     CBitmap bmpBk;  
  6.     bmpBk.LoadBitmapW(IDB_BITMAP_tempbk);  
  7.     //m_bmpBK.LoadBitmapW(IDB_BMPBK);   
  8.     BITMAP bmpSize;  
  9.     bmpBk.GetBitmap(&bmpSize);//獲取背景圖片尺寸   
  10.   
  11.     CRect rect;  
  12.     GetClientRect(&rect);//獲取客戶區尺寸   
  13.   
  14.     CDC dcMem;  
  15.     dcMem.CreateCompatibleDC(pDC);  
  16.     dcMem.SelectObject(&bmpBk);  
  17.     pDC->StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,bmpSize.bmWidth,bmpSize.bmHeight,SRCCOPY);//將背景圖片拉伸或者壓縮copy至客戶區   
  18.     //貼背景圖片   
  19.     return true;  
  20.     //return CDialog::OnEraseBkgnd(pDC);   
  21. }  


效果與方法一相同,圖片就不貼了,參見上圖。

而且這裏還有一個很有意思的現象,若在OnEraseBkgnd貼圖,在OnPaint()函數中不調用基類的OnPaint,即註釋掉CDialog::OnPaint(),則將界面隱藏後再顯示出來則控件全沒了,只有對話框以及背景。如下圖:

 原因見《在OnPaint中必須調用一次BeginPaint和EndPaint,且也只能調用一次。

 

方法三:

在OnCtlColor中返回帶有背景位圖的畫刷

 具體是:

1、在頭文件中定一個背景刷

  1. public:  
  2.     CBrush   m_brushBk;  


2、在OnInitDialog中加入以下句

  1. // TODO: Add extra initialization here   
  2. CBitmap bmp;  
  3. bmp.LoadBitmap(IDB_BITMAP_tempbk);   
  4. m_brushBk.CreatePatternBrush(&bmp);   
  5. //m_brushBk.CreateSolidBrush(RGB(0,255,0)); //用純色作爲背景   
  6.    bmp.DeleteObject();      



 

3、在OnCtlColor函數中返回背景畫刷

  1. HBRUSH CXXXXXDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)  
  2. {  
  3.     HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);  
  4.   
  5.     // TODO:  Change any attributes of the DC here   
  6.   
  7.     // TODO:  Return a different brush if the default is not desired   
  8.     if(pWnd==this//this代表當前對話框窗口   
  9.     {   
  10.         return   m_brushBk;   
  11.     }   
  12.   
  13.     return hbr;  
  14.   
  15. }  

效果如下圖所示:

注意這個函數裏面的if判斷,這個pWnd參數很關鍵。

我們看看,如果沒有這個if判斷,直接返回m_brushBk;會是什麼結果呢,代碼如下:

  1. HBRUSH CqqqqqDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)  
  2. {  
  3.     HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);  
  4.   
  5.     // TODO:  Change any attributes of the DC here   
  6.   
  7.     // TODO:  Return a different brush if the default is not desired   
  8.   
  9.     return  m_brushBk;  
  10.   
  11. }  


截圖如下:
 

 

看圖說話,不解釋,你懂的。

 這種方法的缺點是不具備StretchBlt函數的圖片自動適應對話框(目標矩形)大小的功能。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章