Hunter Peress wrote:
THe link works now, sorry.
OK, I got it. I lost your original message, and I recall that I really
didn't understand what it is you were trying to do when I read it the
first time. However, I looked at your link, and found why the code there
didn't work:
(side note: please post a link to a code file in the future, tather than
html. I had to spend a little time fixing the code I cut & pasted form
my browser)
It looks like you were trying to double buffer the Graph Window. The
reason it didn't work was that you had the size wrong. You had set the
size in the GraphWindow __init__ function, but the window had not yet
been sized to fit the surrounding frame, so you were creating an off
screen buffer that was only 20X20, so of course it didn't draw right.
You were also re-setting the MemDC to a Null bitmap on every OnPaint,
which would kill it.
Fixing these two errors got it working, but I changed a couple of other
things as well. A MamDC is apparently a "scarce resource" on Windows, so
it's not a good idea to keep it around. Instead, you can keep the bitmap
around and re-selct it into a new MemDC when you want to draw to it. I
re-factored your code to do that. Also, you can use DC.DrawBitmap to
blit the offscreen bitmap to the PaintDC. (that there in 2.3.2 anyway,
iot may not be there in older versions, check out DrawIcon if not, or do
what you did with creating a MamDC, selecting the bitmap into it, and
then blitting to the PaintDC. If you do that, you probably want to pass
the MemDC into your draw function, so you can then use it to blit.
NOTES:
I hard-coded the size of the window and off screen bitmap, so if the
window is re-sized, the graphs themselves won't change. You may or may
not want that. If you want the graph to re-size, you need to capture the
OnSize events, and re-size your buffer then.
Why are you doing this anyway? Usually one double buffers so that a
drawing that takes a while to create can be stored, so that the window
will update itself very fast when it is uncovered, or whatever. In this
case, you are calling the Draw function if and only if an Paint event
has happened, so I don't see any apparent advantage.
The way double buffering is usually set up, is that there is an Update
function that updates the buffer, and then updates the screen with a
clientDC. In the OnPaint handler, all you do is Draw the buffer to the
screen, so it doesn't get re-drawn unless it needs to be. In this
example, the figure is getting constantly re-drawn, so there isn't much
point, and the way the demo is originaly written does a fine job. The
way I now have it working might be a slight improvement, because the
bitmap doesn't get re-created each time, but I'm not sure how
significant that is.
So, I got your code working, but either you are doing something
unneccessary, or I didn't get it doing what you want. How do you want it
to function differently than the original demo anyway?
By the way, what was wrong with your code wasn't really a problem of
understanding DCs and all that. If anything, it was a proble with
understanding when windows got sized, but the debugging required wasn
not the least wxWindows specific. Here's how I found it:
I noticed that something was getting drawn, but not the right thing (I
got a little blue splotch)
I put some print statements int he Draw() method, and noticed that only
the first bar was getting drawn.
I then figured out that that the only way out of the bar drawing lop was
the break ionside the checking for ypos being too big, so I put in a
print staement and discoved that self.size was way to small.
Also, the way I've factored the size code is not very clean, I would
redo that. Having a self.size member is a good idea, just make sure it
gets set to the right size! (maybe set it in a OnSize handler or
something)
-Chris
···
--
Christopher Barker, Ph.D.
Oceanographer
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker@noaa.gov