CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2009
    Posts
    15

    Very basic question - Paint event

    Hello,

    I'm new in GDI+ and C# programming, though I have an experience in other programming languages.

    Here is my question:
    I started a new Windows Form Application project in Visual Studio 2008.
    Then I double-clicked the Paint event of the form and added the following line to the Form1_Paint(..) method:

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
    e.Graphics.DrawEllipse(Pens.Red, 0, 0, 50, 50);
    }

    When I run the application I do see the circle, but I don't really understand what happened.
    More specifically:
    1) When does Form1_Paint(..) called ?
    2) When does the Paint event of the form is raised ?
    3) Why the Paint event is raised when I run the application ?
    4) Why the circle is actually drawn on the form ? i.e. what is the relationship between the form class, the "PaintEventArgs e" argument, and the "e.Graphics" object ?

    I will appreciate any help !
    Thanks in advance !

  2. #2
    Join Date
    Nov 2009
    Location
    .net 3.5 csharp 2008 developer
    Posts
    36

    Re: Very basic question - Paint event

    Without going way too deep in how controls are repainted in Windows OS I might be able to explain somewhat simplified

    All controls/windows are only repainted when requested, by either you (the coder) or the OS itself.
    The Windows OS sends commands to the programs controls to repaint themselves whenever it is required, say, when you resize a window, or when you move another window that is overlapping your program. There are more scenarios when this happens though, but I won't list them all here

    When you execute your program for the first time, nothing is actually shown until Windows OS sends it a command to paint itself. These commands are normally nothing to be concerned with, unless you need to do some really tricky/advanced stuff.

    You can always force a control/window to paint itself in a few ways, most simple would be calling the Refresh() method.
    Code:
    // Force Windows OS to send a few commands to form1, one of them being WM_PAINT, which in turn triggers Form1_Paint()
    form1.Refresh(); 
    
    
    // Exactly the same as above, but the commands are sent to myPictureBox :)
    myPictureBox.Refresh();

  3. #3
    Join Date
    Dec 2009
    Posts
    15

    Re: Very basic question - Paint event

    Thank you !
    Now I understand this better.
    Now I have several more questions

    1) Is that true that the default implementation of what happened when Paint event is fired is to call the OnPaint(..) method ?

    2) What is the default implementation of OnPaint(..) ?

    3) What may happen if I override the OnPaint(..) method but will not call base.OnPaint(..) ?

    4) Who and when calls OnPaintBackground(..) ?

    Thank you very much !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured