OnPaint in a secondary class?

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
forrestcupp
Earned some good credits
Earned some good credits
Posts: 102
Joined: Thu Dec 28, 2006 5:12 pm
Location: Indiana, US

OnPaint in a secondary class?

Post by forrestcupp » Wed Oct 12, 2011 8:51 pm

I have my main wxFrame class that has its own OnPaint function that works properly. What I'm trying to do is create another class that will draw a wxBitmap on the screen. The parameters of the constructor for this class take a wxWindow, which is the panel that I draw to in the main wxFrame, and a wxImage that gets converted to a wxBitmap.

So I created a separate OnPaint function in the secondary class, and I attempted to connect the wxWindow that got passed from the main class to this class's OnPaint function. Then I tried to create a wxPaintDC using that wxWindow so I could draw my wxBitmap to the panel that I passed from the main class.

I'm sure this sounds very confusing, but needless to say, it didn't work. So can anyone give me any tips on how I can create a separate class that continually draws to my main panel in my main class without me having to manually calling a function to do it?

catalin
Moderator
Moderator
Posts: 1589
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: OnPaint in a secondary class?

Post by catalin » Wed Oct 12, 2011 10:36 pm

That way is wrong. You can only create one wxPaintDC instance for the same wxWindow when receiving a paint event.

The solution is to have only MyFrame::OnPaint(), give up on TheSecondClass::OnPaint(), and create TheSecondClass::RenderBitmap( wxDC* frameDc ). Call RenderBitmap() from MyFrame::OnPaint() and pass it the paint dc created in MyFrame::OnPaint().

*names above are only an attempt to make the example more explicit :)

forrestcupp
Earned some good credits
Earned some good credits
Posts: 102
Joined: Thu Dec 28, 2006 5:12 pm
Location: Indiana, US

Re: OnPaint in a secondary class?

Post by forrestcupp » Thu Oct 13, 2011 1:56 am

Thanks for your response. I found out through experimentation that if I call one of TheSecondClass's functions from MyFrame::OnPaint, I can have another wxPaintDC in the function in TheSecondClass, and it works because it is being called from OnPaint. My problem is that I don't want to have to manually call a function every time I want it to draw something; I want it to be automated. I'm trying to make a Png animation class that is customized for my needs, and I want to be able to call a Play function, and it will just automatically cycle through the pngs to animate them.

So I tried creating a wxTimer in my animation class. Theoretically, I should be able to use a wxClientDC from within the timer handler function because the wxClientDC is meant for drawing outside the paint event handler. But that didn't work, either. It doesn't draw the bitmap. I guess if I can't figure it out, I'll just have to manually call the Play() function from MyFrame::OnPaint, but I'd much rather have it automated.

I think what I'm trying to draw in the animation class with the wxClientDC just gets immediately drawn over by my paint event handler in MyFrame. From everything I'm seeing, it looks like there is absolutely no way to do what I'm trying to do.

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Re: OnPaint in a secondary class?

Post by Auria » Thu Oct 13, 2011 6:32 pm

Warning, using a client DC does not save you from having to handle paint events, because your window manager may at any time throw away whatever you've drawn and ask you to draw it again with a paint event.

What you can just do is "frame++; Refresh()" in the timer, and let the paint event handler read variable "frame".
"Keyboard not detected. Press F1 to continue"
-- Windows

Post Reply