我做了一个Dialog,想为对话框添加一张背景图片,请问在哪里添加什么代码呢?
还有就是我在App的Inistance()里添加代码SetDialogBkColor(RGB(0, 0,255),RGB(0,255,255)); 每个窗口的背景色都变成上述代码的颜色了,请问如何能只改变一个特定的对话框的背景颜色呢?

解决方案 »

  1.   

    一个简单的方法:用一个picture控件填满窗口
      

  2.   

     在需要改变背景的窗口的OnPaint函数里面
     void CExampleDlgDlg::OnPaint() 
    {
        if (IsIconic())

      else
      {
            CRect rect;
            CPaintDC dc(this);
            GetClientRect(rect);
            dc.FillSolidRect(rect,RGB(0,255,0));  //设置为绿色背景        CDialog::OnPaint();
      }
      

  3.   

    SetDialogBkColor已经过时,而且这个确实是设置整个线程的对话框颜色的,
    如果想改变颜色的话可以响应WM_CTLCOLOR
    如果是想改变对话框背景的话,个人感觉最好是响应WM_ERASEBKGND,返回一个位图画刷,或者自己在里面画
      

  4.   

    你参考下以下代码吧,以前做的:
    BOOL CNewsDlg::OnEraseBkgnd(CDC* pDC)
    {
    // TODO: Add your message handler code here and/or call default
    CRect rect;
    BITMAP bmp;
    CDC dcCompatible;
    m_bitmap.GetBitmap(&bmp);  //m_bitmap 是CBitmap
    dcCompatible.CreateCompatibleDC(pDC);
    CBitmap *pOlBitmap = dcCompatible.SelectObject(&m_bitmap);
    GetWindowRect(&rect);
    pDC->SetStretchBltMode(COLORONCOLOR);
    pDC->StretchBlt(0,0,rect.Width(),rect.Height(),&dcCompatible,
    0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
    dcCompatible.SelectObject(pOlBitmap);
    DeleteDC(dcCompatible.m_hDC); return TRUE; 
    //return CDialog::OnEraseBkgnd(pDC);
    }
      

  5.   

    1.  先载入一张图片,ID为IDB_BITMAP1     在TestDlg.h中 CBrush m_brBk;//在public中定义 TestDlg.cpp中      在初始化函数OnInitDialog()中加入: BOOL CTestDlg::OnInitDialog() {  CDialog::OnInitDialog(); CBitmap bmp; bmp.LoadBitmap(IDB_BITMAP1); m_brBk.CreatePatternBrush(&bmp); bmp.DeleteObject(); 
    return TRUE;  // return TRUE  unless you set the focus to a control } 
         在打开类向导,找到WM_CTLCOLOR消息,重载得对应函数OnCtlColor(),添加如下: HBRUSH  CTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { 
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); 
    if (pWnd == this) { 
        return m_brBk; 

      return hbr; }
      

  6.   

    就是上面所说的,两种方法,一个是onpain,一个是OnEraseBkgnd