我现在可以给对话框里加入背景图了,可是我一调整对话框的大小后,上次显示的图片会覆盖新的图片,这是我的代码,请高手帮帮忙!谢谢
void CtestDlg::OnPaint() 
{
CPaintDC dc(this);

GetClientRect(&rect); m_Bmap=(HBITMAP)::LoadImage(NULL,cc,IMAGE_BITMAP,rect.right,rect.bottom,LR_LOADFROMFILE);

memdc=::CreateCompatibleDC(dc.m_hDC);
::SelectObject(memdc,m_Bmap); BitBlt(dc.m_hDC,0,0,rect.right,rect.bottom,memdc,0,0,SRCCOPY);
}

解决方案 »

  1.   

    你上边在OnPaint里有加载原图片的代码,所以你无论在任何地方加载新图片,最后Invalidate的时候调用OnPaint却又重新加载原始图片了.所以永远都是显示原图片
    OnPaint下只要一行BitBlt(dc.m_hDC,0,0,rect.right,rect.bottom,memdc,0,0,SRCCOPY);
    把创建设备DC/加载图片的代码放在一个函数中,当调整对话框大小的时候,调用这个函数,再调用一下Invalidate另外StretchBlt函数可以放大/缩小图片,参数与bitblt一样
      

  2.   

    这样的话Invalidata()会把以前对话框上的按钮刷掉,不知道有没有解决的办法啊,或者这样说在那里加入Invalidata()才不会重绘以前的按钮
      

  3.   

    对于sscad001的说的那个问题,有一跟Invalidate相似的函数可以达到你目的,InvalidateRect/InvalidateRgn对指定的"某区域"做Invalidate
    void InvalidateRect( LPCRECT lpRect, BOOL bErase = TRUE );
    void InvalidateRgn( CRgn* pRgn, BOOL bErase = TRUE );以我浅浅的@_@理解rect与rgn在很多时候的区别就是一个是规则区域(正方形,圆形..),另一个是"不是很规则"的区域吧
    Invalidate实际上调用OnDraw/Onpaint
    Invalidate(true)//"关闭视图,激活OnDraw
    Invalidate(flase)//激活视图并调用OnPaint
    既然知道了它的作用(其实应该深点理解OnDraw/OnPaint的调用方式/过程,对以后写重绘是相当大帮助的,CWnd*自绘,DrawItem的调用方式与OnDraw很相似)void Invalidate( BOOL bErase = TRUE );
    Parameters
    bErase
    Specifies whether the background within the update region is to be erased.Res
    Invalidates the entire client area of CWnd. The client area is ed for painting when the next WM_PAINT message occurs. The region can also be validated before a WM_PAINT message occurs by the ValidateRect or ValidateRgn member function.The bErase parameter specifies whether the background within the update area is to be erased when the update region is processed. If bErase is TRUE, the background is erased when the BeginPaint member function is called; if bErase is FALSE, the background remains unchanged. If bErase is TRUE for any part of the update region, the background in the entire region, not just in the given part, is erased. Windows sends a WM_PAINT message whenever the CWnd update region is not empty and there are no other messages in the application queue for that window.