开发者

MessageDlg shows information icon instead of confirmation

On Win 7, MessageDlg shows information icon instead of confirmation icon (question mark). Here is 开发者_运维知识库the code:

MessageDlg('Are you sure you want to delete this file?'+ CRLF+ FileName, mtConfirmation, [mbYes, mbNo], 0)= mrYes

What am I doing wrong?


First, notice that a simple workaround is to use the Windows API MessageBox function instead:

MessageBox(Handle, 'This is a test.', 'Test', MB_ICONQUESTION or MB_YESNO)

MessageDlg shows information icon instead of confirmation

But why doesn't MessageDlg work? Well, MessageDlg does one of two possible things. It uses the Windows Vista+ Task Dialog, if possible, that is, it the OS is Windows Vista or later and themes are enabled and the UseLatestCommonDialogs global variable is true (the default). If not, the VCL actually creates a custom TForm and adds all buttons, labels, and icons manually (which is a bit odd if you ask me -- why not simply use MessageBox?).

The last approach supports the question-mark icon. Indeed, try

UseLatestCommonDialogs := false;
MessageDlg('This is a test', mtConfirmation, [mbYes, mbNo], 0);

MessageDlg shows information icon instead of confirmation

But this looks so ugly! Please don't do this! It is stupid to create a custom message dialog instead of using the native OS dialogs!

Unfortunately, however, the Task Dialog does not support the question-mark icon (as a pre-defined icon). This is not a restriction of the TTaskDialog wrapper, but a limitation of the Windows Task Dialog API. See the official documentation, for instance. There are TD_ERROR_ICON, TD_WARNING_ICON, and TD_INFORMATION_ICON, but no question-mark icon.

Of course, the Task Dialog can use any icon. For instance, you can do

if (Win32MajorVersion >= 6) and ThemeServices.ThemesEnabled then
    with TTaskDialog.Create(Self) do
      try
        Caption := 'Test';
        Title := 'Test';
        Text := 'This is a test.';
        CommonButtons := [tcbYes, tcbNo];
        CustomMainIcon.ReleaseHandle;
        CustomMainIcon.Handle := LoadIcon(0, IDI_QUESTION);
        Flags := [tfUseHiconMain];
        Execute;
      finally
        Free;
      end
  else
    MessageBox(Handle,
               'This is a test.',
               'Test',
               MB_ICONQUESTION or MB_YESNO);

Notice that I fall back on the old MessageBox should the Task Dialog API not be available.

MessageDlg shows information icon instead of confirmation

In principle, the MessageDlg function could use additional logic to handle the question-mark case separately, in a way that differs from the information, warning, and error cases. This would have made the mtConfirmation give the right icon even with the Task Dialog, at the expence of slightly more complicated, and less elegant, VCL code. Apparently, however, Embarcadero chose the simpler alternative of simply pretending that you asked for the information icon.

Still, I wonder why Microsoft chose not to include a TD_QUESTION_ICON constant in the API. Perhaps this icon is discouraged in their latest UI guidelines?


If you need some Dialogs, you may take a look at our Open Source SynTaskDialog unit for Windows XP, Vista, Seven.

It will call the Native Task Dialog under Vista and Seven, and emulate it under XP. You'll have better interaction than with the standard Dialogs unit or Windows API, and will have your confirmation icon, just as expected.

Here is the result under a Windows Seven 64 bit computer:

MessageDlg shows information icon instead of confirmation

And here is the same dialog created from our Emulated pure Delphi code:

MessageDlg shows information icon instead of confirmation

Here is a selection task dialog, with "Command links" buttons, in native Seven:

MessageDlg shows information icon instead of confirmation

And here is the window as created with the VCL emulation code:

MessageDlg shows information icon instead of confirmation

Note that there is the confirmation icon. ;)

Working from Delphi 6 up to XE.


if (Win32MajorVersion >= 6) and ThemeServices.ThemesEnabled then
    with TTaskDialog.Create(Self) do
      try
        Caption := 'Delete';
        Title := 'Are you sure to remove the minecraft?';
        Text := 'if you delete minecraft it well be gone forever.';
        CommonButtons := [tcbYes, tcbNo];
        CustomMainIcon.ReleaseHandle;
        CustomMainIcon.Handle := LoadIcon(0, IDI_QUESTION);
        Flags := [tfUseHiconMain];
        Execute;
      finally
        Free;
      end
  else
    MessageBox(Handle,
               'if you delete minecraft it well be gone forever.',
               'Are you sure to remove the minecraft?',
               MB_ICONQUESTION or MB_YESNO);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜