Vamos à versão no C++ do MS VS2005, começando com a interface gráfica e o destaque da referência ao evento "button1_Click":
Aqui é destacada a referência ao evento "pictureBox1_Paint":
Começando pelo código da classe Quadrado através do arquivo "Quadrado.h":
ref class Quadrado
{
private:
int x;
int y;
int largura;
public:
Quadrado(); // construtor default
Quadrado( int, int ); // construtor
Quadrado( int, int, int ); // construtor
void Plotar( System::Drawing::Graphics^ );
~Quadrado(); // destrutor
};
O código para o arquivo "Quadrado.cpp":
#include "stdafx.h"
#include "Quadrado.h"
Quadrado :: Quadrado( ) // construtor default
{
this->x = 100;
this->y = 200;
this->largura = 90;
}
Quadrado :: Quadrado( int a, int b ) // construtor
{
this->x = a;
this->y = b;
this->largura = 50;
}
Quadrado :: Quadrado( int a, int b, int c) // construtor
{
this->x = a;
this->y = b;
this->largura = c;
}
void Quadrado :: Plotar( System::Drawing::Graphics^ tmp )
{
// cor e espessura para a caneta
System::Drawing::Pen ^caneta = gcnew System::Drawing::Pen
( System::Drawing::Color::Red, 1 );
tmp->DrawLine( caneta,
this->x, this->y, // ponto (x1, y1)
this->x + this->largura, this->y ); // ponto (x2, y2)
tmp->DrawLine( caneta,
this->x + this->largura, this->y, // ponto (x2, y2)
this->x + this->largura, this->y + this->largura ); // ponto (x3, y3)
tmp->DrawLine( caneta,
this->x + this->largura, this->y + this->largura, // ponto (x3, y3)
this->x, this->y + this->largura ); // ponto (x4, y4)
tmp->DrawLine( caneta,
this->x, this->y + this->largura, // ponto (x4, y4)
this->x, this->y ); // ponto (x1, y1)
delete caneta;
}
Quadrado :: ~Quadrado()
{
}
E o principal, o código da classe do formulário (renomeado para "Plotador.h"):
#pragma once
#include "Quadrado.h"
namespace ClasseQuadrado {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Plotador
///
/// 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.
/// </summary>
public ref class Plotador : public System::Windows::Forms::Form
{
private:
System::Drawing::Bitmap^ figura;
System::Drawing::Graphics^ quadro;
public:
Plotador(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
this->figura = gcnew System::Drawing::Bitmap( this->pictureBox1->Width,
this->pictureBox1->Height );
this->quadro = System::Drawing::Graphics::FromImage( this->figura );
this->quadro->Clear( Color::White );
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Plotador()
{
if (components)
{
delete components;
}
delete this->figura;
}
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Label^ label3;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::TextBox^ textBox2;
private: System::Windows::Forms::TextBox^ textBox3;
private: System::Windows::Forms::PictureBox^ pictureBox1;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->label1 = (gcnew System::Windows::Forms::Label());
this->label2 = (gcnew System::Windows::Forms::Label());
this->label3 = (gcnew System::Windows::Forms::Label());
this->button1 = (gcnew System::Windows::Forms::Button());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->textBox3 = (gcnew System::Windows::Forms::TextBox());
this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
(cli::safe_cast<System::ComponentModel::ISupportInitialize^
>(this->pictureBox1))->BeginInit();
this->SuspendLayout();
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(12, 9);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(14, 13);
this->label1->TabIndex = 0;
this->label1->Text = L"X";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(95, 9);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(14, 13);
this->label2->TabIndex = 1;
this->label2->Text = L"Y";
//
// label3
//
this->label3->AutoSize = true;
this->label3->Location = System::Drawing::Point(178, 9);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(43, 13);
this->label3->TabIndex = 2;
this->label3->Text = L"Largura";
//
// button1
//
this->button1->Location = System::Drawing::Point(290, 4);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(57, 23);
this->button1->TabIndex = 3;
this->button1->Text = L"Plotar";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Plotador::button1_Click);
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(32, 6);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(57, 20);
this->textBox1->TabIndex = 4;
//
// textBox2
//
this->textBox2->Location = System::Drawing::Point(115, 6);
this->textBox2->Name = L"textBox2";
this->textBox2->Size = System::Drawing::Size(57, 20);
this->textBox2->TabIndex = 5;
//
// textBox3
//
this->textBox3->Location = System::Drawing::Point(227, 6);
this->textBox3->Name = L"textBox3";
this->textBox3->Size = System::Drawing::Size(57, 20);
this->textBox3->TabIndex = 6;
//
// pictureBox1
//
this->pictureBox1->Location = System::Drawing::Point(10, 34);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size = System::Drawing::Size(540, 369);
this->pictureBox1->TabIndex = 7;
this->pictureBox1->TabStop = false;
this->pictureBox1->Paint += gcnew System::Windows::Forms::PaintEventHandler
(this, &Plotador::pictureBox1_Paint);
//
// Plotador
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(558, 412);
this->Controls->Add(this->pictureBox1);
this->Controls->Add(this->textBox3);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->button1);
this->Controls->Add(this->label3);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Name = L"Plotador";
this->Text = L"Plotador de Quadrados";
(cli::safe_cast<System::ComponentModel::ISupportInitialize^
>(this->pictureBox1))->EndInit();
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
Quadrado ^q1, ^q2, ^q3;
q1 = gcnew Quadrado();
q1->Plotar( this->quadro );
q2 = gcnew Quadrado( 400, 100 );
q2->Plotar( this->quadro );
q3 = gcnew Quadrado( 50, 50, 20);
q3->Plotar( this->quadro );
this->pictureBox1->Refresh() ;
delete q1;
delete q2;
delete q3;
}
private: System::Void pictureBox1_Paint(System::Object^ sender,
System::Windows::Forms::PaintEventArgs^ e)
{
e->Graphics->DrawImage( this->figura, 0, 0 ); // bitmap e ponto(x, y)
}
}; // class
} // namespace
E para encerrar, a saída da execução:
Como também na versão C++ Builder, fica faltando complementar o código com as instruções para plotar o quadrado desejado conforme sua localização e largura indicadas.
Até mais.
Nenhum comentário:
Postar um comentário