Practical
Learning: Creating the Application
|
|
- To create a new program, on the main menu, click File -> New ->
Project...
- In the Templates list, click Windows Forms Application
- Set the Name to ColorSelector and click OK
- Copy the following picture and paste it somewhere in your computer
- Design the form as follows:
|
Control |
Text |
Name |
Other Properties |
PictureBox |
|
pbxColor |
Image: colorpal1.jpg |
Label |
Red: |
|
|
TextBox |
|
txtRed |
|
Label |
Green: |
|
|
TextBox |
|
txtGreen |
|
Label |
Blue: |
|
|
TextBox |
|
txtBlue |
|
Panel |
|
pnlPreview |
BorderStyle: FixedSingle |
Button |
Close |
btnClose |
|
|
- Right-click the form and click View Code
- In the body of the class, declare a Boolean variable named IsSelecting:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
bool isSelecting;
- Return to the form and double-click an unoccupied area of its body
- Implement the event as follows:
System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
isSelecting = false;
}
- Return to the form and click the picture box control on it
- In the Properties window, click the Events button
and double-click MouseDown
- Implement the event as follows:
System::Void pbxColor_MouseDown(System::Object^ sender,
System::Windows::Forms::MouseEventArgs^ e)
{
isSelecting = true;
}
- Return to the form and click the picture box control on it
- In the Events section of the Properties window, double-click MouseUp and
implement the event as follows:
System::Void pbxColor_MouseUp(System::Object^ sender,
System::Windows::Forms::MouseEventArgs^ e)
{
isSelecting = false;
}
- Return to the form and click the picture box control on it
- In the Events section of the Properties window, double-click MouseMove and
implement the event as follows:
System::Void pbxColor_MouseMove(System::Object^ sender,
System::Windows::Forms::MouseEventArgs^ e)
{
if( isSelecting == true )
{
Bitmap ^ bmpImage = static_cast<Bitmap ^>(pbxColor->Image);
Color clr = bmpImage->GetPixel(e->X, e->Y);
txtRed->Text = clr.R.ToString();
txtGreen->Text = clr.G.ToString();
txtBlue->Text = clr.B.ToString();
pnlPreview->BackColor = clr;
}
}
- Execute the application
- Click the mouse on the picture mouse and drag to produce a color
- Close the form
|
|