Painting in MFC

Started by
3 comments, last by Endurion 18 years, 9 months ago
Greetings! I was wondering, what is the best way to paint in MFC? My idea is to create something like a Bitmap (Rectangular area) where I could fill pixels in it. How to manage that in MFC? What control is most suitable for souch kind of operation? I was looking at the OnPaint strcture (Visutal C 7.1): ---Paint method---- CPaintDC dc(this); dc.MoveTo(10,10); dc.LineTo(20,20); this works only in paint method. What if I had a Button associated with m_Button variable: --Case A:false, don't know why?-- CPaintDC dc(&m_Button); dc.MoveTo(10,10); dc.LineTo(20,20); //This does not work?! but this works: CDC* pDC = m_Button.GetDC(); pDC->MoveTo(10,10); dc.LineTo(20,20); //pDC has no release?! Is it ok to just leave it?!! - Memory leaks?! And what happens if I resize/repaint the window, I have to draw it all from the beginning?! Is it possible to draw into a control that holds the bitmap for me? Thank you in advance!
Advertisement
There are some different options available:

If you don't want to user to press the bitmap, you can use the same methods on the CStatic control instead.

The DC retrieved by GetDC should be released by a call to CWnd::ReleaseDC.
It's not the point that i would like a button with a bitmap, I would like a control, where i could plot pixels in it. Like Picture control in Visual Basic 6 or whatever.

CDC does not provide RelaseDC only GetDC(). What to do?!
CPaintDC(*) does not work for some reason, only for this.
All windows receive a WM_PAINT message when they need to be repainted (with MFC this results in OnPaint being called). You use the DC (device context) to perform various drawing operations like you have outlined, drawing lines, circles, bitmaps etc.

You could draw everything each time the message is received, or you can do things a bit nicer. Create a bitmap (using CreateCompatibleBitmap) and store it in your class. Then when you come to paint you can do a simple check to see if the bitmap needs to be re-created (based on whether things have moved or changed or whatever). If nothing needs to be redrawn you simply BitBlt the bitmap to the screen - otherwise you redraw the bitmap then BitBlt it.
"Absorb what is useful, reject what is useless, and add what is specifically your own." - Lee Jun Fan
Quote:Original post by Samurai Jack
-snip-
CDC does not provide RelaseDC only GetDC(). What to do?!
CPaintDC(*) does not work for some reason, only for this.


You call m_Button.GetDC(). For CDC you get like this you do m_Button.ReleaseDC( pDC ).

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

This topic is closed to new replies.

Advertisement