- Start a new application
- Resize the form and design it as follows:
- From the Standard property sheet of the Component Palette, click the
Panel and click on the form.
- Position the panel at Left = 16 and Top = 24
- Change its Color value to clSilver
- Change the name of the panel to pnlPreview
- On the Component Palette, click ScrollBar and click on the form. In
the same way, place two more scroll bars on the from.
- Change the names of the scroll bars from left to right to scrRed,
scrGreen, and scrBlue
respectively.
- Change the Min value of each ScrollBar control to 0
- Change the Max value of each ScrollBar control to 255
- Change the Position value of each ScrollBar control to 192
- On the Component Palette, click the GroupBox and click under the
panel on the form. Change the caption of the group box to RGB
- Inside the group box on the form, add three labels with captions of
&Red, &Green, and &Blue respectively.
- Again inside the group box on the form, add three Edit controls,
each on the right side of the previously added labels.
- Change the names of the new edit boxes to edtRGBRed, edtRGBGreen,
and edtRGBBlue respectively.
- Change the Text value of each Edit control to CO
- On the form, click the group box to select it.
- Press Ctrl + C to copy the selection.
- Press Ctrl + V to paste the selection.
- Move the newly pasted group box and its hosted controls under the
scroll bars.
- Change the caption of the right group box to Numeric.
- Change the names of the right edit boxes to edtRed, edtGreen, and
edtBlue respectively.
- Change the Text value of each of the newly pasted Edit controls to
192
- In the lower section of the form, add a new label with a caption of
&Color:
- Add a new Edit control on the right side of the Color label.
- Change its name to edtColor and Change its Text value to #COCOCO
- Add a button to the form and change its name to btnClose
- To test it, press F9.
|
- On the form, double-click the Red scroll bar to access its OnChange()
event.
- Inplmenet the event as follows (the comments explain what the code
is doing):
//---------------------------------------------------------------------------
void __fastcall TForm1::scrRedChange(TObject *Sender)
{
// While the user is scrolling the Red scroll bar
// get the integer value or position of the scroll bar
edtRed->Text = scrRed->Position;
// Get the Position value of the Red scroll bar.
// Format it to a HEX value
// Display the result in the Red Edit control of the RGB section
edtRGBRed->Text = IntToHex(scrRed->Position, 2);
// Get the current Position of each scroll bar
// Combine these values to create an RGB color
// Preview the resulting color in the Panel
pnlPreview->Color = TColor(RGB(scrRed->Position,
scrGreen->Position,
scrBlue->Position));
// Get the current Position of each scroll bar
// Convert this Position to a HEX value
// Using these HEX values, create an RGB (Web) color
// Display the resulting RGB color in the Color edit box,
// preceded by a # sign
edtColor->Text = "#" + IntToHex(scrRed->Position, 2)
+ IntToHex(scrGreen->Position, 2)
+ IntToHex(scrBlue->Position, 2);
}
//---------------------------------------------------------------------------
|
- To test the program again, press F9. Scroll the Red scroll bar and
observe the Preview panel. Also observe the edit boxes.
- Return to Bcb
- Implement the OnChange() event of the scroll bars:
//---------------------------------------------------------------------------
void __fastcall TForm1::scrGreenChange(TObject *Sender)
{
edtGreen->Text = scrGreen->Position;
edtRGBGreen->Text = IntToHex(scrGreen->Position, 2);
pnlPreview->Color = TColor(RGB(scrRed->Position,
scrGreen->Position,
scrBlue->Position));
edtColor->Text = "#" + IntToHex(scrRed->Position, 2)
+ IntToHex(scrGreen->Position, 2)
+ IntToHex(scrBlue->Position, 2);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::scrBlueChange(TObject *Sender)
{
edtBlue->Text = scrBlue->Position;
edtRGBBlue->Text = IntToHex(scrBlue->Position, 2);
pnlPreview->Color = TColor(RGB(scrRed->Position,
crGreen->Position,
scrBlue->Position));
edtColor->Text = "#" + IntToHex(scrRed->Position, 2)
+ IntToHex(scrGreen->Position, 2)
+ IntToHex(scrBlue->Position, 2);
}
//---------------------------------------------------------------------------
|
- Test the program and return to Bcb.
|
|
|