简体   繁体   English

如何使用Delphi标准确认对话框,但带有复选框“不要再问我”?

[英]How to use Delphi standard confirmation dialog but with checkbox “Don't ask me again”?

In many confirmation dialogs it is usefull to have such option (quick wayt to disable confirmation). 在许多确认对话框中,有这样的选项是有用的(快速方法来禁用确认)。 But i can't find how to do that. 但我无法找到如何做到这一点。 I don't want to design it myself because i need this dialog to be standard-like and don't wont to redesign with every update of Delphi. 我不想自己设计它,因为我需要这个对话框是标准的,不要随着Delphi的每次更新重新设计。 Is there simple way to use Delphi standard confirmation dialog with such checkbox ? 是否有简单的方法使用这种复选框的Delphi标准确认对话框?

UPDATE2. UPDATE2。 Suggested SynTaskDialog library from Synopse project does great job (all i need and even more), i will use it in my projects. Synopse项目推荐的SynTaskDialog库做得很好(我需要的甚至更多),我将在我的项目中使用它。 Thanks! 谢谢!

UPDATE. UPDATE。 So, thank you guys for ideas. 所以,谢谢你们的想法。 System function MessageBoxCheck is nice solution but seem to be not so stable as it should be. 系统函数MessageBoxCheck是一个不错的解决方案,但似乎不是那么稳定。 In general i agree that it is good idea to use latest API functions to provide users with best UI experience of modern os and use old-fashioned design for older systems. 总的来说,我同意最好使用最新的API函数为用户提供现代操作系统的最佳UI体验,并为旧系统使用老式设计。 At moment i stay on simple solution (code is following), but if someone share the code with support of UI for modern OS, it will be nice. 目前我仍然坚持简单的解决方案(代码如下),但如果有人在现代操作系统的UI支持下共享代码,那就太好了。

function MsgDlgWithCB(const Msg,Title,CBMsg: string; DlgType: TMsgDlgType;
  Buttons: TMsgDlgButtons; DefaultButton: TMsgDlgBtn;
  var cbDontAskAnymore: TCheckBox): TForm;
var
  i: integer;
  b: TButton;
  y: integer;
begin
  Result := CreateMessageDialog(Msg, DlgType, Buttons, DefaultButton) ;
  Result.Position := poScreenCenter;
  cbDontAskAnymore := TCheckBox.Create(Result);
  cbDontAskAnymore.Caption := CBMsg;
  cbDontAskAnymore.Width := 130;
  y := -1;
  for i := 0 to result.ComponentCount-1 do
    if result.Components[i] is TButton then
    begin
      b := TButton(result.Components[i]);
      b.Left := b.Left + cbDontAskAnymore.Width + 16;
      Result.ClientWidth := Max(Result.ClientWidth, b.Left+b.Width+16);
      y := b.Top+b.Height-cbDontAskAnymore.Height;
    end;
  if y<0 then
    y := Result.ClientHeight - cbDontAskAnymore.height - 16;
  Result.Caption := Title;
  cbDontAskAnymore.Parent := Result;
  cbDontAskAnymore.Top := y;
  cbDontAskAnymore.Left := 8;
end;

function MessageDlgCheckbox(const Msg: string; DlgType: TMsgDlgType;
  Buttons: TMsgDlgButtons; DefaultButton: TMsgDlgBtn;
  var cbDontAskAnymore: Boolean;
  const Title: string ='Confirmation';
  const CBMsg: string = 'Don''t ask anymore'): integer;
var
  f: TForm;
  c: TCheckbox;
begin
  f := MsgDlgWithCB(Msg,Title,CBMsg,DlgType,Buttons,DefaultButton,c);
  try
    result := f.ShowModal;
    cbDontAskAnymore := c.Checked;
  finally
    f.free;
  end;
end;

You can use our Open Source SynTaskDialog unit . 您可以使用我们的开源SynTaskDialog单元

Windows provides a generic task dialog available since Vista/Seven. Windows提供自Vista / Seven以来可用的通用任务对话框。 But there is none available with previous versions of Windows, ie Windows XP or 2K. 但是以前版本的Windows(即Windows XP或2K)都没有。

This unit (licensed under a MPL/GPL/LGPL tri-license) will use the new TaskDialog API under Vista/Seven, and emulate it with pure Delphi code and standard themed VCL components under XP or 2K. 该单元(根据MPL / GPL / LGPL三许可证授权)将使用Vista / Seven下的新TaskDialog API,并使用纯Delphi代码和XP或2K下的标准主题VCL组件进行仿真。 It supports Delphi 6 up to XE4, and is Win32/Win64 Unicode ready. 它支持Delphi 6到XE4,并且支持Win32 / Win64 Unicode。

Here is the result under a Windows Seven 64 bit computer: 这是Windows 7 64位计算机下的结果:

在此输入图像描述

And here is the same dialog created from our emulated pure Delphi code: 这是从我们模拟的纯Delphi代码创建的相同对话框:

在此输入图像描述

Since this screenshot was made on a Win 7 machine, the styling is native for that OS. 由于此屏幕截图是在Win 7计算机上制作的,因此该操作系统的样式是原生的。 When the emulated version of the dialog runs on XP it displays in a style native to that OS. 当对话框的模拟版本在XP上运行时,它将以该操作系统的原生样式显示。

You have your "Do not ask for this setting next time" checkbox... and potentially much more! 你有“下次不要求这个设置”复选框......还有更多!

The system native functionality that offers such facilities is the task dialog API introduced in Vista. 提供此类功能的系统本机功能是Vista中引入的任务对话 API。 This provides means for you to show much more capable dialogs than the older MessageBox API. 这为您提供了比旧的MessageBox API显示功能更强大的对话框的方法。

Should you need to support XP then you will have to create your own dialog. 如果您需要支持XP,则必须创建自己的对话框。 For example by deriving from TForm and calling ShowModal. 例如,从TForm派生并调用ShowModal。 If you do this, make the form capable of building itself dynamically. 如果这样做,请使表单能够动态构建自己。 Don't make one form per message that you show! 不要为每个显示的消息制作一个表单!

In my codebase, I have my own wrapper of the task dialog API. 在我的代码库中,我有自己的任务对话框API包装器。 This detects at runtime versions of Windows that do not support task dialog and falls back on a custom built Delphi dialog. 这会在运行时版本的Windows中检测到不支持任务对话框并返回到自定义构建的Delphi对话框。

Regarding SHMessageBoxCheck I'd be a little wary of taking a dependency on that. 关于SHMessageBoxCheck,我有点担心依赖于它。 According to its documentation it's not supported beyond XP, and you have to import it by ordinal. 根据它的文档,它不支持超出XP,你必须按顺序导入它。 I'd personally be worried that it might be dropped from a future version of Windows. 我个人担心它可能会从未来的Windows版本中删除。 That said, MS has a strong track record of doing whatever it takes to keep legacy apps working with new OS releases. 也就是说,MS在保持遗留应用程序处理新操作系统版本方面做出了一切努力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM