By default, the area a brush hits is uniformly made of
one color and no background line. A brush style is a line or a combination
of line patterns that would cover the area painted using a brush. The
Microsoft Windows operating system provides a (limited) list of styles you can use for
your brush to cover a painted area. These styles are:
The Visual Component Library (VCL) provides an
enumerator that holds 8 constant for brush styles. This enumerator is
TBrushStyle and is defined as follows:
enum TBrushStyle {bsSolid, bsClear, bsHorizontal, bsVertical, bsFDiagonal, bsBDiagonal, bsCross, bsDiagCross};
To select a style for your brush, assign the desired
value to the Brush member variable of your TCanvas object.
Brush styles are typically used to fill an area for a
particular pattern. One way you can use them is to use a constant brush
for a simple drawing. On the other hand, if you are creating a drawing
application, you may want users to select the brush style they want. This
can be provided through a list such as a combo box:
|
This form with a bsDialog BorderStyle contains a combo
box whose Style is csOwnerDrawFixed, a ColorGrid under the combo box,
and a Shape control. |
You can start by creating an array that would hold the
list of styles as follows: |
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
const AnsiString strStyle[] = { "bsSolid", "bsClear", "bsHorizontal",
"bsVertical", "bsFDiagonal", "bsBDiagonal",
"bsCross", "bsDiagCross" };
const TBrushStyle brStyle[] = { bsSolid, bsClear, bsHorizontal,
bsVertical, bsFDiagonal, bsBDiagonal,
bsCross, bsDiagCross };
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TShape *Shape1;
TComboBox *ComboBox1;
TCColorGrid *CColorGrid1;
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
|
The first thing you should do here is to initialize
the control that would hold the list of styles. This can be done in the
OnCreate event of the Form: |
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
for(int i = 0; i < 8; ++i)
ComboBox1->Items->Add(strStyle[i]);
}
//---------------------------------------------------------------------------
|
Since you would usually use an owner draw control,
such as a combo box, a list box, a list view, a tree view, grid list, a
menu, etc, you should use the related event to process the actual drawing.
For our example, we can use the OnDrawItem event as follows: |
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBox1DrawItem(TWinControl *Control, int Index,
TRect &Rect, TOwnerDrawState State)
{
if( State.Contains(odSelected) )
ComboBox1->Canvas->Brush->Color = clHighlight;
else
ComboBox1->Canvas->Brush->Color = clWhite;
ComboBox1->Canvas->FillRect(Rect);
ComboBox1->Canvas->Brush->Color = clBlack;
ComboBox1->Canvas->Brush->Style = brStyle[Index];
ComboBox1->Canvas->Rectangle(Rect.Left + 1, Rect.Top + 1,
Rect.Width(), Rect.Bottom - 1);
}
//---------------------------------------------------------------------------
|
With this event, the control has been appropriately
drawn and is ready to be used. All you need now is to retrieve the value
selected by the user and do whatever you want with it. For a drawing
application, you would usually use the selected style/pattern to fill an
area. Here is an example: |
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBox1Change(TObject *Sender)
{
Shape1->Brush->Color = CColorGrid1->ForegroundColor;
Shape1->Brush->Style = brStyle[ComboBox1->ItemIndex];
}
//---------------------------------------------------------------------------
|
|
|