Jump to content
  • Advertisement
Sign in to follow this  
Samurai Jack

Painting in MFC

This topic is 4771 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!