FunctionX Practical Learning Logo

The Color Changer

 

Introduction

Color Changer

In this exercise, we will use track bars to change the color of a panel control.

 

Creating the Application

The simplest static control is considered a label. It consists of a simple object that displays text. The user cannot directly change this text unless you programmatically allow it. This form of the static control is the simplest control you can use in a VCL application.

To add a simple label to a dialog box, you can click the Label button on the Standard tab of the Component Palette. The only thing you need to do is to change the Caption property on the Object Inspector window.

If you cannot add a label to the dialog box when designing it, you can programmatically create one. This is done using the TLabel class and calling its constructor.

 

Practical Learning: Starting the Exercise

  1. Start Borland C++ Builder and create a new application
  2. To start saving the application, on the Standard toolbar, click the Save All button. Create a new folder named ColorChanger1 and display it in the Save In combo box
  3. Save the unit as Main and save the project as ColorChanger
  4. Design the dialog box as follows:
     
    Color Changer in Design
    Control Name Caption Additional Properties
    Form   frmMain Color Changer BorderStyle: bsDialog
    Position: poScreenCenter
    Panel Panel pnlPreview None Color: clSilver
    TrackBar Trackbar trcRed   Frequency: 20
    Max: 255
    Orientation: trVertical
    Position: 63
    TickMarks: tmBoth
    TrackBar Trackbar trcGreen   Frequency: 20
    Max: 255
    Orientation: trVertical
    Position: 63
    TickMarks: tmBoth
    TrackBar Trackbar trcBlue   Frequency: 20
    Max: 255
    Orientation: trVertical
    Position: 63
    TickMarks: tmBoth
    BitBtn Bitmap Button     Kind: bkClose
    Label Label lblRed 192 Alignment: taCenter
    Label Label lblGreen 192 Alignment: taCenter
    Label Label lblBlue 192 Alignment: taCenter
  5. Save All
  6. On the form, double-click the left track bar and implement its OnChange event as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::trcRedChange(TObject *Sender)
    {
        int iRed   = 255 - trcRed->Position;
        int iGreen = 255 - trcGreen->Position;
        int iBlue  = 255 - trcBlue->Position;
    
        pnlPreview->Color = TColor(RGB(iRed, iGreen, iBlue));
        lblRed->Caption   = IntToStr(iRed);
    }
    //---------------------------------------------------------------------------
  7. On the form, double-click the middle track bar and implement its OnChange event as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TForm1::trcGreenChange(TObject *Sender)
    {
        int iRed   = 255 - trcRed->Position;
        int iGreen = 255 - trcGreen->Position;
        int iBlue  = 255 - trcBlue->Position;
    
        pnlPreview->Color = TColor(RGB(iRed, iGreen, iBlue));
        lblGreen->Caption   = IntToStr(iGreen);
    }
    //---------------------------------------------------------------------------
  8. On the form, double-click the right track bar and implement its OnChange event as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::trcBlueChange(TObject *Sender)
    {
        int iRed   = 255 - trcRed->Position;
        int iGreen = 255 - trcGreen->Position;
        int iBlue  = 255 - trcBlue->Position;
    
        pnlPreview->Color = TColor(RGB(iRed, iGreen, iBlue));
        lblBlue->Caption   = IntToStr(iBlue);
    }
    //---------------------------------------------------------------------------
  9. Test the application
  10. After using it, close it and return to your programming environment.
 

Home Copyright 2003-2007 FunctionX, Inc. FunctionX