【Delphi】Dialogs.TOptionPane(MessageBox 简单模板封装)

MessageBox 简单模板封装

unit Dialogs.OptionPane;

interface

uses
    Winapi.Windows, Vcl.Dialogs;

const
    TITLE_T = ' 提示消息 ';
    TITLE_W = ' 警告消息 ';
    TITLE_E = ' 错误消息 ';
    TITLE_Q = ' 询问消息 ';

type
    TOptionPane = class(TObject)
    public
        class function ShowTips(text: string; title: string = TITLE_T): Integer; overload;
        class function ShowTips(owner: HWND; text: string; title: string = TITLE_T): Integer; overload;
        class function ShowTipsYesNo(text: string; title: string = TITLE_T): Integer; overload;
        class function ShowTipsYesNo(owner: HWND; text: string; title: string = TITLE_T): Integer; overload;
        class function ShowTipsYesNoCancel(text: string; title: string = TITLE_T): Integer; overload;
        class function ShowTipsYesNoCancel(owner: HWND; text: string; title: string = TITLE_T): Integer; overload;
        class function ShowWarning(text: string; title: string = TITLE_W): Integer; overload;
        class function ShowWarning(owner: HWND; text: string; title: string = TITLE_W): Integer; overload;
        class function ShowWarningYesNo(text: string; title: string = TITLE_W): Integer; overload;
        class function ShowWarningYesNo(owner: HWND; text: string; title: string = TITLE_W): Integer; overload;
        class function ShowWarningYesNoCancel(text: string; title: string = TITLE_W): Integer; overload;
        class function ShowWarningYesNoCancel(owner: HWND; text: string; title: string = TITLE_W): Integer; overload;
        class function ShowError(text: string; title: string = TITLE_E): Integer; overload;
        class function ShowError(owner: HWND; text: string; title: string = TITLE_E): Integer; overload;
        class function ShowErrorYesNo(text: string; title: string = TITLE_E): Integer; overload;
        class function ShowErrorYesNo(owner: HWND; text: string; title: string = TITLE_E): Integer; overload;
        class function ShowErrorYesNoCancel(text: string; title: string = TITLE_E): Integer; overload;
        class function ShowErrorYesNoCancel(owner: HWND; text: string; title: string = TITLE_E): Integer; overload;
        class function ShowConfirm(text: string; title: string = TITLE_Q): Boolean; overload;
        class function ShowConfirm(owner: HWND; text: string; title: string = TITLE_Q): Boolean; overload;
        class function ShowConfirmYesNo(text: string; title: string = TITLE_Q): Integer; overload;
        class function ShowConfirmYesNo(owner: HWND; text: string; title: string = TITLE_Q): Integer; overload;
        class function ShowConfirmYesNoCancel(text: string; title: string = TITLE_Q): Integer; overload;
        class function ShowConfirmYesNoCancel(owner: HWND; text: string; title: string = TITLE_Q): Integer; overload;
    end;

implementation

class function TOptionPane.ShowTips(text: string; title: string = TITLE_T): Integer;
begin
    Result := ShowTips(GetActiveWindow, text, title);
end;

class function TOptionPane.ShowTips(owner: HWND; text: string; title: string = TITLE_T): Integer;
begin
    Result := MessageBox(owner, PChar(text), PChar(title), MB_OK + MB_ICONINFORMATION);
    // Result := TaskMessageDlg(title, text, mtInformation, [mbOK], 0);
end;

class function TOptionPane.ShowTipsYesNo(text: string; title: string = TITLE_T): Integer;
begin
    Result := ShowTipsYesNo(GetActiveWindow, text, title);
end;

class function TOptionPane.ShowTipsYesNo(owner: HWND; text: string; title: string = TITLE_T): Integer;
begin
    Result := MessageBox(owner, PChar(text), PChar(title), MB_YESNO + MB_ICONINFORMATION);
    // Result := TaskMessageDlg(title, text, mtInformation, [mbYes, mbNo], 0);
end;

class function TOptionPane.ShowTipsYesNoCancel(text: string; title: string = TITLE_T): Integer;
begin
    Result := ShowTipsYesNoCancel(GetActiveWindow, text, title);
end;

class function TOptionPane.ShowTipsYesNoCancel(owner: HWND; text: string; title: string = TITLE_T): Integer;
begin
    Result := MessageBox(owner, PChar(text), PChar(title), MB_YESNOCANCEL + MB_ICONINFORMATION);
    // Result := TaskMessageDlg(title, text, mtInformation, [mbYes, mbNo, mbCancel], 0);
end;

////////////////////////////////////////////////////////////////////////////////

class function TOptionPane.ShowWarning(text: string; title: string = TITLE_W): Integer;
begin
    Result := ShowWarning(GetActiveWindow, text, title);
end;

class function TOptionPane.ShowWarning(owner: HWND; text: string; title: string = TITLE_W): Integer;
begin
    Result := MessageBox(owner, PChar(text), PChar(title), MB_OK + MB_ICONWARNING);
    // Result := TaskMessageDlg(title, text, mtWarning, [mbOK], 0);
end;

class function TOptionPane.ShowWarningYesNo(text: string; title: string = TITLE_W): Integer;
begin
    Result := ShowWarningYesNo(GetActiveWindow, text, title);
end;

class function TOptionPane.ShowWarningYesNo(owner: HWND; text: string; title: string = TITLE_W): Integer;
begin
    Result := MessageBox(owner, PChar(text), PChar(title), MB_YESNO + MB_ICONWARNING);
    // Result := TaskMessageDlg(title, text, mtWarning, [mbYes, mbNo], 0);
end;

class function TOptionPane.ShowWarningYesNoCancel(text: string; title: string = TITLE_W): Integer;
begin
    Result := ShowWarningYesNoCancel(GetActiveWindow, text, title);
end;

class function TOptionPane.ShowWarningYesNoCancel(owner: HWND; text: string; title: string = TITLE_W): Integer;
begin
    Result := MessageBox(owner, PChar(text), PChar(title), MB_YESNOCANCEL + MB_ICONWARNING);
    // Result := TaskMessageDlg(title, text, mtWarning, [mbYes, mbNo, mbCancel], 0);
end;

////////////////////////////////////////////////////////////////////////////////

class function TOptionPane.ShowError(text: string; title: string = TITLE_E): Integer;
begin
    Result := ShowError(GetActiveWindow, text, title);
end;

class function TOptionPane.ShowError(owner: HWND; text: string; title: string = TITLE_E): Integer;
begin
    Result := MessageBox(owner, PChar(text), PChar(title), MB_OK + MB_ICONERROR);
    // Result := TaskMessageDlg(title, text, mtError, [mbOK], 0);
end;

class function TOptionPane.ShowErrorYesNo(text: string; title: string = TITLE_E): Integer;
begin
    Result := ShowErrorYesNo(GetActiveWindow, text, title);
end;

class function TOptionPane.ShowErrorYesNo(owner: HWND; text: string; title: string = TITLE_E): Integer;
begin
    Result := MessageBox(owner, PChar(text), PChar(title), MB_YESNO + MB_ICONERROR);
    // Result := TaskMessageDlg(title, text, mtError, [mbYes, mbNo], 0);
end;

class function TOptionPane.ShowErrorYesNoCancel(text: string; title: string = TITLE_E): Integer;
begin
    Result := ShowErrorYesNoCancel(GetActiveWindow, text, title);
end;

class function TOptionPane.ShowErrorYesNoCancel(owner: HWND; text: string; title: string = TITLE_E): Integer;
begin
    Result := MessageBox(owner, PChar(text), PChar(title), MB_YESNOCANCEL + MB_ICONERROR);
    // Result := TaskMessageDlg(title, text, mtError, [mbYes, mbNo, mbCancel], 0);
end;

////////////////////////////////////////////////////////////////////////////////

class function TOptionPane.ShowConfirm(text: string; title: string = TITLE_Q): Boolean;
begin
    Result := ShowConfirm(GetActiveWindow, text, title);
end;

class function TOptionPane.ShowConfirm(owner: HWND; text: string; title: string = TITLE_Q): Boolean;
begin
    Result := idYes = MessageBox(owner, PChar(text), PChar(title), MB_YESNO + MB_ICONQUESTION);
    // Result := idYes = TaskMessageDlg(title, text, mtConfirmation, [mbYes, mbNo], 0);
end;

class function TOptionPane.ShowConfirmYesNo(text: string; title: string = TITLE_Q): Integer;
begin
    Result := ShowConfirmYesNo(GetActiveWindow, text, title);
end;

class function TOptionPane.ShowConfirmYesNo(owner: HWND; text: string; title: string = TITLE_Q): Integer;
begin
    Result := MessageBox(owner, PChar(text), PChar(title), MB_YESNO + MB_ICONQUESTION);
    // Result := TaskMessageDlg(title, text, mtConfirmation, [mbYes, mbNo], 0);
end;

class function TOptionPane.ShowConfirmYesNoCancel(text: string; title: string = TITLE_Q): Integer;
begin
    Result := ShowConfirmYesNoCancel(GetActiveWindow, text, title);
end;

class function TOptionPane.ShowConfirmYesNoCancel(owner: HWND; text: string; title: string = TITLE_Q): Integer;
begin
    Result := MessageBox(owner, PChar(text), PChar(title), MB_YESNOCANCEL + MB_ICONQUESTION);
    // Result := TaskMessageDlg(title, text, mtConfirmation, [mbYes, mbNo, mbCancel], 0);
end;

////////////////////////////////////////////////////////////////////////////////

end.

原文地址:https://www.cnblogs.com/zhuzhongxing/p/14147084.html