Visual C++ PaintEventArgs
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Visual C++ PaintEventArgs

  1. #1
    Join Date
    Aug 2011
    Posts
    19

    Visual C++ PaintEventArgs

    Hi everyone,
    I followed the example on MSDN, which was very helpful (http://msdn.microsoft.com/en-us/libr...eventargs.aspx).

    However, the problem that I have is that I don't know how to reuse PaintEventArgs over again!

    Once I assigned the "Form" Appearance in its property to have Paint Appearance, then I got the following appeared in my code
    private: void Form1_Paint(System::Object ^ sender, System::Windows::Forms::PaintEventArgs ^ pe)

    And when I followed the example listed on the above link I got it to work.
    But now, I want to define an internal function that keeps doing more stuff, and I did something like this:
    I defined this at a global level:
    Private: System::Windows::Forms::PaintEventArgs my_pe

    And inside the "Form1_paint" function, I added the following line: my_pe = pe

    And then I defined a separate function

    private: void draw_line(double long x, double long y, System::Object ^ sender, System::Windows::Forms::PaintEventArgs ^ pe){
    Pen^ greenPen = gcnew Pen(Color::Green);
    Point startPoint;
    Point endPoint;

    startPoint= Point(20+x, 50+x);
    endPoint = Point(200+y, 300+y);
    pe->Graphics->DrawLine( greenPen, startPoint, endPoint);
    }

    And somewhere else in the code, I am calling the following:

    draw_line(100, 100, sender, my_pe);

    But I get a run time error and doesn't understand (my_pe->Graphics)???


    Can someone please help and tell me how can I actually reuse the PaintEventArgs, such that I can keep calling different function that do different drawings?

    I really appreciate,

    --Rudy
    Last edited by rudy01; January 23rd, 2013 at 10:01 AM.

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,583

    Re: Visual C++ PaintEventArgs

    Tunneling the Graphics object you got from the event args parameter out of the event handler is useless: The object becomes invalid as soon as the event handler returns.

    According to the Windows philosophy, the only actual painting that's going on happens inside the paint event handler and is usually based on parameters determined by the outside world.

    For demonstration, I have modified the MSDN sample you're refererring to a bit, so it draws a line rotating at the pace of a clock's seconds hand:

    Code:
    // Form1.cpp
    
    #include "stdafx.h"
    
    #include "Form1.h"
    
    using namespace PaintEventDemo;
    
    /*
    Event handler function taken from the sample at
    http://msdn.microsoft.com/en-us/libr...v=vs.100).aspx
    and modified:
    */
    
    System::Void Form1::pictureBox1_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e)
    {
      // Create a local version of the graphics object for the PictureBox:
    
      Graphics^ g = e->Graphics;
    
      // Draw a string on the PictureBox:
    
      g->DrawString( "This is a rotating line drawn on the control",
        gcnew Drawing::Font("Arial", 10), Brushes::Blue, Point(30, 30));
    
      // Rotate the Graphics' coordinate system around its own center to make the line drawn with
      // numerically fixed endpoints effectively rotate:
    
      g->TranslateTransform(pictureBox1->ClientSize.Width / 2, pictureBox1->ClientSize.Height / 2);
      g->RotateTransform(DateTime::Now.Second * 6);
    
      // Draw the line:
    
      g->DrawLine(Pens::Red, 0, -pictureBox1->ClientSize.Height / 2, 0, pictureBox1->ClientSize.Height / 2);
    }
    
    System::Void Form1::timer1_Tick(System::Object^  sender, System::EventArgs^  e)
    {
      pictureBox1->Invalidate();
    }
    The job of the timer tick handler is to periodically invalidate the picture box to cause it to repaint. That's how any Windows GUI program would do it: Update the "outside world" parameters (in case of this demo the system clock's seconds value which is updated automatically) and then invalidate the control to cause it to redraw itself, which then is done in the paint event handler.

    In this demo I also have separated the event handler implementations into a separate implementation file (Form1.cpp) as has been discussed in several threads around here. The demo project is attached to this post.
    Attached Files Attached Files
    Last edited by Eri523; January 24th, 2013 at 12:29 PM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Aug 2011
    Posts
    19

    Re: Visual C++ PaintEventArgs

    Perfect,
    Thanks a lot for taking the time and replying. It is making sense, and thanks for attaching the demo paint code.

    Thanks again,
    --Rudy

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center