#pragma once /////////////////////////////////////////////////////////////////////////////// // Form1.h - Demonstrate Drawing on a C++/CLI form // // // // Jim Fawcett, CSE681 - Software Modeling and Analysis, Fall 2008 // /////////////////////////////////////////////////////////////////////////////// #include "../DrawLib/DrawLib.h" namespace Drawer { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; using namespace System::Drawing::Drawing2D; /// /// Summary for Form1 /// /// WARNING: If you change the name of this class, you will need to change the /// 'Resource File Name' property for the managed resource compiler tool /// associated with all .resx files this class depends on. Otherwise, /// the designers will not be able to interact properly with localized /// resources associated with this form. /// public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); } protected: ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::Button^ button1; System::Windows::Forms::Button^ button2; System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// void InitializeComponent(void) { this->button1 = (gcnew System::Windows::Forms::Button()); this->button2 = (gcnew System::Windows::Forms::Button()); this->SuspendLayout(); // // button1 // this->button1->Anchor = static_cast((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left)); this->button1->Location = System::Drawing::Point(56, 223); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(75, 23); this->button1->TabIndex = 0; this->button1->Text = L"draw"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click); // // button2 // this->button2->Anchor = static_cast((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left)); this->button2->Location = System::Drawing::Point(161, 223); this->button2->Name = L"button2"; this->button2->Size = System::Drawing::Size(75, 23); this->button2->TabIndex = 1; this->button2->Text = L"save"; this->button2->UseVisualStyleBackColor = true; this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click); // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(284, 264); this->Controls->Add(this->button2); this->Controls->Add(this->button1); this->Name = L"Form1"; this->Text = L"Form1"; this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load); this->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::Form1_Paint); this->SizeChanged += gcnew System::EventHandler(this, &Form1::Form1_SizeChanged); this->ResumeLayout(false); } #pragma endregion private: Image^ image; Rectangle rect; System::Drawing::Region^ region; bool redraw; static bool first = true; int left, top, width, height; //----< initialize form >-------------------------------------------------- System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { redraw = false; rect = this->ClientRectangle; rect.Width -= 20; rect.Height -= 65; rect.X = 10; rect.Y = 10; region = gcnew System::Drawing::Region(rect); } //----< generate drawing - just a random rectangle here >------------------ void prepareDrawing(Graphics^% g) { // access to native drawing code Drawer::DrawingInfo^ di = gcnew Drawer::DrawingInfo(); if(redraw) { int* points = di->getPoints(); left = points[0]; top = points[1]; width = points[2]; height = points[3]; } g->FillRectangle(Brushes::BlanchedAlmond,left,top,width,height); g->DrawRectangle(Pens::Black,left,top,width,height); g->DrawString( redraw.ToString(), this->Font, Brushes::Black, float(left+10),float(top+10) ); first = false; } //----< paint handler >---------------------------------------------------- System::Void Form1_Paint( System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e ) { Graphics^ g = e->Graphics; g->Clip = region; g->FillRectangle(Brushes::White,10,10,rect.Width,rect.Height); g->DrawRectangle(Pens::Black,10,10,rect.Width,rect.Height); prepareDrawing(g); redraw = false; } //----< save button handler >---------------------------------------------- System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { Graphics^ saveGraphics = this->CreateGraphics(); image = gcnew Bitmap( this->ClientRectangle.Width, this->ClientRectangle.Height, saveGraphics ); Graphics^ imageGraphics = Graphics::FromImage(image); saveGraphics->Clip = region; saveGraphics->FillRectangle(Brushes::White,10,10,rect.Width,rect.Height); saveGraphics->DrawRectangle(Pens::Black,10,10,rect.Width,rect.Height); prepareDrawing(imageGraphics); image->Save("SavedDrawing.png"); this->Invalidate(); } //----< resize handler >--------------------------------------------------- System::Void Form1_SizeChanged(System::Object^ sender, System::EventArgs^ e) { rect = this->ClientRectangle; rect.Width -= 20; rect.Height -= 65; rect.X = 10; rect.Y = 10; region = gcnew System::Drawing::Region(rect); redraw = false; this->Invalidate(); } //----< draw button handler >---------------------------------------------- System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { redraw = true; this->Invalidate(); } }; }