p> In developing securities analysis software, it is often necessary to draw analytical curve for various stocks. In order to make the software's functions more convenient and flexible, users hope to be able to customize the colors of various curves according to their preferences. There are similar functions in the Font dialog box under the [Format] menu of WORD97. When the user clicks on the color drop-down box in the font dialog box, simple patterns of various colors are displayed together with the color names of the fonts. The result of this processing is obviously much better than providing only a drop-down box with a color name. 1. Implementation of custom color dialog box
In Delphi, we can use TComboBox to achieve similar functionality. There is a Style attribute in the TcomboBox component, which determines the display attribute of TcomboBox. Usually, csDropDown, CSSimple, csDropDownList, csOwnerDrawFixed, csOwnerDrawVariable, etc. can be selected. When csOwnerDrawFixed is selected, it means to create a self-drawn drop-down box. The height of each item in the drop-down box is determined by the ItemHeight property. And the self-painting process must be responded to in the OnDrawItem event of TcomboBox. OnDrawItem is defined as:
PRopertyOnDrawItem:TDrawItemEvent;
TDrawItemEvent =procedure(Control:TWinControl;Index:IntegerRect:TRect; State:TOwnerDrawState) ofobject;
The meaning of three parameters is:
Control: TComboBox containing drop-down box
Index: The index number of the self-drawn drop-down box in the Items property of TComboBox
Rect: The position of self-drawing Therefore, we know the position of the rectangle that needs to be self-drawn (Rect parameter) and the index number (Index parameter) in TComboBox, we can use the Canvas property of TcomboBox to draw on its canvas. The specific implementation process is as follows:
1. Create a new project file and set the relevant properties of its default form to:
Caption Custom drop-down box
Name Form1
PositionpoScreenCenter
2. Place two TcomboBox components in the form and set their properties as follows:
NameStyleItemHeightOnDrawItem
ColorCombo1csOwnerDrawFixed 20ColorComboDrawItem
ColorCombo2csOwnerDrawFixed 30ColorComboDrawItem
3. Double-click the dot button next to the Items properties of ColorCombo1 and ColorCombo2 and enter it in the StringListEditor dialog box.
black
blue
Blue and green
Bright green
red
yellow
Names of various colors
4. In the OnDrawItem event in ColorCombo1, Yu Manlu?
procedureTForm1.ColorComboDrawItem(Control: TWinControl;Index:Integer;Rect:TRect;State:OwnerDrawState);
var
TempColor:TColor; //Picture color
TempBrushColor:TColor; //Temporary color
Begin
with(ControlasTComboBox)do
//Draw it on Combo's Canvas
Begin
TempBrushColor:=Canvas.Brush.Color;
//Save the original color
Canvas.FillRect(Rect);
caseIndexof//Define different self-painted colors according to the Index
0://black
TempColor:=clBlack;
1://blue
TempColor:=clBlue;
2://blue and green
TempColor:=clAqua;
3://bright green
TempColor:=clLime;
4: //Red
TempColor:=clRed;
5://yellow
TempColor:=clyellow;
//Response to other colors can be added here.
end;
Canvas.Brush.Color:=TempColor;
// Self-drawn color rectangle
Canvas.Rectangle(Rect.Left+4,
Rect.Top+1,
(Rect.Right+Rect.Left)div3,
Rect.Bottom 1);
Canvas.Brush.Color:=TempBrushColor;
//Show the string corresponding to the color
Canvas.TextOut((Rect.Left+Rect.Right)div2,
Rect.Top+1,
Items[Index]);
end;
end;
5. Save and run the file, we can see the same effect as the color drop-down box in WORD
Readers who are interested can add other colors to the locations shown in the article.
The above program is passed on Delphi3.0, 4.0. 2. Write custom color dialog components
For many Delphi programmers, it is quite unfamiliar to how to write their own Delphi components. Delphi components actually continue to develop from the Tcomponent class, and writing components is actually writing extraordinary classes. Below we will introduce the writing of components using the custom color dialog box as an example. The following TColorComboBox continues from the TcomboBox class. When clicking the drop-down arrow on the right, the various colors of the corresponding drop-down items pop up. 1. Select the New Component option in the Component menu item. Select TcomboBox in the AncestorType box, fill in TColorComboBox in the ClassName box, select Samples in the Palette Page box, fill in ColorComboBox.pas in the UnitFileName box, and then click the OK button.
2. Select the Install Component option in the Component menu item, click Intonewpackage, write the path and ColorComboDpk.dpk in the package name box, and click OK to generate the ColorComboDpk.bpl file.
3. Use ImageEditor in the Tools menu to create the edit file ColorComBox.dcr and create a bitmap for the TColorComboBox class.
4. Add the specifications for the font size and height and the control's Style attribute (set to csOwnerDrawFixed) in CreateWnd executed after Create. If so many color items are not needed, you can generate them later. The control's items property directly deletes the unwanted colors.
5. Add a color self-painting program to the DrawItem event, which occurs before On DrawItem.
The implementation procedure is as follows:
unitColorComboBox;
interface
uses
Windows, Messages, SysUtils, Classes,
Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TColorComboBox=class(TComboBox)
Private
{Privatedeclarations}
FOnDrawItem:TDrawItemEvent;
procedureDrawItem(Index:Integer;Rect:TRect; State:TOwnerDrawState); override;
protected
{Protecteddeclarations}
public
{Publicdeclarations}
constrUCtorCreate(AOwner:TComponent);override;
procedureCreateWnd;override;
published
{Publisheddeclarations}
propertyOnDrawItem:TDrawItemEvent
ReadFOnDrawItemwriteFOnDrawItem;
end;
procedureRegister;
Implementation
procedureRegister;//Register component
Begin
RegisterComponents(Samples,[TColorComboBox]);
end;
constructorTColorComboBox.Create
(AOwner:TComponent);//Initialization of component
Begin
inheritedCreate(AOwner);
Style:=csOwnerDrawFixed; //The initial type of component
ItemHeight :=20;
Font.Size:=10;
end;
procedureTColorComboBox.CreateWnd;
//Initialize the Items property of the color component
Begin
inheritedCreateWnd;
Items.Clear;
Items.Add(black);
Items.Add(blue);
Items.Add(blue and green);
Items.Add(bright green);
Items.Add(pink);
Items.Add(red);
Items.Add(yellow);
Items.Add(white);
Items.Add(dark blue);
Items.Add(cyan);
Items.Add(green);
Items.Add(purple);
Items.Add(Crimson Red);
Items.Add(deep yellow);
Items.Add(dark gray);
Items.Add(silver);
//If you do not need so many colors, you can delete unwanted colors in the item attribute of the component
end;
//Overload the DrawItem process
procedureTColorComboBox.DrawItem(Index: Integer;Rect:TRect;State:TOwnerDrawState);
var
TempColor:TColor; //Picture color
TempBrushColor:TColor; //Temporary color
Begin
//The default self-painting settings of this component
TempBrushColor:=Canvas.Brush.Color;
//Save the original color
Canvas.FillRect(Rect);
ifItems[index]=black then
TempColor:=clBlack
elseifItems[index]=blue then
TempColor:=clBlue
elseifItems[index]=blue green then
TempColor:=clAqua
elseifItems[index]=bright green then
TempColor:=clLime
elseifItems[index]=pink then
TempColor:=clFuchsia
elseifItems[index]=red then
TempColor:=clRed
elseifItems[index]=yellow then
TempColor:=clYellow
elseifItems[index]=white then
TempColor:=clWhite
elseifItems[index]=dark blue then
TempColor:=clNavy
elseifItems[index]=Cyan then
TempColor:=clTeal
elseifItems[index]=green then
TempColor:=clGreen
elseifItems[index]=purple then
TempColor:=clPurple
elseifItems[index]=Crimson then
TempColor:=clMaroon
elseifItems[index]=deep yellow then
TempColor:=clOlive
elseifItems[index]=dark gray then
TempColor:=clGray
elseifItems[index]=silver then
elseTempColor:=clSilver;
Canvas.Brush.Color:=TempColor;
// Self-drawn color rectangle
Canvas.Rectangle(Rect.Left+4,
Rect.Top+1,
(Rect.Right+Rect.Left)div3,
Rect.Bottom 1);
Canvas.Brush.Color:=TempBrushColor;
//Show the string corresponding to the color
Canvas.TextOut((Rect.Left+Rect.Right)div2,
Rect.Top+1,
Items[Index]);
end;
end. This control can be used in all programs that require color options and is very convenient and beautiful, and saves programming a lot of time and increases program reliability and readability. 3. Use of custom color dialog components
After registering a custom color component, you can select a custom color component from the Sample page of the Delphi component template, and there is no difference between using Delphi's own component.