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;
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 11:01 AM.
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.
Last edited by Eri523; January 24th, 2013 at 01: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.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.