【3.Delphi常用组件】3.按钮类组件

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控制文本的字体及风格

界面设计如下图所示:

 

组件及其属性:

对象属性属性值说明
From1CaptionSpeedButton示例窗体标题
SpeedButton1Caption宋体按钮标题
Down1按下状态
GroupIndex1组1
SpeedButton2Caption黑体按钮标题
GroupIndex1组1
SpeedButton3Caption楷体按钮标题
GroupIndex1组1
SpeedButton4Caption隶书按钮标题
GroupIndex1组1
SpeedButton5Caption仿宋按钮标题
GroupIndex1组1
SpeedButton6CaptionB按钮标题
AllowAllUpTrue允许按键松开
Font.Style.isBoldTrue粗体
GroupIndex2组2
SpeedButton7CaptionI按钮标题
AllowAllUpTrue允许按键松开
Font.Style.isItalicTrue斜体
GroupIndex3组3
SpeedButton8CaptionU按钮标题
AllowAllUpTrue允许按钮松开
Font.Style.IsUnderlineTrue下划线
GroupIndex4组4
Label1Caption您好,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;
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Janeb1018

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值