Doodle
#pragma
once
namespace
VCDoodle
{
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 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.
///
</summary>
public ref
class Form1 :
public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
///
<summary>
/// Clean up any resources being used.
///
</summary>
///
<param name="disposing">"description of
the parameter"</param>
private:
System::Windows::Forms::Panel^
pnlControl;
private:
System::Windows::Forms::Button^
btnClear;
private:
System::Windows::Forms::ComboBox^
cbxColor;
protected:
protected:
private:
System::Windows::Forms::Label^
lblColor;
private:
System::Windows::Forms::Button^
btnDraw;
private:
System::Windows::Forms::Button^
btnFillCircle;
private:
System::Windows::Forms::Button^
btnFillRectangle;
private:
System::Windows::Forms::Button^
btnCircle;
private:
System::Windows::Forms::Button^
btnLine;
private:
System::Windows::Forms::Button^
btnRectangle;
private:
System::Windows::Forms::Label^
lblControls;
private:
///
<summary>
/// Required designer variable.
///
</summary>
System::ComponentModel::Container ^components;
//Handle to manage form's drawing
functionality
System::Drawing::Graphics ^FormGraphic;
//Handle to manage drawing pen
System::Drawing::Pen ^pen;
//Handle to manage graphical brush
System::Drawing::SolidBrush ^brush;
//Variables to manage drawing coordinates
System::Drawing::Point ptStart, ptEnd;
//Values for possible drawing states
enum class
DrawingMode: Int16 {
DRAWRECTANGE, DRAWLINE,
DRAWCIRCLE, DRAWFILLRECTANGLE,
DRAWFILLCIRCLE,
DRAWFREE, DRAWNONE
};
//Variables to track current drawing state
DrawingMode drmCurrentMode;
//Flag to determine if drawing is in
progress
Boolean blnDrawing;
#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->pnlControl = (gcnew
System::Windows::Forms::Panel());
this->btnClear = (gcnew
System::Windows::Forms::Button());
this->cbxColor = (gcnew
System::Windows::Forms::ComboBox());
this->lblColor = (gcnew
System::Windows::Forms::Label());
this->btnDraw = (gcnew
System::Windows::Forms::Button());
this->btnFillCircle = (gcnew
System::Windows::Forms::Button());
this->btnFillRectangle = (gcnew
System::Windows::Forms::Button());
this->btnCircle = (gcnew
System::Windows::Forms::Button());
this->btnLine = (gcnew
System::Windows::Forms::Button());
this->btnRectangle = (gcnew
System::Windows::Forms::Button());
this->lblControls = (gcnew
System::Windows::Forms::Label());
this->pnlControl->SuspendLayout();
this->SuspendLayout();
//
// pnlControl
//
this->pnlControl->BackColor =
System::Drawing::Color::LightGray;
this->pnlControl->Controls->Add(this->btnClear);
this->pnlControl->Controls->Add(this->cbxColor);
this->pnlControl->Controls->Add(this->lblColor);
this->pnlControl->Controls->Add(this->btnDraw);
this->pnlControl->Controls->Add(this->btnFillCircle);
this->pnlControl->Controls->Add(this->btnFillRectangle);
this->pnlControl->Controls->Add(this->btnCircle);
this->pnlControl->Controls->Add(this->btnLine);
this->pnlControl->Controls->Add(this->btnRectangle);
this->pnlControl->Controls->Add(this->lblControls);
this->pnlControl->Location =
System::Drawing::Point(0, 0);
this->pnlControl->Name = L"pnlControl";
this->pnlControl->Size =
System::Drawing::Size(130, 430);
this->pnlControl->TabIndex = 0;
//
// btnClear
//
this->btnClear->Location =
System::Drawing::Point(12, 304);
this->btnClear->Name = L"btnClear";
this->btnClear->Size =
System::Drawing::Size(96, 23);
this->btnClear->TabIndex = 9;
this->btnClear->Text = L"Clear";
this->btnClear->Click +=
gcnew System::EventHandler(this,
&Form1::btnClear_Click);
//
// cbxColor
//
this->cbxColor->FormattingEnabled =
true;
this->cbxColor->Items->AddRange(gcnew
cli::array< System::Object^
>(6) {L"Red", L"Green",
L"Blue", L"Black",
L"Yellow",
L"Purple"});
this->cbxColor->Location =
System::Drawing::Point(4, 260);
this->cbxColor->Name = L"cbxColor";
this->cbxColor->Size =
System::Drawing::Size(121, 21);
this->cbxColor->TabIndex = 8;
this->cbxColor->Text = L"Red";
this->cbxColor->SelectedIndexChanged
+= gcnew System::EventHandler(this,
&Form1::cbxColor_SelectedIndexChanged);
//
// lblColor
//
this->lblColor->AutoSize =
true;
this->lblColor->Font = (gcnew
System::Drawing::Font(L"Microsoft Sans Serif",
8.25F, System::Drawing::FontStyle::Bold,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->lblColor->Location =
System::Drawing::Point(28, 244);
this->lblColor->Name = L"lblColor";
this->lblColor->Size =
System::Drawing::Size(80, 13);
this->lblColor->TabIndex = 7;
this->lblColor->Text = L"Select
Color:";
//
// btnDraw
//
this->btnDraw->Location =
System::Drawing::Point(12, 194);
this->btnDraw->Name = L"btnDraw";
this->btnDraw->Size =
System::Drawing::Size(96, 23);
this->btnDraw->TabIndex = 6;
this->btnDraw->Text = L"Draw";
this->btnDraw->Click +=
gcnew System::EventHandler(this,
&Form1::btnDraw_Click);
//
// btnFillCircle
//
this->btnFillCircle->Location =
System::Drawing::Point(12, 165);
this->btnFillCircle->Name = L"btnFillCircle";
this->btnFillCircle->Size =
System::Drawing::Size(96, 23);
this->btnFillCircle->TabIndex = 5;
this->btnFillCircle->Text = L"Fill
Circle";
this->btnFillCircle->Click +=
gcnew System::EventHandler(this,
&Form1::btnFillCircle_Click);
//
// btnFillRectangle
//
this->btnFillRectangle->Location =
System::Drawing::Point(12, 136);
this->btnFillRectangle->Name = L"btnFillRectangle";
this->btnFillRectangle->Size =
System::Drawing::Size(96, 23);
this->btnFillRectangle->TabIndex =
4;
this->btnFillRectangle->Text = L"Fill
Rectangle";
this->btnFillRectangle->Click +=
gcnew System::EventHandler(this,
&Form1::btnFillRectangle_Click);
//
// btnCircle
//
this->btnCircle->Location =
System::Drawing::Point(12, 107);
this->btnCircle->Name = L"btnCircle";
this->btnCircle->Size =
System::Drawing::Size(96, 23);
this->btnCircle->TabIndex = 3;
this->btnCircle->Text = L"Circle";
this->btnCircle->Click +=
gcnew System::EventHandler(this,
&Form1::btnCircle_Click);
//
// btnLine
//
this->btnLine->Location =
System::Drawing::Point(12, 78);
this->btnLine->Name = L"btnLine";
this->btnLine->Size =
System::Drawing::Size(96, 23);
this->btnLine->TabIndex = 2;
this->btnLine->Text = L"Line";
this->btnLine->Click +=
gcnew System::EventHandler(this,
&Form1::btnLine_Click);
//
// btnRectangle
//
this->btnRectangle->Location =
System::Drawing::Point(12, 49);
this->btnRectangle->Name = L"btnRectangle";
this->btnRectangle->Size =
System::Drawing::Size(96, 23);
this->btnRectangle->TabIndex = 1;
this->btnRectangle->Text = L"Rectangle";
this->btnRectangle->Click +=
gcnew System::EventHandler(this,
&Form1::btnRectangle_Click);
//
// lblControls
//
this->lblControls->AutoSize =
true;
this->lblControls->Font = (gcnew
System::Drawing::Font(L"Microsoft Sans Serif",
8.25F, System::Drawing::FontStyle::Bold,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->lblControls->Location =
System::Drawing::Point(40, 9);
this->lblControls->Name = L"lblControls";
this->lblControls->Size =
System::Drawing::Size(53, 13);
this->lblControls->TabIndex = 0;
this->lblControls->Text = L"Controls";
//
// Form1
//
this->AutoScaleDimensions =
System::Drawing::SizeF(6, 13);
this->AutoScaleMode =
System::Windows::Forms::AutoScaleMode::Font;
this->BackColor =
System::Drawing::Color::White;
this->ClientSize =
System::Drawing::Size(580, 424);
this->Controls->Add(this->pnlControl);
this->Cursor =
System::Windows::Forms::Cursors::Cross;
this->FormBorderStyle =
System::Windows::Forms::FormBorderStyle::Fixed3D;
this->Name = L"Form1";
this->Text = L"VC++
Doodle";
this->MouseUp +=
gcnew
System::Windows::Forms::MouseEventHandler(this,
&Form1::HandleMouseUp);
this->MouseMove +=
gcnew
System::Windows::Forms::MouseEventHandler(this,
&Form1::HandleMouseMove);
this->MouseDown +=
gcnew
System::Windows::Forms::MouseEventHandler(this,
&Form1::HandleMouseDown);
this->Load +=
gcnew System::EventHandler(this,
&Form1::Form1_Load);
this->pnlControl->ResumeLayout(false);
this->pnlControl->PerformLayout();
this->ResumeLayout(false);
}
#pragma
endregion
private:
System::Void Form1_Load(System::Object^
sender, System::EventArgs^
e) {
FormGraphic =
this->CreateGraphics();
pen =
gcnew System::Drawing::Pen( \
System::Drawing::Color::FromName( cbxColor->Text ) );
brush =
gcnew System::Drawing::SolidBrush( \
System::Drawing::Color::FromName( cbxColor->Text ) );
//Set
the default drawing coordinates to center screen
ptStart.X =
this->Size.Width / 2;
ptStart.Y =
this->Size.Height / 2;
ptEnd.X =
this->Size.Width / 2;
ptEnd.Y =
this->Size.Height / 2;
//Set
default drawing mode
drmCurrentMode =
DrawingMode::DRAWNONE;
blnDrawing =
false;
}
private:
System::Void btnRectangle_Click(System::Object^
sender, System::EventArgs^
e) {
drmCurrentMode =
DrawingMode::DRAWRECTANGE;
}
private:
System::Void btnLine_Click(System::Object^
sender, System::EventArgs^
e) {
drmCurrentMode =
DrawingMode::DRAWLINE;
}
private:
System::Void btnCircle_Click(System::Object^
sender, System::EventArgs^
e) {
drmCurrentMode =
DrawingMode::DRAWCIRCLE;
}
private:
System::Void btnFillRectangle_Click(System::Object^
sender, System::EventArgs^
e) {
drmCurrentMode =
DrawingMode::DRAWFILLRECTANGLE;
}
private:
System::Void btnFillCircle_Click(System::Object^
sender, System::EventArgs^
e) {
drmCurrentMode =
DrawingMode::DRAWFILLCIRCLE;
}
private:
System::Void btnDraw_Click(System::Object^
sender, System::EventArgs^
e) {
drmCurrentMode =
DrawingMode::DRAWFREE;
}
private:
System::Void cbxColor_SelectedIndexChanged(System::Object^
sender, System::EventArgs^
e) {
//Set
pen and brush colors
pen->Color = \
System::Drawing::Color::FromName( cbxColor->Text );
brush->Color = \
System::Drawing::Color::FromName( cbxColor->Text );
}
private:
System::Void btnClear_Click(System::Object^
sender, System::EventArgs^
e) {
//Clear
the form to the background color
FormGraphic->Clear(
this->BackColor );
}
private:
System::Void HandleMouseUp(System::Object^
sender, System::Windows::Forms::MouseEventArgs^
e) {
blnDrawing =
false;
//Stop
drawing if current mode is freehand drawing
if(
drmCurrentMode == DrawingMode::DRAWFREE )
return;
//Draw
shape
if(
drmCurrentMode != DrawingMode::DRAWNONE )
{
//If
the current mode is a line, draw it
if(
drmCurrentMode == DrawingMode::DRAWLINE )
{
FormGraphic->DrawLine( pen, ptStart, ptEnd );
return;
}
//Current
mode is a shape
//Create
a rectangle to represent shape extents
//
Height and width are composed of absolute
//
value difference between the two X and Y
//
coordinates
System::Drawing::Rectangle
shapeRect(
ptStart.X, ptStart.Y,
Math::Abs( ptEnd.X -
ptStart.X ),
Math::Abs( ptEnd.Y -
ptStart.Y ) );
//Draw
the shape
DrawShape( shapeRect );
}
}
private:
System::Void HandleMouseMove(System::Object^
sender, System::Windows::Forms::MouseEventArgs^
e) {
if(
blnDrawing == true )
{
ptEnd.X = e->X;
ptEnd.Y = e->Y;
if(
drmCurrentMode == DrawingMode::DRAWFREE )
{
//Offset
the current coordinates so that
//
a small line can be drawn to represent
//
the current point
ptStart.X = e->X + 1;
ptStart.Y = e->Y + 1;
FormGraphic->DrawLine( pen,
ptStart, ptEnd );
}
}
}
private:
System::Void HandleMouseDown(System::Object^
sender, System::Windows::Forms::MouseEventArgs^
e) {
if(
drmCurrentMode != DrawingMode::DRAWNONE )
{
//Now
drawing, set starting coordinates
ptStart.X = e->X;
ptStart.Y = e->Y;
blnDrawing =
true;
}
}
private:
System::Void DrawShape( System::Drawing::Rectangle rect ) {
//Set
a default shape rectangle
switch(
drmCurrentMode )
{
case
DrawingMode::DRAWRECTANGE :
FormGraphic->DrawRectangle(
pen, rect );
break;
case
DrawingMode::DRAWCIRCLE :
FormGraphic->DrawEllipse(
pen, rect );
break;
case
DrawingMode::DRAWFILLRECTANGLE :
FormGraphic->FillRectangle(
brush, rect );
break;
case
DrawingMode::DRAWFILLCIRCLE :
FormGraphic->FillEllipse(
brush, rect );
break;
}
}
};
}