About drawing something on a Control.

  • Thread starter Thread starter linuxfedora
  • Start date Start date
L

linuxfedora

i have added a Panel with size = (100, 100). Then i would like to draw
a Line on that Panel. So i write:

I write some codes on the OnPaint event of the Panel:
private void panel_Paint(...)
{
Graphics g = e.Graphics;
g.DrawLine(new Pen(Colors.Red, 6), new Point(0, 0), new Point(100,
100));
}


Then it can draw a line on the Panel, but when part of the Panel is
covered by other windows, the hope Panel will call OnPaint, and the
whole line will be redraw, is there any way to prevent it??Thanks
 
i have added a Panel with size = (100, 100). Then i would like to draw
a Line on that Panel. So i write:

I write some codes on the OnPaint event of the Panel:
private void panel_Paint(...)
{
Graphics g = e.Graphics;
g.DrawLine(new Pen(Colors.Red, 6), new Point(0, 0), new Point(100,
100));
}


Then it can draw a line on the Panel, but when part of the Panel is
covered by other windows, the hope Panel will call OnPaint, and the
whole line will be redraw, is there any way to prevent it??Thanks

The Graphics object that you get in the event arguments is cropped to
the area that needs to be redrawn. If you are using that, you can't draw
anywhere that you aren't supposed to.
 
if i want to draw something on the control, then how can i do it?
For example, when i call function named WriteSomeThingOnPanel(),

Then there will be a line drawn on Panel?

I have tried to get the Graphics of the Panel by
Graphic.FromHwnd(panel.Handle);
then draw the line, but nothing can be drawn?Thanks
 
This is source code of my testing program:
http://www.teleeye.com/marketing/Software/PlaybackLogCtrl_sample.zip
(Run in Visual Studio 2005)
In the source, i used a tableLayout Named logCamTableLayout and put
many logCamPanel on them, and then draw a Line on each of the
logCamPanel, and when these logCamPanel is redraw, it will call
OnPaint, and i will write a line that, but i find the screen will be
flicking when it needs to redraw. Does anyone know how to solve it?
Thanks
 
if i want to draw something on the control, then how can i do it?
For example, when i call function named WriteSomeThingOnPanel(),

Then there will be a line drawn on Panel?

I have tried to get the Graphics of the Panel by
Graphic.FromHwnd(panel.Handle);
then draw the line, but nothing can be drawn?Thanks

You use the Paint event, just as in the code that you posted. The event
arguments contains a reference to the graphics object that should be
used for drawing, just as in the code that you posted.

To trigger the Paint event, you call the Invalidate method on the control.
 

Please don't post links to your code. Post an actual code sample with
your post.

And please don't just copy and paste whatever code you have. Post a
concise-but-complete sample of code. "Concise" means you have removed
_everything_ from the code that is not absolutely required in order to
reproduce the problem. "Complete" means there is _nothing_ left out of
the code that is needed in order to compile and run it.

That said, you may not need to fix that mistake. Specifically...
In the source, i used a tableLayout Named logCamTableLayout and put
many logCamPanel on them, and then draw a Line on each of the
logCamPanel, and when these logCamPanel is redraw, it will call
OnPaint, and i will write a line that, but i find the screen will be
flicking when it needs to redraw. Does anyone know how to solve it?

It sounds to me as though the problem you're concerned about is simply
the visual effect of the control erasing its background and then you
drawing the line again. This makes the line seem to flicker.

You can usually solve this easily simply by setting the DoubleBuffered
property of the control to "true". Note that to do this, you have to
have your own class derived (at some point) from Control. IMHO, if you
want to do custom drawing, this is a better way anyway. So make a new
class, derived from Panel, and in the constructor set the DoubleBuffered
property to "true. Then instead of subscribing to the Paint event, just
override the OnPaint() method in the class.

Pete
 
Sorry, i have removed most of the code from the original source before
posting.

Okey, thank, then i should search for document on doublebuffering and
try to use a custom panel to draw the line. THanks
 
Back
Top