About drawing something on a Control. - DOTNET

This is a discussion on About drawing something on a Control. - DOTNET ; 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 ...

+ Reply to Thread
Results 1 to 7 of 7

About drawing something on a Control.

  1. Default About drawing something on a Control.

    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


  2. Default Re: About drawing something on a Control.

    linuxfedora@yahoo.com.hk wrote:
    > 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.

    --
    Göran Andersson
    _____
    http://www.guffa.com

  3. Default Re: About drawing something on a Control.

    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


  4. Default Re: About drawing something on a Control.

    This is source code of my testing program:
    http://www.teleeye.com/marketing/Sof...trl_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


  5. Default Re: About drawing something on a Control.

    linuxfedora@yahoo.com.hk wrote:
    > 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.

    --
    Göran Andersson
    _____
    http://www.guffa.com

  6. Default Re: About drawing something on a Control.

    linuxfedora@yahoo.com.hk wrote:
    > This is source code of my testing program:
    > http://www.teleeye.com/marketing/Sof...trl_sample.zip


    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

  7. Default Re: About drawing something on a Control.

    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


+ Reply to Thread

Similar Threads

  1. Drawing a .net control to printer using inches
    By Application Development in forum DOTNET
    Replies: 7
    Last Post: 11-13-2006, 08:47 AM
  2. Drawing in a user control
    By Application Development in forum DOTNET
    Replies: 7
    Last Post: 05-30-2006, 11:42 PM
  3. Drawing on the WebBrowser control
    By Application Development in forum DOTNET
    Replies: 0
    Last Post: 05-16-2006, 09:55 PM
  4. Control for drawing
    By Application Development in forum DOTNET
    Replies: 1
    Last Post: 05-10-2006, 11:27 PM
  5. Drawing on a MonthCalendar control
    By Application Development in forum DOTNET
    Replies: 1
    Last Post: 01-18-2006, 08:09 AM