📄 tntdialogs.pas
字号:
var
P: PWideChar;
begin
Separator := #0;
if (ofAllowMultiSelect in Options) and
((ofOldStyleDialog in Options) or not NewStyleControls) then
Separator := ' ';
with OpenFileName do
begin
if ofAllowMultiSelect in Options then
begin
ExtractFileNamesW(lpstrFile);
FileName := FFiles[0];
end else
begin
P := lpstrFile;
FileName := ExtractStringFromStringArray(P, Separator);
FFiles.Add(FileName);
end;
end;
// Sync inherited Files
inherited Files.Assign(FFiles);
end;
function TTntOpenDialog.Execute: Boolean;
begin
if (not Win32PlatformIsUnicode) then
Result := DoExecute(@GetOpenFileNameA)
else
Result := DoExecuteW(@GetOpenFileNameW);
end;
{ TTntSaveDialog }
function TTntSaveDialog.Execute: Boolean;
begin
if (not Win32PlatformIsUnicode) then
Result := DoExecute(@GetSaveFileNameA)
else
Result := DoExecuteW(@GetSaveFileNameW);
end;
{ Message dialog }
function GetAveCharSize(Canvas: TCanvas): TPoint;
var
I: Integer;
Buffer: array[0..51] of WideChar;
begin
for I := 0 to 25 do Buffer[I] := WideChar(I + Ord('A'));
for I := 0 to 25 do Buffer[I + 26] := WideChar(I + Ord('a'));
GetTextExtentPointW(Canvas.Handle, Buffer, 52, TSize(Result));
Result.X := Result.X div 52;
end;
type
TTntMessageForm = class(TTntForm)
private
Message: TTntLabel;
procedure HelpButtonClick(Sender: TObject);
protected
procedure CustomKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
function GetFormText: WideString;
public
constructor CreateNew(AOwner: TComponent); reintroduce;
end;
constructor TTntMessageForm.CreateNew(AOwner: TComponent);
var
NonClientMetrics: TNonClientMetrics;
begin
inherited CreateNew(AOwner);
NonClientMetrics.cbSize := sizeof(NonClientMetrics);
if SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, @NonClientMetrics, 0) then
Font.Handle := CreateFontIndirect(NonClientMetrics.lfMessageFont);
end;
procedure TTntMessageForm.HelpButtonClick(Sender: TObject);
begin
Application.HelpContext(HelpContext);
end;
procedure TTntMessageForm.CustomKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if (Shift = [ssCtrl]) and (Key = Word('C')) then
begin
Beep;
TntClipboard.AsWideText := GetFormText;
end;
end;
function TTntMessageForm.GetFormText: WideString;
var
DividerLine, ButtonCaptions: WideString;
I: integer;
begin
DividerLine := StringOfChar('-', 27) + sLineBreak;
for I := 0 to ComponentCount - 1 do
if Components[I] is TTntButton then
ButtonCaptions := ButtonCaptions + TTntButton(Components[I]).Caption +
StringOfChar(' ', 3);
ButtonCaptions := Tnt_WideStringReplace(ButtonCaptions,'&','', [rfReplaceAll]);
Result := DividerLine + Caption + sLineBreak + DividerLine + Message.Caption + sLineBreak
+ DividerLine + ButtonCaptions + sLineBreak + DividerLine;
end;
function GetMessageCaption(MsgType: TMsgDlgType): WideString;
begin
case MsgType of
mtWarning: Result := SMsgDlgWarning;
mtError: Result := SMsgDlgError;
mtInformation: Result := SMsgDlgInformation;
mtConfirmation: Result := SMsgDlgConfirm;
mtCustom: Result := '';
else
raise ETntInternalError.Create('Unexpected MsgType in GetMessageCaption.');
end;
end;
function GetButtonCaption(MsgDlgBtn: TMsgDlgBtn): WideString;
begin
case MsgDlgBtn of
mbYes: Result := SMsgDlgYes;
mbNo: Result := SMsgDlgNo;
mbOK: Result := SMsgDlgOK;
mbCancel: Result := SMsgDlgCancel;
mbAbort: Result := SMsgDlgAbort;
mbRetry: Result := SMsgDlgRetry;
mbIgnore: Result := SMsgDlgIgnore;
mbAll: Result := SMsgDlgAll;
mbNoToAll: Result := SMsgDlgNoToAll;
mbYesToAll: Result := SMsgDlgYesToAll;
mbHelp: Result := SMsgDlgHelp;
else
raise ETntInternalError.Create('Unexpected MsgDlgBtn in GetButtonCaption.');
end;
end;
var
IconIDs: array[TMsgDlgType] of PAnsiChar = (IDI_EXCLAMATION, IDI_HAND,
IDI_ASTERISK, IDI_QUESTION, nil);
ButtonNames: array[TMsgDlgBtn] of WideString = (
'Yes', 'No', 'OK', 'Cancel', 'Abort', 'Retry', 'Ignore', 'All', 'NoToAll',
'YesToAll', 'Help');
ModalResults: array[TMsgDlgBtn] of Integer = (
mrYes, mrNo, mrOk, mrCancel, mrAbort, mrRetry, mrIgnore, mrAll, mrNoToAll,
mrYesToAll, 0);
function WideCreateMessageDialog(const Msg: WideString; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons): TTntForm;
const
mcHorzMargin = 8;
mcVertMargin = 8;
mcHorzSpacing = 10;
mcVertSpacing = 10;
mcButtonWidth = 50;
mcButtonHeight = 14;
mcButtonSpacing = 4;
var
DialogUnits: TPoint;
HorzMargin, VertMargin, HorzSpacing, VertSpacing, ButtonWidth,
ButtonHeight, ButtonSpacing, ButtonCount, ButtonGroupWidth,
IconTextWidth, IconTextHeight, X, ALeft: Integer;
B, DefaultButton, CancelButton: TMsgDlgBtn;
IconID: PAnsiChar;
ATextRect: TRect;
ThisButtonWidth: integer;
begin
Result := TTntMessageForm.CreateNew(Application);
with Result do
begin
BiDiMode := Application.BiDiMode;
BorderStyle := bsDialog;
Canvas.Font := Font;
KeyPreview := True;
Position := poDesigned;
OnKeyDown := TTntMessageForm(Result).CustomKeyDown;
DialogUnits := GetAveCharSize(Canvas);
HorzMargin := MulDiv(mcHorzMargin, DialogUnits.X, 4);
VertMargin := MulDiv(mcVertMargin, DialogUnits.Y, 8);
HorzSpacing := MulDiv(mcHorzSpacing, DialogUnits.X, 4);
VertSpacing := MulDiv(mcVertSpacing, DialogUnits.Y, 8);
ButtonWidth := MulDiv(mcButtonWidth, DialogUnits.X, 4);
for B := Low(TMsgDlgBtn) to High(TMsgDlgBtn) do
begin
if B in Buttons then
begin
ATextRect := Rect(0,0,0,0);
Tnt_DrawTextW(Canvas.Handle,
PWideChar(GetButtonCaption(B)), -1,
ATextRect, DT_CALCRECT or DT_LEFT or DT_SINGLELINE or
DrawTextBiDiModeFlagsReadingOnly);
with ATextRect do ThisButtonWidth := Right - Left + 8;
if ThisButtonWidth > ButtonWidth then
ButtonWidth := ThisButtonWidth;
end;
end;
ButtonHeight := MulDiv(mcButtonHeight, DialogUnits.Y, 8);
ButtonSpacing := MulDiv(mcButtonSpacing, DialogUnits.X, 4);
SetRect(ATextRect, 0, 0, Screen.Width div 2, 0);
Tnt_DrawTextW(Canvas.Handle, PWideChar(Msg), Length(Msg) + 1, ATextRect,
DT_EXPANDTABS or DT_CALCRECT or DT_WORDBREAK or
DrawTextBiDiModeFlagsReadingOnly);
IconID := IconIDs[DlgType];
IconTextWidth := ATextRect.Right;
IconTextHeight := ATextRect.Bottom;
if IconID <> nil then
begin
Inc(IconTextWidth, 32 + HorzSpacing);
if IconTextHeight < 32 then IconTextHeight := 32;
end;
ButtonCount := 0;
for B := Low(TMsgDlgBtn) to High(TMsgDlgBtn) do
if B in Buttons then Inc(ButtonCount);
ButtonGroupWidth := 0;
if ButtonCount <> 0 then
ButtonGroupWidth := ButtonWidth * ButtonCount +
ButtonSpacing * (ButtonCount - 1);
ClientWidth := Max(IconTextWidth, ButtonGroupWidth) + HorzMargin * 2;
ClientHeight := IconTextHeight + ButtonHeight + VertSpacing +
VertMargin * 2;
Left := (Screen.Width div 2) - (Width div 2);
Top := (Screen.Height div 2) - (Height div 2);
if DlgType <> mtCustom then
Caption := GetMessageCaption(DlgType)
else
Caption := TntApplication.Title;
if IconID <> nil then
with TTntImage.Create(Result) do
begin
Name := 'Image';
Parent := Result;
Picture.Icon.Handle := LoadIcon(0, IconID);
SetBounds(HorzMargin, VertMargin, 32, 32);
end;
TTntMessageForm(Result).Message := TTntLabel.Create(Result);
with TTntMessageForm(Result).Message do
begin
Name := 'Message';
Parent := Result;
WordWrap := True;
Caption := Msg;
BoundsRect := ATextRect;
BiDiMode := Result.BiDiMode;
ALeft := IconTextWidth - ATextRect.Right + HorzMargin;
if UseRightToLeftAlignment then
ALeft := Result.ClientWidth - ALeft - Width;
SetBounds(ALeft, VertMargin,
ATextRect.Right, ATextRect.Bottom);
end;
if mbOk in Buttons then DefaultButton := mbOk else
if mbYes in Buttons then DefaultButton := mbYes else
DefaultButton := mbRetry;
if mbCancel in Buttons then CancelButton := mbCancel else
if mbNo in Buttons then CancelButton := mbNo else
CancelButton := mbOk;
X := (ClientWidth - ButtonGroupWidth) div 2;
for B := Low(TMsgDlgBtn) to High(TMsgDlgBtn) do
if B in Buttons then
with TTntButton.Create(Result) do
begin
Name := ButtonNames[B];
Parent := Result;
Caption := GetButtonCaption(B);
ModalResult := ModalResults[B];
if B = DefaultButton then Default := True;
if B = CancelButton then Cancel := True;
SetBounds(X, IconTextHeight + VertMargin + VertSpacing,
ButtonWidth, ButtonHeight);
Inc(X, ButtonWidth + ButtonSpacing);
if B = mbHelp then
OnClick := TTntMessageForm(Result).HelpButtonClick;
end;
end;
end;
function WideMessageDlg(const Msg: WideString; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; HelpCtx: Longint): Integer;
begin
Result := WideMessageDlgPosHelp(Msg, DlgType, Buttons, HelpCtx, -1, -1, '');
end;
function WideMessageDlgPos(const Msg: WideString; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; HelpCtx: Longint; X, Y: Integer): Integer;
begin
Result := WideMessageDlgPosHelp(Msg, DlgType, Buttons, HelpCtx, X, Y, '');
end;
function WideMessageDlgPosHelp(const Msg: WideString; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; HelpCtx: Longint; X, Y: Integer;
const HelpFileName: WideString): Integer;
begin
with WideCreateMessageDialog(Msg, DlgType, Buttons) do
try
HelpContext := HelpCtx;
HelpFile := HelpFileName;
if X >= 0 then Left := X;
if Y >= 0 then Top := Y;
if (Y < 0) and (X < 0) then Position := poScreenCenter;
Result := ShowModal;
finally
Free;
end;
end;
procedure WideShowMessage(const Msg: WideString);
begin
WideShowMessagePos(Msg, -1, -1);
end;
procedure WideShowMessageFmt(const Msg: WideString; Params: array of const);
begin
WideShowMessage(WideFormat(Msg, Params));
end;
procedure WideShowMessagePos(const Msg: WideString; X, Y: Integer);
begin
WideMessageDlgPos(Msg, mtCustom, [mbOK], 0, X, Y);
end;
{ Input dialog }
function WideInputQuery(const ACaption, APrompt: WideString; var Value: WideString): Boolean;
var
Form: TTntForm;
Prompt: TTntLabel;
Edit: TTntEdit;
DialogUnits: TPoint;
ButtonTop, ButtonWidth, ButtonHeight: Integer;
begin
Result := False;
Form := TTntForm.Create(Application);
with Form do begin
try
Canvas.Font := Font;
DialogUnits := GetAveCharSize(Canvas);
BorderStyle := bsDialog;
Caption := ACaption;
ClientWidth := MulDiv(180, DialogUnits.X, 4);
Position := poScreenCenter;
Prompt := TTntLabel.Create(Form);
with Prompt do
begin
Parent := Form;
Caption := APrompt;
Left := MulDiv(8, DialogUnits.X, 4);
Top := MulDiv(8, DialogUnits.Y, 8);
Constraints.MaxWidth := MulDiv(164, DialogUnits.X, 4);
WordWrap := True;
end;
Edit := TTntEdit.Create(Form);
with Edit do
begin
Parent := Form;
Left := Prompt.Left;
Top := Prompt.Top + Prompt.Height + 5;
Width := MulDiv(164, DialogUnits.X, 4);
MaxLength := 255;
Text := Value;
SelectAll;
end;
ButtonTop := Edit.Top + Edit.Height + 15;
ButtonWidth := MulDiv(50, DialogUnits.X, 4);
ButtonHeight := MulDiv(14, DialogUnits.Y, 8);
with TTntButton.Create(Form) do
begin
Parent := Form;
Caption := SMsgDlgOK;
ModalResult := mrOk;
Default := True;
SetBounds(MulDiv(38, DialogUnits.X, 4), ButtonTop, ButtonWidth,
ButtonHeight);
end;
with TTntButton.Create(Form) do
begin
Parent := Form;
Caption := SMsgDlgCancel;
ModalResult := mrCancel;
Cancel := True;
SetBounds(MulDiv(92, DialogUnits.X, 4), Edit.Top + Edit.Height + 15, ButtonWidth,
ButtonHeight);
Form.ClientHeight := Top + Height + 13;
end;
if ShowModal = mrOk then
begin
Value := Edit.Text;
Result := True;
end;
finally
Form.Free;
end;
end;
end;
function WideInputBox(const ACaption, APrompt, ADefault: WideString): WideString;
begin
Result := ADefault;
WideInputQuery(ACaption, APrompt, Result);
end;
function WidePromptForFileName(var AFileName: WideString; const AFilter: WideString = '';
const ADefaultExt: WideString = ''; const ATitle: WideString = '';
const AInitialDir: WideString = ''; SaveDialog: Boolean = False): Boolean;
var
Dialog: TTntOpenDialog;
begin
if SaveDialog then
begin
Dialog := TTntSaveDialog.Create(nil);
Dialog.Options := Dialog.Options + [ofOverwritePrompt];
end
else
Dialog := TTntOpenDialog.Create(nil);
with Dialog do
try
Title := ATitle;
DefaultExt := ADefaultExt;
if AFilter = '' then
Filter := SDefaultFilter else
Filter := AFilter;
InitialDir := AInitialDir;
FileName := AFileName;
Result := Execute;
if Result then
AFileName := FileName;
finally
Free;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -