Drawing to custom control surface help please
-
Hi anyone! I am trying to draw to my custom control with VS2005 with compact framework 2.0. My control and a second class which references its Graphics object are seperate but in the same file... i create my second class and attempt to draw small images on the control using its graphics object reference at later stages of the app running. I get an ObjectDisposedException, but I understand what this means, but is there anywway I can reference the controls drawing object just before i process the drawing code? or any other work around for that matter! thankyou in advance for this!
-
Hi anyone! I am trying to draw to my custom control with VS2005 with compact framework 2.0. My control and a second class which references its Graphics object are seperate but in the same file... i create my second class and attempt to draw small images on the control using its graphics object reference at later stages of the app running. I get an ObjectDisposedException, but I understand what this means, but is there anywway I can reference the controls drawing object just before i process the drawing code? or any other work around for that matter! thankyou in advance for this!
Best practice is to do all your drawing in the
OnPaint
method or in a handler for thePaint
event. You can cause a callback intoOnPaint
by calling theInvalidate
method, to tell Windows what part of the control needs to be repainted. If you want to perform some immediate painting, rather than waiting for the Paint event to fire (which is deferred, in the Windows messaging model, to allow input to be processed at a higher priority than painting operations) follow this pattern:using ( Graphics g = ctl.CreateGraphics() )
{
// do drawing here
}The documentation for
CreateGraphics
specifically says, "The Graphics is only valid for the duration of the current window's message." You are not allowed to save it and reuse it. The technical reason behind this is that Device Contexts - the Windows data structure behind the Graphics object - are expensive, and the system only keeps a small pool of them available. If you were to retain them, the pool would quickly run out and no-one would be able to draw anything. So the Framework automatically gives it back at the end of processing every window message. You still need to remember everything that you've drawn anyway and be prepared to redraw it inOnPaint
. Windows does not save your screen layout when something is drawn on top of it (the start menu, a notification bubble, another application), you have to repaint it when the obscuring object is removed. The best resource for understanding Windows' drawing and events model is Charles Petzold's book "Programming Windows, Fifth Edition". Yes, it hasn't been updated in 10 years. It targets desktop Windows. It uses the C programming language. The fundamentals are the same between the desktop and CE, and they haven't changed since Win32 was introduced with NT 3.1 in 1993. If you do turn your hand to the desktop, note that Windows Vista with Aero now does save your drawing because you're not actually drawing to the screen, you're drawing to an off-screen bitmap which the graphics card combines with all the other windows' bitmaps to produce the screen output. You should still do all your drawing in OnPaint so that the drawing can be synchronized properly.DoEvents: Generating unexpected recursion since 1991