Color Changer

 

This exercise combines a panel, a button, and three scroll bars combined to create an application.

Prerequisites:
Panel
Button
TrackBar

Color Changer

 

  1. Start a new project and, in the New Project dialog box, select Managed C++ Application from the Visual C++ Projects node
  2. Set the Name to WinForms1 and click OK
  3. From Solution Explorer, expand WinForms1 and double-click _tmain().
  4. Replace the contents of the file with the following:
     
    // This is the main project file for VC++ application project 
    // generated using an Application Wizard.
    
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Drawing.dll>
    #using <System.Windows.Forms.dll>
    
    using namespace System;
    using namespace System::Drawing;
    using namespace System::ComponentModel;
    using namespace System::Windows::Forms;
    
    __gc public class ColorForm : public Form
    {
    public:
    	ColorForm();
    private:
    	Panel    *pnlPreview;
    	TrackBar *scrRed, *scrGreen, *scrBlue;
    	Button	 *btnClose;
    	Label	 *lblRed, *lblGreen, *lblBlue;
    
    	void CreateCloseButton();
    	void CreateRedScrollBar();
    	void CreateGreenScrollBar();
    	void CreateBlueScrollBar();
    
    	void scrRedScroll(Object *Sender,   System::EventArgs* pArgs);
    	void scrGreenScroll(Object *Sender, System::EventArgs* pArgs);
    	void scrBlueScroll(Object *Sender,  System::EventArgs* pArgs);
    	void btnCloseClick(Object *Sender,  System::EventArgs* pArgs);
    };
    
    void ColorForm::CreateCloseButton()
    {
    	btnClose = new Button;
    	btnClose->Location = Point(50, 150);
    	btnClose->Text = S"&Close";
    	btnClose->Location = Point(60, 150);
    	btnClose->Click += new EventHandler(this, &ColorForm::btnCloseClick);
    
    	Controls->Add(btnClose);
    }
    
    void ColorForm::CreateRedScrollBar()
    {
    	scrRed = new TrackBar;
    	scrRed->Orientation = System::Windows::Forms::Orientation::Vertical;
    	scrRed->TickStyle = System::Windows::Forms::TickStyle::Both;
    	scrRed->Size = System::Drawing::Size(160, 140);
    	scrRed->Location = System::Drawing::Point(180, 8);
    	scrRed->Minimum = 0;
    	scrRed->Maximum = 255;
    	scrRed->Value = 128;
    	scrRed->TickFrequency = 20;
    	scrRed->add_ValueChanged(new EventHandler(this, scrRedScroll));
    
    	Controls->Add(scrRed);
    }
    void ColorForm::CreateGreenScrollBar()
    {
    	scrGreen = new TrackBar;
    	scrGreen->Orientation = System::Windows::Forms::Orientation::Vertical;
    	scrGreen->TickStyle = System::Windows::Forms::TickStyle::Both;
    	scrGreen->Size = System::Drawing::Size(160, 140);
    	scrGreen->Location = System::Drawing::Point(230, 8);
    	scrGreen->Minimum = 0;
    	scrGreen->Maximum = 255;
    	scrGreen->Value = 128;
    	scrGreen->TickFrequency = 20;
    	scrGreen->add_ValueChanged(new EventHandler(this, scrGreenScroll));
    
    	Controls->Add(scrGreen);
    }
    
    void ColorForm::CreateBlueScrollBar()
    {
    	scrBlue = new TrackBar;
    	scrBlue->Orientation = System::Windows::Forms::Orientation::Vertical;
    	scrBlue->TickStyle = System::Windows::Forms::TickStyle::Both;
    	scrBlue->Size = System::Drawing::Size(160, 140);
    	scrBlue->Location = System::Drawing::Point(280, 8);
    	scrBlue->Minimum = 0;
    	scrBlue->Maximum = 255;
    	scrBlue->Value = 128;
    	scrBlue->TickFrequency = 20;
    	scrBlue->add_ValueChanged(new EventHandler(this, scrBlueScroll));
    
    	Controls->Add(scrBlue);
    }
    
    ColorForm::ColorForm()
    {
    	Text = S"TrackBarExample";
    
    	pnlPreview = new Panel;
    	pnlPreview->Size = System::Drawing::Size(160, 125);
    	pnlPreview->Location = System::Drawing::Point(14, 14);
    	pnlPreview->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
    	pnlPreview->BackColor = System::Drawing::Color::FromArgb(128, 128, 128);
    
    	lblRed = new Label;
    	lblRed->Text = "Red";
    	lblRed->AutoSize = true;
    	lblRed->Location = Point(190, 150);
    	lblGreen = new Label;
    	lblGreen->Text = "Green";
    	lblGreen->AutoSize = true;
    	lblGreen->Location = Point(235, 150);
    	lblBlue = new Label;
    	lblBlue->Text = "Blue";
    	lblBlue->AutoSize = true;
    	lblBlue->Location = Point(290, 150);
    
    	CreateRedScrollBar();
    	CreateGreenScrollBar();
    	CreateBlueScrollBar();
    	CreateCloseButton();
    
    	Controls->Add(pnlPreview);
    	Controls->Add(scrRed);
    	Controls->Add(scrGreen);
    	Controls->Add(scrBlue);
    	Controls->Add(btnClose);
    	Controls->Add(lblRed);
    	Controls->Add(lblGreen);
    	Controls->Add(lblBlue);
    }
    
    void ColorForm::btnCloseClick(Object* Sender, System::EventArgs *pArgs)
    {
    	Close();
    }
    
    void ColorForm::scrRedScroll(Object* Sender, System::EventArgs* pArgs)
    {
    	int RedValue   = scrRed->Value;
    	int GreenValue = scrGreen->Value;
    	int BlueValue  = scrBlue->Value;
    
    	pnlPreview->BackColor = System::Drawing::Color::FromArgb(RedValue, GreenValue, BlueValue);
    }
    
    void ColorForm::scrGreenScroll(Object *Sender, System::EventArgs* pArgs)
    {
    	int RedValue = scrRed->Value;
    	int GreenValue = scrGreen->Value;
    	int BlueValue  = scrBlue->Value;
    
    	pnlPreview->BackColor = System::Drawing::Color::FromArgb(RedValue, GreenValue, BlueValue);
    }
    
    void ColorForm::scrBlueScroll(Object* Sender, System::EventArgs* pArgs)
    {
    	int RedValue = scrRed->Value;
    	int GreenValue = scrGreen->Value;
    	int BlueValue  = scrBlue->Value;
    
    	pnlPreview->BackColor = System::Drawing::Color::FromArgb(RedValue, GreenValue, BlueValue);
    }
    // This is the entry point for this application
    int __stdcall WinMain()
    {
    	ColorForm *CF = new ColorForm();
    
    	CF->Height = 220;
    	CF->Width = 350;
    	Application::Run(CF);
    	return 0;
    }
  5. Test the application.

Home Copyright © 2002 FunctionX, Inc.