Visual c++ paint event. My paint.h into form.h

Hello.

I am trying to paint anything in Microsoft Visual c++ 2010 with paint event in windows form application.

Here is the code:

private: System::Void Form1_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  pe)
			 {
				 Graphics ^ g = pe->Graphics;

				 System::Drawing::Pen^ myPen =
				     gcnew System::Drawing::Pen(System::Drawing::Color::Red);
				 System::Drawing::Graphics^ formGraphics;
				 formGraphics = this->CreateGraphics();
				 formGraphics->DrawRectangle(myPen, Rectangle(0, 0, 200, 200));
				 delete myPen;
				 delete formGraphics;
			 }


It's working :D But is it possible to link it somehow like this?

private: System::Void Form1_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  pe)
			 {
				 Graphics ^ g = pe->Graphics;

				 Tank;
			 }


and it will read the code from tank.h and add it on position where is word Tank.

tank.h

#ifndef TANK_H
#define TANK_H

class Tank
{
private:
    void DrawRectangle()
    {
        System::Drawing::Pen^ myPen =
            gcnew System::Drawing::Pen(System::Drawing::Color::Red);
        System::Drawing::Graphics^ formGraphics;
        formGraphics = this->CreateGraphics();
        formGraphics->DrawRectangle(myPen, Rectangle(0, 0, 200, 200));
        delete myPen;
        delete formGraphics;
    }
};

#endif


Thank you.
The tags you're using for your code made it pretty unreadable to me!

Andy
That doesn't help me see your code!

From what I can see through the murk, I am wondering why you are creating a new Graphics instance rather than using the one passed with the event. Is there a reason?

It would probably be better to use a function here, rather than a class.

Andy

P.S. If you need to use a class here:
1. make the method DrawRectangle() static and call via ::
2. create an instance and call DrawRectangle() via .
Last edited on
Thanks for reply. :)
Topic archived. No new replies allowed.