How to draw into memDC, then show it

I would like to:
1. memDC.LineTo(60,60)
2. Determine the extension of memDC
3. draw memDC to my window screen

Is this possible without using bitmap?

If bitmap is needed to use, now I can
handle it but must first determine the
extension of bitmap first, then draw on
memDC. But I want to first draw on
memDC, then create a bitmap with the
correct needed extension, then draw
bitmap on window.

Please write sample code.
I use VisualStudio 6, MFC, c++

Thanks, Tomas
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tko092397Author Commented:
Edited text of question.
You can not transwer data from one DC to another without bitmaps.

When you use memoryDC
You must before create bitmap with required dims. select it into thebmemory context? draw into the context, BitBlt from memory context to the destination context and then select memory context old bitmap back.
for example:
CDC dcDisplay;
dcDisplay.CreateIC("DISPLAY", "", "", NULL)
CDC dcMem;
CBitmap bm;
bm.CreateCompatibleBitmap(&dcDisplay,100,100 );
CBitmap* pbmOld = dcMem.SelectObject(&bm);
/* drawing routines here width using dcMem */
dcDest.BitBlt(...); //dcDest is the DC where to your image will be transfered

Hi tko,

perhaps you'd prefer drawing to a metafile in memory and than draw this metafile to your dc. For this see help for CreateMetaFile, CloseMetaFile and CDC::PlayMetaFile()

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

A memory DC without a bitmap selected into it can't be used for drawing.
I give you my code in CMyView::OnDraw() to avoid flicking. You can use it to draw all you need in a bitmap and copy this bitmap to the screen.

void CMyView::OnDraw(CDC* pDC)
CMyDoc* pDoc = GetDocument();

// traw to memory to avoid flicking
CRect rect; GetClientRect(&rect);
CDC dcMem; dcMem.CreateCompatibleDC(pDC);
CBitmap bitmap; bitmap.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
CBitmap *pOldBitmap = dcMem.SelectObject(&bitmap);

// draw backgroung, but implement OnEraseBkgnd() to do nothing
CBrush brush(RGB(255,255,255));
dcMem.FillRect(&rect, &brush);

// TODO: Your drawing using dcMem

// copy bitmap to screen
pDC->BitBlt(rect.left,, rect.Width(), rect.Height(), &dcMem, 0, 0, SRCCOPY);


BOOL CMyView::OnEraseBkgnd(CDC* pDC)
return TRUE;

regards, jean-claude.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tko092397Author Commented:

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.