把代码放到
BOOL CDlg::OnEraseBkgnd (CDC* pDC)
中显示位图

解决方案 »

  1.   


    void CTestDlg::OnPaint() 
    {
    if (IsIconic())
    {
    CPaintDC dc(this); // device context for painting

    SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

    // Center icon in client rectangle
    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;

    // Draw the icon
    dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
    //载入背景
    CClientDC  dc(this);
    CDC     tempDC;
    CBitmap bitmap;
    BITMAP  bmp;
    //int x = IDB_MAIN_BG;
    bitmap.LoadBitmap(IDB_MAIN_BG);
    bitmap.GetObject (sizeof(BITMAP),&bmp);
    tempDC.CreateCompatibleDC (&dc);
    tempDC.SelectObject (bitmap);
    dc.BitBlt (0,20,bmp.bmWidth ,bmp.bmHeight ,&tempDC,0,0,SRCCOPY);
    //dc.BitBlt (0,20,bmp.bmWidth ,bmp.bmHeight ,&tempDC,0,0,SRCCOPY);
    }
    CDialog::OnPaint();
    }
      

  2.   

    注意:显示位图的代友要在CDialog::OnEraseBkgnd(pDC);之后
      

  3.   

    在OnEraseBkgnd(pDC);里画也不行~求解中
      

  4.   

    如果在OnEraseBkgnd()里画就能进来,哪位有带BMP背景的程序我看看,这样不就简单了;)感谢
      

  5.   

    BOOL CDlg::OnEraseBkgnd (CDC* pDC)
    {
    if(m_Bitmap.GetObjectType())
    {
    CDC dc;
    BITMAP Bmp;
    m_Bitmap.GetBitmap(&Bmp);
    if(!dc.CreateCompatibleDC(pDC))
    return CDialog::OnEraseBkgnd(pDC);
    CBitmap *pOldBmp=dc.SelectObject(&m_Bitmap);
    CRect rect;
    GetWindowRect(&rect);
    int sizex=rect.right-rect.left,sizey=rect.bottom-rect.top;
    int bmpx=Bmp.bmWidth,bmpy=Bmp.bmHeight;
    for(int x=0;x<=sizex;x+=bmpx)
    for(int y=0;y<=sizey;y+=bmpy)
    pDC->BitBlt(x,y,bmpx,bmpy,&dc,0,0,SRCCOPY);
    dc.SelectObject(pOldBmp); return true;
    }
    else
    return CDialog::OnEraseBkgnd(pDC);
      

  6.   

    m_Bitmap在其他地方应该有初始化吧?
      

  7.   

    改了一下,还一样的问题://擦除背景窗口初始化的时候:
    //背景
    m_Bitmap.LoadBitmap(IDB_MAIN_BG);
    BOOL CKnightDlg::OnEraseBkgnd(CDC* pDC)
    {
    //m_Bitmap.
    //载入BMP背景
    if(m_Bitmap.GetObjectType())
    {
    CClientDC  dc(this);
    CDC     tempDC;
    BITMAP  bmp;
    m_Bitmap.GetObject (sizeof(BITMAP),&bmp);
    tempDC.CreateCompatibleDC (&dc);
    tempDC.SelectObject (m_Bitmap);
    dc.BitBlt (0,20,bmp.bmWidth ,bmp.bmHeight ,&tempDC,0,0,SRCCOPY);
    return TRUE;
    }
    else
    return CDialog::OnEraseBkgnd(pDC);
      

  8.   

    Mothod1:是不是要把全部控件都重画一遍?由哪个消息促发重画?--------------------------------------------use EnumChildWindows to find all child window.->force repaint 
    GetWindowRect(hChildWindow,&rect)
    InvalidateRect(hChildWindow, &rect, TRUE)this method is not good enough, but it work.
    ----------------------
    Method2:
    make sure all child window's Z-order is greader than Dlg window itself.Use SetWindowPos to change child window Z-Order.
    do in wm_initdialog maybe safe(child has been created)-----------------------
    Method3: (correct method)dealing in WM_CTLCOLOR(DLG)