Delphi任务对话框TTaskDialog类介绍


本文整理自网络,侵删。

 
本文档详细介绍了TTaskDialog
查看更多关于 TTaskDialog 的文章
类,这个类封装了Microsoft Windows CTaskDialog类。
CTaskDialog是一个任务对话框,用于替换 windows Vista 或更高版本中的 windows 消息框。 CTaskDialog 改进了原始消息框并添加了功能,详见这里。
本文来源于amingstudio.com。
详细介绍
下边,我们来详细介绍一下。

1、普通对话框

procedure TForm1.Button1Click(Sender: TObject); //第一个对话框
begin
  with TTaskDialog.Create(Self) do
  try
    Caption := '第一个对话框';
    Title := 'Hello World!';
    Text := '这是一个TTaskDialog,这是第一个对话框。' +
            '测试环境,Windows10专业版,Delphi10.3.3';
    CommonButtons := [tcbClose];
    Execute;
  finally
    Free;
  end;
end;
Caption:显示在标题栏上的文本。
Title:对话框的文本的标题。
Text:对话框的文本正文。
CommonButtons:对话框所显示的按钮类型,可以为1个或更多。例如:[tcbYes, tcbNo]
Execute:执行,显示对话框
标准用法
早于 Windows Vista 的 Windows 版本不支持CTaskDialog。 如果你想要向在早期版本的 Windows 上运行你的应用程序的用户显示一条消息,则必须编写备用对话框选项的程序。上边的代码如果用在Windows Xp以下系统,或视觉主题是禁用的,这个应用程序将会崩溃,所以我们需要改进一下。
procedure TForm1.Button2Click(Sender: TObject);//标准对话框
begin
if (Win32MajorVersion >= 6) and ThemeServices.ThemesEnabled then
  with TTaskDialog.Create(Self) do
    try
      Caption := '标准对话框';
      Title := 'Hello World!';
      Text := '这是一个TTaskDialog,这是第一个对话框。' +
              '测试环境,Windows10专业版,Delphi10.3.3';
      CommonButtons := [tcbClose];
      Execute;
    finally
      Free;
    end
else
  MessageBox(Handle,
             '这是一个普通的 MessageBox 对话框,用于支持' +
             'Microsoft Windows 早期版本 (XP 或者更低的版本).',
             '标准对话框',
              MB_ICONINFORMATION or MB_OK);
end;
对话框返回结果类型
CommonButtons属性是TTaskDialog
查看更多关于 TTaskDialog 的文章
CommonButtons类型,定义如下:

TTaskDialogCommonButton = (tcbOk, tcbYes, tcbNo, tcbCancel, tcbRetry, tcbClose);
TTaskDialogCommonButtons = set of TTaskDialogCommonButton;

此属性确定对话框中显示的按钮(如果没有手动添加按钮,我们稍后将执行此操作)。如果用户单击这些按钮中的任何一个,那么一旦Execute返回,相应的TModalResult值就会存储在ModalResult属性中。MainIcon属性确定对话框中显示的图标,当然,它应该反映对话框的性质,按钮集也应该反映对话框的性质。形式上,MainIcon可以设置为tdiNone、tdiWarning、tdiError、tdiInformation和tdiShield的任何值。此外,还可以设置DefaultButton的值,此属性显示为为默认高亮按钮。
procedure TForm1.Button3Click(Sender: TObject);
begin
 with TTaskDialog.Create(Self) do  //两个按钮对话框
  try
    Caption := '两个按钮对话框';
    Title := '请先确认';
    Text := '您真的要这样继续操作吗[...]?';
    CommonButtons := [tcbYes, tcbNo];
    DefaultButton := tcbNo;//默认高亮按钮
    MainIcon := tdiWarning; // tdiNone无图标, tdiWarning警告图标, tdiError错误图标, tdiInformation信息图标, tdiShield盾形图标
    if Execute then
      if ModalResult = mrYes then
        beep;
  finally
    Free;
  end;
end;
自定义按钮
当然,我们亦可以在任务对话框上添加自定义按钮。首先,设置CommonButtons属性为空,然后再添加一个自定义按钮(无限数量的按钮)。下面的的例子显示了这样的一个对话框:
procedure TForm1.Button4Click(Sender: TObject); //自定义按钮对话框
begin
 with TTaskDialog.Create(self) do
  try
    Caption := '自定义按钮对话框';
    Title := '请先确认';
    Text := '确定后将删除所有记录,您确定要这么做吗?';
    CommonButtons := [];
    DefaultButton := tcbNo;
    with TTaskDialogButtonItem(Buttons.Add) do
    begin
      Caption := '删除';
      ModalResult := mrYes;
    end;
    with TTaskDialogButtonItem(Buttons.Add) do
    begin
      Caption := '保留';
      ModalResult := mrNo;
    end;
    MainIcon := tdiNone;
    if Execute then
      if ModalResult = mrYes then
        beep;
  finally
    Free;
  end
end;
命令链接按钮Command Links
任务对话框按钮可以是命令链接按钮command links,而不使用传统的按钮。此项通过设置tfUseCommandLinks属性实现。现在还可以设置CommandLinkHint(每个按钮)属性:
procedure TForm1.Button5Click(Sender: TObject); //命令链接按钮对话框
begin
with TTaskDialog.Create(self) do
  try
    Caption := '命令链接按钮对话框';
    Title := '请先确认';
    Text := '确定后将删除所有记录,您确定要这么做吗?';
    CommonButtons := [];
    DefaultButton := tcbNo;
    with TTaskDialogButtonItem(Buttons.Add) do
    begin
      Caption := '删除';
      CommandLinkHint := '确定删除所选数据.';
      ModalResult := mrYes;
    end;
    with TTaskDialogButtonItem(Buttons.Add) do
    begin
      Caption := '保留';
      CommandLinkHint := '保留所选数据.';
      ModalResult := mrNo;
    end;
    Flags := [tfUseCommandLinks];
    MainIcon := tdiNone;
    if Execute then
      if ModalResult = mrYes then
        beep;
  finally
    Free;
  end
end;

添加内容详情按钮对话框
可以使用ExpandButtonCaption属性添加一个按钮,同时使用ExpandedText属性添加一段文本。当按钮点击时,显示或者隐藏这段文本。
注:如果Flags标签内加入tfExpandFooterArea,文本将在下方显示,否则文本将在上方显示。
procedure TForm1.Button6Click(Sender: TObject);  //内容详情按钮对话框
begin
  with TTaskDialog.Create(self) do
  try
    Caption := '内容详情按钮对话框';
    Title := '请先确认';
    Text :=  '确定后将删除所有记录,您确定要这么做吗?';
    CommonButtons := [];
    DefaultButton := tcbNo;
    with TTaskDialogButtonItem(Buttons.Add) do
    begin
      Caption := '删除';
      CommandLinkHint := '确定删除所选数据.';
      ModalResult := mrYes;
    end;
    with TTaskDialogButtonItem(Buttons.Add) do
    begin
      Caption := '保留';
      CommandLinkHint := '保留所选数据.';
      ModalResult := mrNo;
    end;
    Flags := [tfUseCommandLinks, tfAllowDialogCancellation,tfExpandFooterArea];//去掉tfExpandFooterArea文本将在上方显示
    ExpandButtonCaption := '查看更多';
    ExpandedText := '如果您点击删除按钮,这些信息将从您的电脑上彻底删除.';
    MainIcon := tdiNone;
    if Execute then
      if ModalResult = mrYes then
        beep;
  finally
    Free;
  end
end;
自定义图标对话框
通过使用 tfUseHiconMain标志并指定要在CustomMainIcon属性中使用的TIcon,可以在任务对话框中使用任何自定义图标。
procedure TForm1.Button7Click(Sender: TObject);//自定义图标对话框
begin
with TTaskDialog.Create(self) do
  try
    Caption := '自定义图标对话框';
    Title := 'Hello World!';
    CommonButtons := [tcbClose];
    Text := '这是一个自定义图标对话框';
    Flags := [tfUseHiconMain, tfAllowDialogCancellation];
    CustomMainIcon := Application.Icon;
    Execute;
  finally
    Free;
  end
end;
超级链接
如果只想添加tfEnableHyperlinks标志,你可以使用类似HTML的超链接
procedure TForm1.Button8Click(Sender: TObject);//超级链接对话框
begin
 with TTaskDialog.Create(self) do
  try
    Caption := '超级链接对话框';
    Title := '文件信息';
    CommonButtons := [tcbClose];
    Text := '文件版本: V1.0.0.0'#13#10#13#10'Copyright ? 2020 阿明工作室'#13#10#13#10'<a href="https://www.amingstudio.com">https://www.amingstudio.com</a>';
    Flags := [tfUseHiconMain, tfAllowDialogCancellation, tfEnableHyperlinks];
    CustomMainIcon := Application.Icon;
    Execute;
  finally
    Free;
  end
end;
注意:以上代码单击链接时不会发生任何事情,并不会转到链接的网页,链接的操作必须手动实现。如果在点击链接是想做一些事情,请响应OnHyperlinkClicked事件,它是一个TNotifyEvent。链接的URL(即a元素的href)存储在TTaskDialog的URL公共属性中。

首先,要uses shellapi(如果使用api打开)
在项目的public下部加入 procedure TaskDialogHyperLinkClicked(Sender: TObject);
在TTaskDialog的OnHyperlinkClicked加入:OnHyperlinkClicked := TaskDialogHyperlinkClicked;
procedure TForm1.Button8Click(Sender: TObject);//超级链接对话框
begin
 with TTaskDialog.Create(self) do
  try
    Caption := '超级链接对话框';
    Title := '文件信息';
    CommonButtons := [tcbClose];
    Text := '文件版本: V1.0.0.0'#13#10#13#10'Copyright ? 2020 阿明工作室'#13#10#13#10'<a href="https://www.amingstudio.com">https://www.amingstudio.com</a>';
    Flags := [tfUseHiconMain, tfAllowDialogCancellation, tfEnableHyperlinks];
    OnHyperlinkClicked := TaskDialogHyperlinkClicked;
    CustomMainIcon := Application.Icon;
    Execute;
  finally
    Free;
  end
end;

procedure TForm1.TaskDialogHyperLinkClicked(Sender: TObject);
begin
  if Sender is TTaskDialog then
    with Sender as TTaskDialog do
      ShellExecute(0, 'open', PChar('https://www.amingstudio.com'), nil, nil, SW_SHOWNORMAL);
end;
页脚
可以使用FooterText和FooterIcon 属性创建页脚。icon属性接受与MainIcon属性相同的值。
procedure TForm1.Button9Click(Sender: TObject); //自定义页脚对话框
begin
 with TTaskDialog.Create(self) do
  try
    Caption := '自定义页脚对话框';
    Title := 'Hello World!';
    Text := '这是一个自定义页脚对话框';
    CommonButtons := [tcbYes, tcbNo];
    MainIcon := tdiNone;
    FooterText := '这里将显示页脚信息 ...';
    FooterIcon := tdiWarning;
    Execute;
  finally
    Free;
  end
end;
多选框CheckBox
使用TTaskDialog的VerificationText属性可以在页脚部分添加checkbox。
procedure TForm1.Button10Click(Sender: TObject);  //带CheckBox对话框
begin
with TTaskDialog.Create(self) do
  try
    Caption := '带CheckBox对话框';
    Title := '请先确认';
    Text := '确认后将删除所有数据,确认这样做吗?';
    CommonButtons := [tcbYes, tcbNo];
    MainIcon := tdiNone;
    VerificationText := '不再询问';
    Execute;
  finally
    Free;
  end
end;
单元框Radio Buttons
单选按钮Radio Buttons的实现方式与添加自定义按钮(或命令链接按钮)的方式类似。
 with TTaskDialog.Create(self) do
  try
    Caption := '带RadioButton对话框';
    Title := '请选择';
    Text := '选择一个你最喜欢的老师...';
    CommonButtons := [tcbOk, tcbCancel];
    MainIcon := tdiNone;
    with RadioButtons.Add do
      Caption := '1、苍井空';
    with RadioButtons.Add do
      Caption := '2、小泽玛利亚';
    with RadioButtons.Add do
      Caption := '3、波多野结衣';
    if Execute then
      if ModalResult = mrOk then
        ShowMessage('你选择的是 ' + RadioButton.Caption);
  finally
    Free;
  end
带进度条对话框
同样,可以在Flag标签中加入tfShowProgressBar添加一个进度条,或加入tfShowMarqueeProgressBar添加一个分段块进度条。
实现方法
 1、加入全局变量
Delphi/Pascal

var
tdprogress:TTaskDialog;
FFinished:Boolean;
FCurrNumber, FPrimeNumbersCount:integer;
2、加入常量

Const
MAX_NUMBERS=1000;
NUMBERS_IN_A_SINGLE_STEP=50;
3、在Public下加入事件
Delphi/Pascal

    procedure tdProgressButtonClicked(Sender: TObject;ModalResult:TModalResult;var CanClose:Boolean);
    procedure tdProgressTimer(Sender: TObject;TickCount:Cardinal;var Reset:Boolean);
主要代码:
function IsPrimeNumber(Num: integer): Boolean; //判断是否为素数
var
  i: integer;
begin
  Result := False;
  for i := 2 to Num - 1 do
    if Num mod i = 0 then
    begin
      Result := True;
      Break;
    end;
end;

procedure TForm1.Button12Click(Sender: TObject);//带进度条对话框
begin
  tdProgress:= TTaskDialog.Create(self);
  FFinished:=False;
  FCurrNumber:=1;
  FPrimeNumbersCount:=0;
  tdProgress.ProgressBar.Position :=0;
 with tdProgress do
  try
    Caption := '带进度条对话框';
    Title := '计算素数...';
    Text := '正在计算1000以内的素数';
    CommonButtons := [tcbCancel];
    ExpandButtonCaption := '更多';
    ExpandedText := '素数也叫质数,是指在大于1的自然数中,除了1和它本身以外不再有其他因数的自然数.';
    Flags := [tfAllowDialogCancellation,tfShowProgressBar,tfCallbackTimer];
    Footericon:=3;
    FooterText:='正在计算素数,请稍后...';
    VerificationText := '不再询问';
    OnButtonClicked:=tdProgressButtonClicked;
    OnTimer:= tdProgressTimer;
    MainIcon := tdiNone;
    Execute;
  finally
    Free;
  end
end;

procedure TForm1.tdProgressButtonClicked(Sender: TObject;ModalResult:TModalResult;var CanClose:Boolean);
begin
  if not FFinished then
  begin
   tdprogress.OnTimer:=nil;
   showmessage('用户已取消计算!');
   CanClose:=True;
  end;

end;

 procedure TForm1.tdProgressTimer(Sender: TObject; TickCount: Cardinal; var Reset: Boolean);
var
 i:integer;
begin
   for I := 1 to NUMBERS_IN_A_SINGLE_STEP do
   begin
     if IsPrimeNumber(FCurrNumber) then
      Inc(FprimeNumbersCount);
     tdprogress.ProgressBar.Position :=  FCurrNumber * 100 div  MAX_NUMBERS;
      Inc(FCurrNumber);
   end;
   FFinished:=FCurrNumber>=MAX_NUMBERS;
   if FFinished then
   begin
     tdprogress.OnTimer:=nil;
     tdprogress.ProgressBar.Position :=100;
     showmessage(MAX_NUMBERS.ToString + ' 以内的素数之和为 ' + FprimeNumbersCount.ToString);
   end;
end;

来源:https://www.amingstudio.com/delphi/676.html

相关阅读 >>

Delphi adoconnection1 连接excel表格读取数据

Delphi动态复选框源码

Delphi控制摄像头

Delphi 时间年月日,星期

Delphi程序只允许运行一个实例的三种方法

Delphi 10分钟10行代码开发app(Delphi 应用案例)

Delphi 写的经常用到的加解密函数

Delphi sql 语句(常见) 新建,删除,修改表结构

Delphi控件adsl拨号和断开

xp/2003下Delphi最简单的方法开放3389

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...