您的位置:首页 > 编程语言 > Delphi

Delphi 2010 TCategoryButtons控件的使用

2011-11-08 17:02 232 查看
TCategoryButtons定义一组分类的按钮,跟TButtonGroup很像,而TCategoryButtons多了分类功能,在表现形式上也比较丰富。下面来学习使用这个控件,拖动Tool Palette面板的Additional类别下TCategoryButtons控件到窗体上,控件截图如下所示:



动态设置此控件属性和添加按钮项代码如下所示:

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60
uses GraphUtil;



procedure TForm1.FormCreate(Sender: TObject);

begin

with btn1 do

begin

BackgroundGradientColor := clNone; //背景渐变填充颜色

BackgroundGradientDirection := gdHorizontal; //背景渐变填充方向

BorderStyle := bsNone; //边框风格,设成无边框

ButtonFlow := cbfVertical; //类别按钮方向

ButtonHeight := 48; //类别里的按钮高

ButtonWidth := 48; //类别里的按钮宽

ButtonOptions := [

boAllowReorder, //允许用户在运行时可拖动按钮改变顺序

// boAllowCopyingButtons, //允许用户在运行时复制按钮

boFullSize, //设置按钮的最大宽度,达到整个容器的宽度

boGradientFill, //Color作为结束色渐变填充

boShowCaptions //开关按钮标题,类别的标题仍然显示

// boVerticalCategoryCaptions, //当一个类别展开后,此类别标题出现在垂直端

// boBoldCaptions, //类别标题加粗

// boUsePlusMinus, //使用+/-标志类别的展开和折叠,否则使用箭头标志

// boCaptionOnlyBorder //当类别标题折叠起来时,在按钮周围画边框

];

Color := clWhite; //背景色

GradientDirection := gdHorizontal; //前景渐变色填充方向

HotButtonColor := RGB(254,202,115); //按钮的热点颜色

Images := il1; //绑定TImageList

RegularButtonColor := RGB(196,218,241); //规则按钮的颜色

SelectedButtonColor := RGB(253,183,64); //选定按钮的颜色

with Categories.Add do

begin

Caption := '类别一';

Color := $00FFEAFF; //类别背景色

GradientColor := $00FFEAFF; //此类别进行渐变填充,跟Color一样则不渐变

TextColor := clWindowText; //类别标题颜色

with Items.Add do

begin

Caption := '按钮一';

ImageIndex := 0;

end;

with Items.Add do

begin

Caption := '按钮二';

ImageIndex := 1;

end;

end;

with Categories.Add do

begin

Caption := '类别二';

Color := clBlue;

GradientColor := clBlue; //此类别进行渐变填充

TextColor := clRed;

with Items.Add do

begin

Caption := '按钮三';

ImageIndex := 2;

end;

end;

end;

end;
效果如下图所示:



TCategoryButtons的类别标题按钮无法改变大小,其绘制函数DrawCategory是私有的,无法重载,只能通过TCategoryButtons的Font字体大小来调整,可这样一来也调整了显示字体,可能会显示的不协调。对于类别下的按钮就比较方便修改显示的效果,添加其OnDrawButton事件,要实现按钮与Category底色一致,不出现边框,实现代码如下:

001

002

003

004

005

006

007

008

009

010

011

012

013

014

015

016

017

018

019

020

021

022

023

024

025

026

027

028

029

030

031

032

033

034

035

036

037

038

039

040

041

042

043

044

045

046

047

048

049

050

051

052

053

054

055

056

057

058

059

060

061

062

063

064

065

066

067

068

069

070

071

072

073

074

075

076

077

078

079

080

081

082

083

084

085

086

087

088

089

090

091

092

093

094

095

096

097

098

099

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164
uses GraphUtil;



procedure TForm1.btn1DrawButton(Sender: TObject; const Button: TButtonItem;

Canvas: TCanvas; Rect: TRect; State: TButtonDrawState);

var

TextLeft, TextTop: Integer;

RectHeight: Integer;

ImgTop: Integer;

TextOffset: Integer;

FillColor: TColor;

EdgeColor: TColor;

InsertIndication: TRect;

TextRect: TRect;

OrgRect: TRect;

Caption: string;

begin

OrgRect := Rect;

{if Assigned(FOnBeforeDrawButton) then

FOnBeforeDrawButton(Self, Button, Canvas, Rect, State); }

InflateRect(Rect, -1, -1);



Canvas.Font.Color := (Sender as TCategoryButtons).Font.Color;

if bdsHot in State then

begin

FillColor := (Sender as TCategoryButtons).HotButtonColor;

if bdsSelected in State then

FillColor := GetShadowColor(FillColor, -10);

EdgeColor := GetShadowColor(FillColor);

end

else if bdsSelected in State then

begin

FillColor := (Sender as TCategoryButtons).SelectedButtonColor;

EdgeColor := GetShadowColor(FillColor);

end

else

begin

FillColor := (Sender as TCategoryButtons).RegularButtonColor;

if (bdsFocused in State) then

EdgeColor := GetShadowColor((Sender as TCategoryButtons).SelectedButtonColor)

else

EdgeColor := GetShadowColor(FillColor);

end;

//添加下面行

FillColor := clNone;



Canvas.Brush.Color := FillColor;

Canvas.Pen.Color := FillColor;

if FillColor <> clNone then

begin

//注释掉填充 Canvas.FillRect(Rect);

{ Draw the edge outline }

Canvas.Brush.Color := EdgeColor;

//注释掉边框 Canvas.FrameRect(Rect);

end;



if bdsFocused in State then

begin

InflateRect(Rect, -1, -1);

//注释掉边框 Canvas.FrameRect(Rect);

end;



Canvas.Brush.Color := FillColor;



{ Compute the text location }

TextLeft := Rect.Left + 4;

RectHeight := Rect.Bottom - Rect.Top;

TextTop := Rect.Top + (RectHeight - Canvas.TextHeight('Wg')) div 2;



if boFullSize in (Sender as TCategoryButtons).ButtonOptions then

Inc(TextLeft, 4); // indent, slightly



if TextTop < Rect.Top then

TextTop := Rect.Top;

if bdsDown in State then

begin

Inc(TextTop);

Inc(TextLeft);

end;



{ Draw the icon - prefer the event }

TextOffset := 0;

{if Assigned(FOnDrawIcon) then

FOnDrawIcon(Self, Button, Canvas, OrgRect, State, TextOffset)

else} if ((Sender as TCategoryButtons).Images <> nil) and (Button.ImageIndex > -1) and

(Button.ImageIndex < (Sender as TCategoryButtons).Images.Count) then

begin

ImgTop := Rect.Top + (RectHeight - (Sender as TCategoryButtons).Images.Height) div 2;

if ImgTop < Rect.Top then

ImgTop := Rect.Top;

if bdsDown in State then

Inc(ImgTop);

(Sender as TCategoryButtons).Images.Draw(Canvas, TextLeft - 1, ImgTop, Button.ImageIndex);

TextOffset := (Sender as TCategoryButtons).Images.Width + 1;

end;



{ Show insert indications }

if [bdsInsertLeft, bdsInsertTop, bdsInsertRight, bdsInsertBottom] * State <> [] then

begin

Canvas.Brush.Color := GetShadowColor(EdgeColor);

InsertIndication := Rect;

if bdsInsertLeft in State then

begin

Dec(InsertIndication.Left, 2);

InsertIndication.Right := InsertIndication.Left + 2;

end

else if bdsInsertTop in State then

begin

Dec(InsertIndication.Top);

InsertIndication.Bottom := InsertIndication.Top + 2;

end

else if bdsInsertRight in State then

begin

Inc(InsertIndication.Right, 2);

InsertIndication.Left := InsertIndication.Right - 2;

end

else if bdsInsertBottom in State then

begin

Inc(InsertIndication.Bottom);

InsertIndication.Top := InsertIndication.Bottom - 2;

end;

Canvas.FillRect(InsertIndication);

Canvas.Brush.Color := FillColor;

end;



if boShowCaptions in (Sender as TCategoryButtons).ButtonOptions then

begin

if FillColor = clNone then

Canvas.Brush.Style := bsClear;



{ Avoid clipping the image }

Inc(TextLeft, TextOffset);

TextRect.Left := TextLeft;

TextRect.Right := Rect.Right - 2;

TextRect.Top := TextTop;

TextRect.Bottom := Rect.Bottom - 2;



{if Assigned(FOnDrawText) then

FOnDrawText(Self, Button, Canvas, TextRect, State)

else }

begin

Caption := Button.Caption;

//添加不同状态下,显示字体的不同颜色

if bdsHot in State then

begin

Canvas.Font.Color := (Sender as TCategoryButtons).HotButtonColor;

if bdsSelected in State then

Canvas.Font.Color := GetShadowColor(FillColor, -10);

end

else if bdsSelected in State then

begin

Canvas.Font.Color := (Sender as TCategoryButtons).SelectedButtonColor;

end

else

begin

Canvas.Font.Color := (Sender as TCategoryButtons).Font.Color;

end;

//添加结束

Canvas.TextRect(TextRect, Caption, [tfEndEllipsis, tfVerticalCenter]);

end;

end;



{if Assigned(FOnAfterDrawButton) then

FOnAfterDrawButton(Self, Button, Canvas, OrgRect, State); }

end;
设置控件的字体为宋体、9号,运行效果如下所示:



扩展资料:

1.VCL Controls: Category Buttons http://www.functionx.com/delphi/controls/categorybuttons.htm
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: