[.net] Need help with paint type program

Started by
0 comments, last by ferr 16 years, 12 months ago
I am writing a program that is sort of similar to ms paint. In the window you can choose from four colors and three sizes. I also would like a fifth color option for a custom color box to let the user choose their own color. I am having trouble getting this to work. I also would like the user to be able to push + or - to make the size of the brush larger or smaller. Can anyone give me some ideas on what to change or add into my code to make both of these happen..... #pragma once namespace PainterTest { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; public __gc class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); } protected: void Dispose(Boolean disposing) { if (disposing && components) { components->Dispose(); } __super::Dispose(disposing); } private: static int width = 3, height = 3; private: static bool shouldPaint = false; // whether to paint private: System::Windows::Forms::GroupBox * groupBox1; private: System::Windows::Forms::GroupBox * groupBox2; private: System::Windows::Forms::RadioButton * radioButton1; private: System::Windows::Forms::RadioButton * radioButton2; private: System::Windows::Forms::RadioButton * radioButton3; private: System::Windows::Forms::RadioButton * radioButton4; private: System::Windows::Forms::RadioButton * radioButton5; private: System::Windows::Forms::RadioButton * radioButton6; private: System::Windows::Forms::RadioButton * radioButton7; private: System::Windows::Forms::RadioButton * radioButton8; private: System::ComponentModel::Container * components; void InitializeComponent(void) { this->groupBox1 = new System::Windows::Forms::GroupBox(); this->radioButton5 = new System::Windows::Forms::RadioButton(); this->radioButton4 = new System::Windows::Forms::RadioButton(); this->radioButton3 = new System::Windows::Forms::RadioButton(); this->radioButton2 = new System::Windows::Forms::RadioButton(); this->radioButton1 = new System::Windows::Forms::RadioButton(); this->groupBox2 = new System::Windows::Forms::GroupBox(); this->radioButton8 = new System::Windows::Forms::RadioButton(); this->radioButton7 = new System::Windows::Forms::RadioButton(); this->radioButton6 = new System::Windows::Forms::RadioButton(); this->groupBox1->SuspendLayout(); this->groupBox2->SuspendLayout(); this->SuspendLayout(); // // groupBox1 // this->groupBox1->BackColor = System::Drawing::SystemColors::ControlDark; this->groupBox1->Controls->Add(this->radioButton5); this->groupBox1->Controls->Add(this->radioButton4); this->groupBox1->Controls->Add(this->radioButton3); this->groupBox1->Controls->Add(this->radioButton2); this->groupBox1->Controls->Add(this->radioButton1); this->groupBox1->Location = System::Drawing::Point(0, 0); this->groupBox1->Name = S"groupBox1"; this->groupBox1->Size = System::Drawing::Size(80, 160); this->groupBox1->TabIndex = 0; this->groupBox1->TabStop = false; this->groupBox1->Text = S"Color"; // // radioButton5 // this->radioButton5->Location = System::Drawing::Point(0, 112); this->radioButton5->Name = S"radioButton5"; this->radioButton5->TabIndex = 4; this->radioButton5->Text = S"Custom"; // // radioButton4 // this->radioButton4->Location = System::Drawing::Point(0, 88); this->radioButton4->Name = S"radioButton4"; this->radioButton4->TabIndex = 3; this->radioButton4->Text = S"Black"; this->radioButton4->CheckedChanged += new System::EventHandler(this, radioButton4_CheckedChanged); // // radioButton3 // this->radioButton3->Location = System::Drawing::Point(0, 64); this->radioButton3->Name = S"radioButton3"; this->radioButton3->TabIndex = 2; this->radioButton3->Text = S"Green"; // // radioButton2 // this->radioButton2->Location = System::Drawing::Point(0, 40); this->radioButton2->Name = S"radioButton2"; this->radioButton2->TabIndex = 1; this->radioButton2->Text = S"Blue"; // // radioButton1 // this->radioButton1->AccessibleRole = System::Windows::Forms::AccessibleRole::Clock; this->radioButton1->Location = System::Drawing::Point(0, 16); this->radioButton1->Name = S"radioButton1"; this->radioButton1->TabIndex = 0; this->radioButton1->Text = S"Red"; this->radioButton1->Paint += new System::Windows::Forms::PaintEventHandler(this, Red); this->radioButton1->CheckedChanged += new System::EventHandler(this, radioButton1_CheckedChanged); // // groupBox2 // this->groupBox2->BackColor = System::Drawing::SystemColors::ControlDark; this->groupBox2->Controls->Add(this->radioButton8); this->groupBox2->Controls->Add(this->radioButton7); this->groupBox2->Controls->Add(this->radioButton6); this->groupBox2->Location = System::Drawing::Point(0, 160); this->groupBox2->Name = S"groupBox2"; this->groupBox2->Size = System::Drawing::Size(80, 128); this->groupBox2->TabIndex = 1; this->groupBox2->TabStop = false; this->groupBox2->Text = S"Size"; this->groupBox2->Enter += new System::EventHandler(this, groupBox2_Enter); // // radioButton8 // this->radioButton8->Location = System::Drawing::Point(0, 64); this->radioButton8->Name = S"radioButton8"; this->radioButton8->TabIndex = 2; this->radioButton8->Text = S"Large"; // // radioButton7 // this->radioButton7->Location = System::Drawing::Point(0, 40); this->radioButton7->Name = S"radioButton7"; this->radioButton7->TabIndex = 1; this->radioButton7->Text = S"Medium"; // // radioButton6 // this->radioButton6->Location = System::Drawing::Point(0, 16); this->radioButton6->Name = S"radioButton6"; this->radioButton6->TabIndex = 0; this->radioButton6->Text = S"Small"; // // Form1 // this->AutoScaleBaseSize = System::Drawing::Size(5, 13); this->ClientSize = System::Drawing::Size(464, 286); this->Controls->Add(this->groupBox2); this->Controls->Add(this->groupBox1); this->Name = S"Form1"; this->Text = S"Painter"; this->MouseDown += new System::Windows::Forms::MouseEventHandler(this, Form1_MouseDown); this->MouseUp += new System::Windows::Forms::MouseEventHandler(this, Form1_MouseUp); this->MouseMove += new System::Windows::Forms::MouseEventHandler(this, Form1_MouseMove); this->groupBox1->ResumeLayout(false); this->groupBox2->ResumeLayout(false); this->ResumeLayout(false); } // should paint after mouse button has been pressed private: System::Void Form1_MouseDown(System::Object * sender, System::Windows::Forms::MouseEventArgs * e) { shouldPaint = true; } // stop painting when mouse button released private: System::Void Form1_MouseUp(System::Object * sender, System::Windows::Forms::MouseEventArgs * e) { shouldPaint = false; } // draw circle whenever mouse button moves (and mouse is down) private: System::Void Form1_MouseMove(System::Object * sender, System::Windows::Forms::MouseEventArgs * e) { if(shouldPaint) { Graphics * graphics = CreateGraphics(); if(radioButton6->Checked) { width = 3; height = 3; } else if(radioButton7->Checked) { width = 6; height = 6; } else { width = 9; height = 9; } //end if if(radioButton1->Checked) { graphics->FillEllipse(new SolidBrush( Color::Red), e->X, e->Y, width, height); } else if(radioButton2->Checked) { graphics->FillEllipse(new SolidBrush( Color::Blue), e->X, e->Y, width, height); } else if(radioButton3->Checked) { graphics->FillEllipse(new SolidBrush( Color::Green), e->X, e->Y, width, height); } else if(radioButton4->Checked) { graphics->FillEllipse(new SolidBrush( Color::Black), e->X, e->Y, width, height); } else if(radioButton5->Checked) { graphics->FillEllipse(new SolidBrush( ), e->X, e->Y, width, height);//find way to show custom color box } } } private: System::Void red(System::Object * sender, System::EventArgs * e) { } private: System::Void groupBox2_Enter(System::Object * sender, System::EventArgs * e) { } private: System::Void radioButton1_CheckedChanged(System::Object * sender, System::EventArgs * e) { } private: System::Void radioButton4_CheckedChanged(System::Object * sender, System::EventArgs * e) { } private: System::Void Red(System::Object * sender, System::Windows::Forms::PaintEventArgs * e) { } private: System::Void Form1_KeyDown(System::Object * sender, System::Windows::Forms::KeyEventArgs * e) { if(e->KeyCode == Keys::Add) { //this isn't working to change size width++; height++; } else if(e->KeyCode == Keys::Subtract) { width--; height--; } Thanks for any help!!!!
Advertisement
Have you tried setting a breakpoint inside the Add/Subtract keydown event handler to see if it is actually detected? It may be that Key::Add is not what you think it is.

Are you using a Color Dialog for custom colors? http://www.java2s.com/Code/CSharp/GUI-Windows-Form/Displaycolordialogandgetuserselection.htm

What is this Mono or something? What's with the __gc in your class declaration for Form1 and the use of stuff like "private:", etc. Why are you using pointers?

edit: wait, you're trying to change width and height with -/+ but in your MouseMove handler you've got:
if(radioButton6->Checked)
{
width = 3;
height = 3;
}
else if(radioButton7->Checked)
{
width = 6;
height = 6;
}
else
{
width = 9;
height = 9;
} //end if

You may want to change that last else to an else if looking for the Large radio button.

This topic is closed to new replies.

Advertisement