Copy a background image and save in CImage

IDH 21 Reputation points
2021-06-16T08:30:40.063+00:00

Hi,
I am attempting to emulate transparency. My background is drawn in a CView class via OnEraseBkgnd using the CImage.Draw method:

pDoc->m_imgBitmap[pDoc->m_iPage-1].Draw(pDC->m_hDC, point);

I have saved the pDC, thus:

pDoc->m_pDCBitmap[pDoc->m_iPage - 1] = pDC;

where pDC is passed as argument into OnEraseBkgnd. All this seems to work fine.

When I extract the portion of image and display it, I get a back rectangle of the correct size. I assume that the pDC I saved is incorrect. I would appreciate if someone could confirm if I have used the pDC correctly.

Many thanks

Developer technologies C++
0 comments No comments
{count} votes

Accepted answer
  1. SM 416 Reputation points
    2021-06-16T18:56:58.713+00:00
    CView::OnEraseBkgnd() is called from  below function.  
      
    BOOL CWnd::OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult)  
    {  
    [...]  
        	case AfxSig_b_D_v:  
        		lResult = (this->*mmf.pfn_b_D)(CDC::FromHandle(reinterpret_cast<HDC>(wParam)));  
        		break;  
    [...]  
    }  
    

    https://learn.microsoft.com/en-us/cpp/mfc/reference/cdc-class?view=msvc-160#fromhandle clearly mentions, "The pointer may be temporary and should not be stored beyond immediate use."

    -SM

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.