Windows編程入門(2)-MFC中給對話方塊添加位元影像

來源:互聯網
上載者:User

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、編譯運行結果


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.