3.按钮类组件
3.1 Button
基本按钮组件Button是最常用的组件之一,其主要属性:
|
属性 |
说明 |
|
Caption |
按钮显示的文本 |
|
Cancel |
按钮是否为撤销按钮,设置为True,与按ESC键等价 |
|
Default |
按钮是否为默认按钮,设置为True,与按Enter键等价 |
|
ModalResult |
用来决定模式窗体如何关闭,取值: mrNone、mrOK、mrCancel、mrAbort、mrRetry、mrIgnore、mrYes、mrNo、mrAll、mrNoToAll、mrYestToAll。当Button被置于模式窗体上时该属性才有意义。 若属性值不是mrNone,Button被单击后,所在的模式窗体会自动关闭,并且该属性值会作为ShowModal函数的结果返回,所以不需要再为其OnClick事件编写代码。 |
其主要事件:
|
事件 |
说明 |
|
OnClick |
鼠标单击事件,当单击鼠标按钮或者当按钮获得焦点时,按下Enter键或空格键会被触发 |
|
OnMouseDown |
鼠标键按下事件 |
|
OnMouseMove |
鼠标移过事件 |
|
OnMouseUp |
鼠标键释放事件 |
3.2 BitBtn
位图按钮BitBtn组件与Button组件类似,但可以显示一个彩色的位图。其主要属性:
|
属性 |
说明 |
|
Glyph |
指定一个.bmp文件显示在按钮的表面 |
|
Kind |
决定按钮的种类,取值: bkCustom、bkOk、bkCancel、bkHelp、bkYes、bkNo、bkClose、bkAbort、bkIgnore、bkAll。 默认值为bkCustom,表明自定义类型,由Glyph指定位图,其他取值都有自己的位图并将其ModalResult属性自动设置为对应的值。 |
|
Layout |
用来控制BitBtn的位图与文本的相对位置。取值: blGlyphLeft – 位图在文本左侧 blGlyphRight – 位图在文本右侧 blGlyphTop – 位图在文本的上方 blGlyphBottom – 位图在文本的下方 |
|
Margin |
用来控制位图与按钮边界之间的像素,默认值为-1,表明位图位于按钮的正中。 |
|
Spacing |
用来控制位图与文本之间的距离像素,为-1时,文本、位图与边界成等距离;取值为非负整数时,表明位图与文本之间的距离 |
示例:设计一个各种Kind的BitBtn界面,如下图:

Kind属性值为bkClose时,不需要编写按钮事件,按钮自动具备关闭窗体的功能。
3.3 SpeedButton
快速按钮SpeedButton可以成组工作,也可以显示位图,还可以具有单选按钮的功能,单独使用还可以具有复选框的功能。其主要属性如下:
|
属性 |
说明 |
|
AllowAllUp |
控制是否允许单击处于按下状态 |
|
Down |
设置按钮是否处于按下状态 |
|
Flat |
当该属性为True时,按钮具有Office2010工具栏的风格 |
|
GroupIndex |
该属性默认值为0,表明不与其他SpeedButton成组; 取值大于0时,则一样GroupIndex值的SpeedButton将成组工作,类似单选按钮; 取值大于0,同时不与其他SpeedButton的GroupIndex属性值一样,同时AllowAllUp属性设置为True,则SpeedButton类似于复选框工作。 |
示例:使用SpeedButton控制文本的字体及风格
界面设计如下图所示:

组件及其属性:
|
对象 |
属性 |
属性值 |
说明 |
|
From1 |
Caption |
SpeedButton示例 |
窗体标题 |
|
SpeedButton1 |
Caption |
宋体 |
按钮标题 |
|
Down |
1 |
按下状态 |
|
|
GroupIndex |
1 |
组1 |
|
|
SpeedButton2 |
Caption |
黑体 |
按钮标题 |
|
GroupIndex |
1 |
组1 |
|
|
SpeedButton3 |
Caption |
楷体 |
按钮标题 |
|
GroupIndex |
1 |
组1 |
|
|
SpeedButton4 |
Caption |
隶书 |
按钮标题 |
|
GroupIndex |
1 |
组1 |
|
|
SpeedButton5 |
Caption |
仿宋 |
按钮标题 |
|
GroupIndex |
1 |
组1 |
|
|
SpeedButton6 |
Caption |
B |
按钮标题 |
|
AllowAllUp |
True |
允许按键松开 |
|
|
Font.Style.isBold |
True |
粗体 |
|
|
GroupIndex |
2 |
组2 |
|
|
SpeedButton7 |
Caption |
I |
按钮标题 |
|
AllowAllUp |
True |
允许按键松开 |
|
|
Font.Style.isItalic |
True |
斜体 |
|
|
GroupIndex |
3 |
组3 |
|
|
SpeedButton8 |
Caption |
U |
按钮标题 |
|
AllowAllUp |
True |
允许按钮松开 |
|
|
Font.Style.IsUnderline |
True |
下划线 |
|
|
GroupIndex |
4 |
组4 |
|
|
Label1 |
Caption |
您好,Delphi欢迎您! |
标题 |
代码如下:
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
Label1.Font.Name := '宋体';
end;
procedure TForm1.SpeedButton2Click(Sender: TObject);
begin
Label1.Font.Name := '黑体';
end;
procedure TForm1.SpeedButton3Click(Sender: TObject);
begin
Label1.Font.Name := '楷体_GB2312';
end;
procedure TForm1.SpeedButton4Click(Sender: TObject);
begin
Label1.Font.Name := '隶书';
end;
procedure TForm1.SpeedButton5Click(Sender: TObject);
begin
Label1.Font.Name := '仿宋';
end;
procedure TForm1.SpeedButton6Click(Sender: TObject);
begin
if SpeedButton6.Down then
Label1.Font.Style := Label1.Font.Style + [fsBold]
else
Label1.Font.Style := Label1.Font.Style - [fsBold];
end;
procedure TForm1.SpeedButton7Click(Sender: TObject);
begin
if SpeedButton7.Down then
Label1.Font.Style := Label1.Font.Style + [fsItalic]
else
Label1.Font.Style := Label1.Font.Style - [fsItalic];
end;
procedure TForm1.SpeedButton8Click(Sender: TObject);
begin
if SpeedButton8.Down then
Label1.Font.Style := Label1.Font.Style + [fsUnderline]
else
Label1.Font.Style := Label1.Font.Style - [fsUnderline];
end;

