MessageDlg 不发出声音

MessageDlg does not make sound

我有以下代码:

IF MessageDlg('Delete?',mtConfirmation,[mbYes,mbNo],0) = MrYes THEN
Begin
///Do Something;
End
Else
Begin
///Do Something;
End;

StyleWindowsMessageDlg 函数播放声音,但是如果我将 Style 更改为 Windows 10 例如,那么声音不工作。

注意:我正在处理 Delphi 10 Seattle

更新:

我按照 David Heffernan 在他的回答中建议的那样尝试 MessageBeep(MB_ICONQUESTION);,但也没有发出声音。

当您使用 Windows 样式时,消息对话框由 Windows 消息对话框函数之一实现。这些将发出与对话类型匹配的标准系统声音。

当您使用 VCL 样式时,VCL 代码负责对话框。它选择不发出系统声音。这只是使用 VCL 样式实现不精确的众多细节中的另一个。如果您想在使用 VCL 样式时复制标准行为,则需要添加对 MessageBeep.

的适当调用

为了补充 David 的回答,根据您的 Windows 版本、当前活动样式和其他样式检查 MessageDlg函数是使用自定义 TForm 或使用 TTaskDialog class 实现的(这是 Windows Task Dialog). So as workaround you can use the TTaskDialog class directly and add the Vcl.Styles.Hooks 单元的包装器你的项目来设置那种对话框的样式。

uses
  Vcl.Styles.Hooks;

procedure TForm56.Button1Click(Sender: TObject);
var
 LTaskDialog : TTaskDialog;
begin
  LTaskDialog := TTaskDialog.Create(Self);
  try
    LTaskDialog.Caption := 'Confirm';
    LTaskDialog.Text := 'Delete ?';
    LTaskDialog.CommonButtons := [tcbYes, tcbNo];
    LTaskDialog.MainIcon := tdiInformation;
    if LTaskDialog.Execute then
      if LTaskDialog.ModalResult = mrYes then
      begin


      end;
  finally
    LTaskDialog.Free;
  end;