TButtonGroup显示在一个容器中的按钮组。使用TButtonGroup可以创建相关按钮集合。下面来学习使用这个控件,拖动Tool Palette面板的Additional类别下TButtonGroup控件到窗体上,控件截图如下所示:
动态设置此控件属性和添加按钮项代码如下所示:
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 |
procedure TForm1.FormCreate(Sender: TObject); begin with btn1 do begin BorderStyle := bsNone; ButtonHeight := 48; ButtonWidth := 48; ButtonOptions := [gboAllowReorder, //允许用户在运行时可拖动按钮改变顺序 gboFullSize, //设置组中按钮的最大宽度,整个容器的宽度 gboGroupStyle, //按钮应该继承容器组风格 gboShowCaptions]; //在项中的按钮显示Captions标题 Images := il1; //绑定 TImageList ShowHint := True; //可以显示Hint with Items.Add do begin Caption := '按钮一'; Hint := '这是按钮一'; ImageIndex := 0; end; with Items.Add do begin Caption := '按钮二'; Hint := '这是按钮二'; ImageIndex := 1; end; ItemIndex := 0; end; end; |
效果如下图所示:
会不会想要修改按钮显示颜色、显示的效果等等,TButtonGroup并没有开放颜色属性,我们只有重载其DrawButton函数,跟踪其源码,发现可以将其绘制函数复制过来,改为自己想要显示的效果即可,如下代码所示:
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 |
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ButtonGroup, ImgList, CategoryButtons; type TForm1 = class(TForm) btn1: TButtonGroup; il1: TImageList; procedure FormCreate(Sender: TObject); procedure btn1DrawButton(Sender: TObject; Index: Integer; Canvas: TCanvas; Rect: TRect; State: TButtonDrawState); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} uses GraphUtil; procedure TForm1.btn1DrawButton(Sender: TObject; Index: Integer; Canvas: TCanvas; Rect: TRect; State: TButtonDrawState); var TextLeft, TextTop: Integer; RectHeight: Integer; ImgTop: Integer; TextOffset: Integer; ButtonItem: TGrpButtonItem; FillColor: TColor; EdgeColor: TColor; InsertIndication: TRect; TextRect: TRect; OrgRect: TRect; Text: string; begin OrgRect := Rect; //------------------在这里修改了显示颜色--------------------- Canvas.Font := (Sender as TButtonGroup).Font; if bdsSelected in State then begin Canvas.Brush.Color := GetShadowColor(RGB(254,202,115), -25); Canvas.Font.Color := RGB(108,72,2); end else if bdsDown in State then begin Canvas.Brush.Color := RGB(254,202,115); Canvas.Font.Color := RGB(108,72,2); end else begin Canvas.Brush.Color := RGB(196,218,241); Canvas.Font.Color := RGB(43,53,77); end; {if Assigned(FOnBeforeDrawButton) then //在这里可以添加调用OnBeforeDrawButton FOnBeforeDrawButton(Self, Index, Canvas, Rect, State);} FillColor := Canvas.Brush.Color; EdgeColor := GetShadowColor(FillColor, -25); Canvas.FillRect(Rect); InflateRect(Rect, -2, -1); if (bdsHot in State) and not (bdsDown in State) then EdgeColor := GetShadowColor(EdgeColor, -50); { Draw the edge outline } Canvas.Brush.Color := EdgeColor; Canvas.FrameRect(Rect); 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; { Do not localize } if TextTop < Rect.Top then TextTop := Rect.Top; if bdsDown in State then begin Inc(TextTop); Inc(TextLeft); end; ButtonItem := (Sender as TButtonGroup).Items[Index]; { Draw the icon - prefer the event } TextOffset := 0; {if Assigned(FOnDrawIcon) then //在这里可以添加调用OnDrawIcon FOnDrawIcon(Self, Index, Canvas, OrgRect, State, TextOffset) else }if ((Sender as TButtonGroup).Images <> nil) and (ButtonItem.ImageIndex > -1) and (ButtonItem.ImageIndex < (Sender as TButtonGroup).Images.Count) then begin ImgTop := Rect.Top + (RectHeight - (Sender as TButtonGroup).Images.Height) div 2; if ImgTop < Rect.Top then ImgTop := Rect.Top; if bdsDown in State then Inc(ImgTop); (Sender as TButtonGroup).Images.Draw(Canvas, TextLeft - 1, ImgTop, ButtonItem.ImageIndex); TextOffset := (Sender as TButtonGroup).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 gboShowCaptions in (Sender as TButtonGroup).ButtonOptions then begin { Avoid clipping the image } Inc(TextLeft, TextOffset); TextRect.Left := TextLeft; TextRect.Right := Rect.Right - 1; TextRect.Top := TextTop; TextRect.Bottom := Rect.Bottom -1; Text := ButtonItem.Caption; Canvas.TextRect(TextRect, Text, [tfEndEllipsis]); end; if bdsFocused in State then begin { Draw the focus rect } InflateRect(Rect, -2, -2); Canvas.DrawFocusRect(Rect); end; {if Assigned(FOnAfterDrawButton) then //在这里可以添加调用OnAfterDrawButton FOnAfterDrawButton(Self, Index, Canvas, OrgRect, State); } end; |
运行效果如下图所示:
看了DrawButton源码,可以知道,我们也可以添加OnBeforeDrawButton事件,即可快速改变TButtonGroup的显示颜色,代码如下所示:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 |
procedure TForm1.btn1BeforeDrawButton(Sender: TObject; Index: Integer; Canvas: TCanvas; Rect: TRect; State: TButtonDrawState); begin if bdsSelected in State then begin Canvas.Brush.Color := GetShadowColor(RGB(254,202,115), -25); Canvas.Font.Color := RGB(108,72,2); end else if bdsDown in State then begin Canvas.Brush.Color := RGB(254,202,115); Canvas.Font.Color := RGB(108,72,2); end else begin Canvas.Brush.Color := RGB(196,218,241); Canvas.Font.Color := RGB(43,53,77); end; end; |