MFC graphics with CDC, CBitmap, OnPaint

Hello,

I am using MFC and have a class MainPanel :public CFrameWnd.

What I am trying to do is, draw a rectangle, save the CDC into a bitmap,
then on the OnPaint() use that bitmap so that when the user e.g. minimizes the window I don't lose any changes he has made.

I am getting a big black rectangle right now. I am a beginner so if you see something wrong in the logic to "persist" such changes when the onpaint() is called, please advise me.

Your help is mostly appreciated.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
void MainPanel::OnPaint() 
{
	//CPaintDC dc(this);
	//dc.TextOut(20,20,L"Welcome",7);
	//MessageBeep(MB_ICONERROR);

	CPaintDC dc(this);
	CDC MemDC;
	
	MemDC.CreateCompatibleDC(&dc);
	CBitmap *myBmp = MemDC.SelectObject(&m_Bitmap);
	
	dc.BitBlt(0, 0, 1024, 768, &MemDC, 0, 0, SRCCOPY);
	dc.SelectObject(myBmp);

}


void MainPanel::OnLButtonDown(UINT nFlags, CPoint point) 
{

     CFrameWnd::OnLButtonDown(nFlags, point);
     m_StartPoint = point;
}

void MainPanel::OnLButtonUp(UINT nFlags, CPoint point) 
{
	
     CFrameWnd::OnLButtonDown(nFlags, point);
     m_EndPoint = point;

	 CClientDC dc(this);
	 
     dc.MoveTo(m_StartPoint);
     dc.LineTo(m_EndPoint);
	 storeDCinBMP(&dc);

}

void MainPanel::storeDCinBMP(CClientDC* myCDC)
{
	m_Bitmap.DeleteObject();
	m_Bitmap.CreateCompatibleBitmap(myCDC, 1024, 768);
	myCDC->SelectObject(m_Bitmap);
}
Last edited on
To be honest,
If I was doing this, I would have an m_DC member. Which I would setup with the during the constructor of the MainPanel with a compatible bit map.
That would make life much easier I think.

But doing it your way - does this code work for you??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void MainPanel::storeDCinBMP(CClientDC* myCDC)
{
	m_Bitmap.DeleteObject();
   	
    CDC MemDC;	
	MemDC.CreateCompatibleDC(myDC);
    
    m_Bitmap.CreateCompatibleBitmap(myCDC, 1024, 768);
    CBitmap *myBmp = MemDC.SelectObject(&m_Bitmap);
    
   	MemDC.BitBlt(0, 0, 1024, 768, myDC, 0, 0, SRCCOPY); //copy from screen to memdc.
    //The next couple of lines I'm not sure about
    MemDC.SelectObject(myBmp).
    
}



=================================================
On another subject

1
2
3
4
5
6
7
8
9
10
11
12
13
void MainPanel::OnLButtonUp(UINT nFlags, CPoint point) 
{
	
     CFrameWnd::OnLButtonDown(nFlags, point); //?????
     m_EndPoint = point;

	 CClientDC dc(this);
	 
     dc.MoveTo(m_StartPoint);
     dc.LineTo(m_EndPoint);
	 storeDCinBMP(&dc);

}
Thanks for the reply!

I got it working don't remember exactly how. I think I was okish, just forgot to fill white pixels...and thought something was wrong.

1
2
3
4
5
6
m_Bitmap.CreateCompatibleBitmap(this->GetDC(), m_width, m_height);
	m_MemDC.CreateCompatibleDC(this->GetDC());
	int saveState = m_MemDC.SaveDC();
	m_MemDC.SelectObject(m_Bitmap);
	m_MemDC.FloodFill(0,0,RGB(255,255,255));
	m_MemDC.RestoreDC(saveState);


=================================================
On another subject

Now that you mentioned it, I don't see a reason for passing the event down the base class,
probably something I left from an MFC tutorial I was reading.

Thank you for the help, I'm off to ask something basic in the beginner section hehe =)
Topic archived. No new replies allowed.