更改消息DLG语言delphi

更改消息DLG语言delphi,delphi,Delphi,当我使用MessageDlg()函数时,如下代码所示: if MessageDlg(SWarningWishToDelete + ' ' + PersonName + '?', mtWarning, [mbNo, mbYes], 0) = mrYes then 是/否按钮以英语显示,而不是我的操作系统语言。我在互联网上搜索了一下,然而,我只是找到了一些大而旧的解决方案。我想知道是否有一种简单/新的方法来更改它?您可以使用作为 procedure TForm1.Button1Click(Se

当我使用
MessageDlg()
函数时,如下代码所示:

if MessageDlg(SWarningWishToDelete + ' ' + PersonName + '?',
  mtWarning, [mbNo, mbYes], 0) = mrYes then
是/否按钮以英语显示,而不是我的操作系统语言。我在互联网上搜索了一下,然而,我只是找到了一些大而旧的解决方案。我想知道是否有一种简单/新的方法来更改它?

您可以使用作为

procedure TForm1.Button1Click(Sender: TObject);
begin
  with CreateMessageDialog('Msg', mtConfirmation, [mbYes, mbNo], mbNo) do
    begin
      try
        TButton(FindComponent('Yes')).Caption:= 'نعم';
        TButton(FindComponent('No')).Caption:= 'لا';
        ShowModal;
      finally
        case ModalResult of
          mrYes: ShowMessage('You click نعم');
          mrNo: ShowMessage('You click لا');
          mrCancel: ShowMessage('Dialog cancled!');
        end;
        Free;
      end;
    end;
end;
procedure TForm1.Button2Click(Sender: TObject);
Var
  Dlg: TTaskDialog;
begin
  Dlg := TTaskDialog.Create(Self);
  try
    with Dlg do
      begin
        CommonButtons:= [];
        Caption:= 'Caption here';
        Text:= 'Text here';
        with Buttons.Add do
          begin
            Caption:= 'نعم';
            ModalResult:= mrYes;
          end;
        with Buttons.Add do
          begin
            Caption:= 'لا';
            ModalResult:= mrNo;
          end;
        if Execute then
          case ModalResult of
            mrYes: ShowMessage('You click نعم');
            mrNo: ShowMessage('You click لا');
            mrCancel: ShowMessage('Dialog cancled!');
          end;
      end;
  finally
    Dlg.Free;
  end;
end;
使用作为

procedure TForm1.Button1Click(Sender: TObject);
begin
  with CreateMessageDialog('Msg', mtConfirmation, [mbYes, mbNo], mbNo) do
    begin
      try
        TButton(FindComponent('Yes')).Caption:= 'نعم';
        TButton(FindComponent('No')).Caption:= 'لا';
        ShowModal;
      finally
        case ModalResult of
          mrYes: ShowMessage('You click نعم');
          mrNo: ShowMessage('You click لا');
          mrCancel: ShowMessage('Dialog cancled!');
        end;
        Free;
      end;
    end;
end;
procedure TForm1.Button2Click(Sender: TObject);
Var
  Dlg: TTaskDialog;
begin
  Dlg := TTaskDialog.Create(Self);
  try
    with Dlg do
      begin
        CommonButtons:= [];
        Caption:= 'Caption here';
        Text:= 'Text here';
        with Buttons.Add do
          begin
            Caption:= 'نعم';
            ModalResult:= mrYes;
          end;
        with Buttons.Add do
          begin
            Caption:= 'لا';
            ModalResult:= mrNo;
          end;
        if Execute then
          case ModalResult of
            mrYes: ShowMessage('You click نعم');
            mrNo: ShowMessage('You click لا');
            mrCancel: ShowMessage('Dialog cancled!');
          end;
      end;
  finally
    Dlg.Free;
  end;
end;
注意:
TTaskDialog
是特定于平台的,需要Vista、Windows 7或更高版本的Windows操作系统

为了好玩,您甚至可以创建自己的函数:

function TForm1.MyDialog(const DlgCaption, DlgText: String; DlgButtons: array of string;
  DlgType: TMsgDlgType; Buttons: TMsgDlgButtons): TForm;
Var
  I: Integer;
  F: TForm;
begin
  F:= CreateMessageDialog(Text, DlgType, Buttons);
  F.Caption:= DlgCaption;
  TLabel(F.Components[1]).Caption:= DlgText;
  for I := Low(DlgButtons) to High(DlgButtons) do
    begin
      TButton(F.Components[I+2]).Caption:= DlgButtons[I];
    end;
  Result:= F;
end;
并将其测试为

MyDialog('Caption', 'Text', ['Yes do', 'No don''t'], mtConfirmation, [mbYes, mbNo]).ShowModal;

下面是该函数的另一个版本

function MyDialog(Const DlgCaption, DlgText: string; DlgButtons: array of string;
             DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; 
             BidiMode: TBiDiMode = bdLeftToRight;
             DefultButton: TMsgDlgBtn = mbOK):TForm;
function TForm1.MyDialog(const DlgCaption, DlgText: string;
  DlgButtons: array of string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons;
  BidiMode: TBiDiMode; DefultButton: TMsgDlgBtn): TForm;
Var
  I, CntBtns: Integer;
  F: TForm;
  Btn: TMsgDlgBtn;
begin
  // Check for strings
  CntBtns:= 0;
  for Btn in Buttons do
  begin
    Inc(CntBtns);
  end;
  if Length(DlgButtons) <> CntBtns then
    raise Exception.Create('DlgButtons and Buttons params should have the same count');
  //Get the Form created by CreateMessageDialog function
  F:= CreateMessageDialog(DlgText, DlgType, Buttons, DefultButton);
  // Set the Caption of th form
  F.Caption:= DlgCaption;
  // Change the buttons Captions
  for I := Low(DlgButtons) to High(DlgButtons) do
    TButton(F.Components[I+2]).Caption:= DlgButtons[I];
  // Check for bidimode
  if BidiMode = bdRightToLeft then
    begin
      TImage(F.Components[0]).Left:= (F.Width - (TImage(F.Components[0]).Left + TImage(F.Components[0]).Width));
      TLabel(F.Components[1]).Left:= (TImage(F.Components[0]).Left - (TLabel(F.Components[0]).Width + 10));
    end;
  // Return the form
  Result:= F;
end;
函数的代码

function MyDialog(Const DlgCaption, DlgText: string; DlgButtons: array of string;
             DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; 
             BidiMode: TBiDiMode = bdLeftToRight;
             DefultButton: TMsgDlgBtn = mbOK):TForm;
function TForm1.MyDialog(const DlgCaption, DlgText: string;
  DlgButtons: array of string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons;
  BidiMode: TBiDiMode; DefultButton: TMsgDlgBtn): TForm;
Var
  I, CntBtns: Integer;
  F: TForm;
  Btn: TMsgDlgBtn;
begin
  // Check for strings
  CntBtns:= 0;
  for Btn in Buttons do
  begin
    Inc(CntBtns);
  end;
  if Length(DlgButtons) <> CntBtns then
    raise Exception.Create('DlgButtons and Buttons params should have the same count');
  //Get the Form created by CreateMessageDialog function
  F:= CreateMessageDialog(DlgText, DlgType, Buttons, DefultButton);
  // Set the Caption of th form
  F.Caption:= DlgCaption;
  // Change the buttons Captions
  for I := Low(DlgButtons) to High(DlgButtons) do
    TButton(F.Components[I+2]).Caption:= DlgButtons[I];
  // Check for bidimode
  if BidiMode = bdRightToLeft then
    begin
      TImage(F.Components[0]).Left:= (F.Width - (TImage(F.Components[0]).Left + TImage(F.Components[0]).Width));
      TLabel(F.Components[1]).Left:= (TImage(F.Components[0]).Left - (TLabel(F.Components[0]).Width + 10));
    end;
  // Return the form
  Result:= F;
end;

享受。

你不能改用
TTaskDialog
吗?或者,
CreateMessageDialog
查看按钮标题是名为
SMsgDlgYes
的资源字符串,依此类推。搜索“HookResourceString SMsgDlgYes”查看如何翻译它们。感谢您的帮助。我只想提一下,在上一个例子
MyDialog
中,我为文本添加了另一个参数,并保留了一个标题。@SoonSantos是的,我也忘了添加它