📄 dialog.pas
字号:
Result := FFindText;
end;
function TFindDialog.GetLeft: Integer;
begin
Result := Position.X;
end;
function TFindDialog.GetPosition: TPoint;
var
Rect: TRect;
begin
Result := FPosition;
if FFindHandle <> 0 then
begin
GetWindowRect(FFindHandle, Rect);
Result := Rect.TopLeft;
end;
end;
function TFindDialog.GetReplaceText: string;
begin
Result := FReplaceText;
end;
function TFindDialog.GetTop: Integer;
begin
Result := Position.Y;
end;
function TFindDialog.MessageHook(var Msg: TMessage): Boolean;
var
Option: TFindOption;
Rect: TRect;
begin
Result := inherited MessageHook(Msg);
if not Result then
if (Msg.Msg = FindMsg) and (Pointer(Msg.LParam) = @FFindReplace) then
begin
FOptions := [];
for Option := Low(Option) to High(Option) do
if (FFindReplace.Flags and FindOptions[Option]) <> 0 then
Include(FOptions, Option);
if (FFindReplace.Flags and FR_FINDNEXT) <> 0 then
Find
else
if (FFindReplace.Flags and (FR_REPLACE or FR_REPLACEALL)) <> 0 then
Replace
else
if (FFindReplace.Flags and FR_DIALOGTERM) <> 0 then
begin
GetWindowRect(FFindHandle, Rect);
FPosition := Rect.TopLeft;
FFindHandle := 0;
PostMessage(FRedirector.Handle,CM_RELEASE,0,0); // free redirector later
FRedirector := nil;
end;
Result := True;
end;
end;
procedure TFindDialog.Replace;
begin
if Assigned(FOnReplace) then FOnReplace(Self);
end;
procedure TFindDialog.SetFindText(const Value: string);
begin
StrLCopy(FFindText, PChar(Value), SizeOf(FFindText) - 1);
end;
procedure TFindDialog.SetLeft(Value: Integer);
begin
SetPosition(Point(Value, Top));
end;
procedure TFindDialog.SetPosition(const Value: TPoint);
begin
if (FPosition.X <> Value.X) or (FPosition.Y <> Value.Y) then
begin
FPosition := Value;
if FFindHandle <> 0 then
SetWindowPos(FFindHandle, 0, Value.X, Value.Y, 0, 0,
SWP_NOACTIVATE or SWP_NOSIZE or SWP_NOZORDER);
end;
end;
procedure TFindDialog.SetReplaceText(const Value: string);
begin
StrLCopy(FReplaceText, PChar(Value), SizeOf(FReplaceText) - 1);
end;
procedure TFindDialog.SetTop(Value: Integer);
begin
SetPosition(Point(Left, Value));
end;
{ TReplaceDialog }
constructor TReplaceDialog.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FFindReplaceFunc := CommDlg.ReplaceText;
end;
{ Message dialog }
function Max(I, J: Integer): Integer;
begin
if I > J then Result := I else Result := J;
end;
function GetAveCharSize(Canvas: TCanvas): TPoint;
var
I: Integer;
Buffer: array[0..51] of Char;
begin
for I := 0 to 25 do Buffer[I] := Chr(I + Ord('A'));
for I := 0 to 25 do Buffer[I + 26] := Chr(I + Ord('a'));
GetTextExtentPoint(Canvas.Handle, Buffer, 52, TSize(Result));
Result.X := Result.X div 52;
end;
type
TMessageForm = class(TForm)
private
procedure HelpButtonClick(Sender: TObject);
public
constructor CreateNew(AOwner: TComponent); reintroduce;
end;
constructor TMessageForm.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 TMessageForm.HelpButtonClick(Sender: TObject);
begin
Application.HelpContext(HelpContext);
end;
var
Captions: array[TMsgDlgType] of Pointer = (@SMsgDlgWarning, @SMsgDlgError,
@SMsgDlgInformation, @SMsgDlgConfirm, nil);
IconIDs: array[TMsgDlgType] of PChar = (IDI_EXCLAMATION, IDI_HAND,
IDI_ASTERISK, IDI_QUESTION, nil);
ButtonNames: array[TMsgDlgBtn] of string = (
'Yes', 'No', 'OK', 'Cancel', 'Abort', 'Retry', 'Ignore', 'All', 'NoToAll',
'YesToAll', 'Help');
ButtonCaptions: array[TMsgDlgBtn] of Pointer = (
@SMsgDlgYes, @SMsgDlgNo, @SMsgDlgOK, @SMsgDlgCancel, @SMsgDlgAbort,
@SMsgDlgRetry, @SMsgDlgIgnore, @SMsgDlgAll, @SMsgDlgNoToAll, @SMsgDlgYesToAll,
@SMsgDlgHelp);
ModalResults: array[TMsgDlgBtn] of Integer = (
mrYes, mrNo, mrOk, mrCancel, mrAbort, mrRetry, mrIgnore, mrAll, mrNoToAll,
mrYesToAll, 0);
var
ButtonWidths : array[TMsgDlgBtn] of integer; // initialized to zero
function CreateMessageDialog(const Msg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons): TForm;
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: PChar;
TextRect: TRect;
dlgCaption,btCaption:string;
begin
Result := TMessageForm.CreateNew(Application);
with Result do
begin
BiDiMode := Application.BiDiMode;
BorderStyle := bsDialog;
Canvas.Font := Font;
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
if ButtonWidths[B] = 0 then
begin
TextRect := Rect(0,0,0,0);
Windows.DrawText( canvas.handle,
PChar(LoadResString(ButtonCaptions[B])), -1,
TextRect, DT_CALCRECT or DT_LEFT or DT_SINGLELINE or
DrawTextBiDiModeFlagsReadingOnly);
with TextRect do ButtonWidths[B] := Right - Left + 8;
end;
if ButtonWidths[B] > ButtonWidth then
ButtonWidth := ButtonWidths[B];
end;
end;
ButtonHeight := MulDiv(mcButtonHeight, DialogUnits.Y, 8);
ButtonSpacing := MulDiv(mcButtonSpacing, DialogUnits.X, 4);
SetRect(TextRect, 0, 0, Screen.Width div 2, 0);
DrawText(Canvas.Handle, PChar(Msg), Length(Msg), TextRect,
DT_EXPANDTABS or DT_CALCRECT or DT_WORDBREAK or
DrawTextBiDiModeFlagsReadingOnly);
IconID := IconIDs[DlgType];
IconTextWidth := TextRect.Right;
IconTextHeight := TextRect.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 = mtWarning then
Caption:=Pchar(sWarning) else
if DlgType = mtError then
Caption:=Pchar(sError) else
if DlgType = mtInformation then
Caption:=Pchar(sInformation) else
if LoadResString(Captions[DlgType]) = 'Confirm' then
Caption:=Pchar(sConfrim) else
Caption := Application.Title;
{* if DlgType <> mtCustom then
Caption := LoadResString(Captions[DlgType]) else
Caption := Application.Title;
*}
if IconID <> nil then
with TImage.Create(Result) do
begin
Name := 'Image';
Parent := Result;
Picture.Icon.Handle := LoadIcon(0, IconID);
SetBounds(HorzMargin, VertMargin, 32, 32);
end;
with TLabel.Create(Result) do
begin
Name := 'Message';
Parent := Result;
WordWrap := True;
Caption := Msg;
BoundsRect := TextRect;
BiDiMode := Result.BiDiMode;
ALeft := IconTextWidth - TextRect.Right + HorzMargin;
if UseRightToLeftAlignment then
ALeft := Result.ClientWidth - ALeft - Width;
SetBounds(ALeft, VertMargin,
TextRect.Right, TextRect.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 TButton.Create(Result) do
begin
Name := ButtonNames[B];
Parent := Result;
if LoadResString(ButtonCaptions[B])='&Yes' then
Caption :=Pchar(sYes) else
if LoadResString(ButtonCaptions[B])='&No' then
Caption :=Pchar(sNo) else
if LoadResString(ButtonCaptions[B])='OK' then
Caption :=Pchar(sOK) else
if LoadResString(ButtonCaptions[B])='Cancel' then
Caption := Pchar(sCancel) else
if LoadResString(ButtonCaptions[B])='&Abort' then
Caption := Pchar(sAbort) else
if LoadResString(ButtonCaptions[B])='&Retry' then
Caption :=Pchar(sRetry) else
if LoadResString(ButtonCaptions[B])='&Ignore' then
Caption :=Pchar(sIgnore) else
if LoadResString(ButtonCaptions[B])='&All' then
Caption := Pchar(sAll) else
if LoadResString(ButtonCaptions[B])='&Help' then
Caption := Pchar(sHelp) else
Caption := LoadResString(ButtonCaptions[B]);
// Caption := LoadResString(ButtonCaptions[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 := TMessageForm(Result).HelpButtonClick;
end;
end;
end;
function MessageDlg(const Msg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; HelpCtx: Longint): Integer;
begin
Result := MessageDlgPosHelp(Msg, DlgType, Buttons, HelpCtx, -1, -1, '');
end;
function MessageDlgPos(const Msg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; HelpCtx: Longint; X, Y: Integer): Integer;
begin
Result := MessageDlgPosHelp(Msg, DlgType, Buttons, HelpCtx, X, Y, '');
end;
function MessageDlgPosHelp(const Msg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; HelpCtx: Longint; X, Y: Integer;
const HelpFileName: string): Integer;
begin
with CreateMessageDialog(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 ShowMessage(const Msg: string);
begin
ShowMessagePos(Msg, -1, -1);
end;
procedure ShowMessageFmt(const Msg: string; Params: array of const);
begin
ShowMessage(Format(Msg, Params));
end;
procedure ShowMessagePos(const Msg: string; X, Y: Integer);
begin
MessageDlgPos(Msg, mtCustom, [mbOK], 0, X, Y);
end;
{ Input dialog }
function InputQuery(const ACaption, APrompt: string;
var Value: string): Boolean;
var
Form: TForm;
Prompt: TLabel;
Edit: TEdit;
DialogUnits: TPoint;
ButtonTop, ButtonWidth, ButtonHeight: Integer;
begin
Result := False;
Form := TForm.Create(Application);
with Form do
try
Canvas.Font := Font;
DialogUnits := GetAveCharSize(Canvas);
BorderStyle := bsDialog;
Caption := ACaption;
ClientWidth := MulDiv(180, DialogUnits.X, 4);
ClientHeight := MulDiv(63, DialogUnits.Y, 8);
Position := poScreenCenter;
Prompt := TLabel.Create(Form);
with Prompt do
begin
Parent := Form;
AutoSize := True;
Left := MulDiv(8, DialogUnits.X, 4);
Top := MulDiv(8, DialogUnits.Y, 8);
Caption := APrompt;
end;
Edit := TEdit.Create(Form);
with Edit do
begin
Parent := Form;
Left := Prompt.Left;
Top := MulDiv(19, DialogUnits.Y, 8);
Width := MulDiv(164, DialogUnits.X, 4);
MaxLength := 255;
Text := Value;
SelectAll;
end;
ButtonTop := MulDiv(41, DialogUnits.Y, 8);
ButtonWidth := MulDiv(50, DialogUnits.X, 4);
ButtonHeight := MulDiv(14, DialogUnits.Y, 8);
with TButton.Create(Form) do
begin
Parent := Form;
Caption := SMsgDlgOK;
ModalResult := mrOk;
Default := True;
SetBounds(MulDiv(38, DialogUnits.X, 4), ButtonTop, ButtonWidth,
ButtonHeight);
end;
with TButton.Create(Form) do
begin
Parent := Form;
Caption := SMsgDlgCancel;
ModalResult := mrCancel;
Cancel := True;
SetBounds(MulDiv(92, DialogUnits.X, 4), ButtonTop, ButtonWidth,
ButtonHeight);
end;
if ShowModal = mrOk then
begin
Value := Edit.Text;
Result := True;
end;
finally
Form.Free;
end;
end;
function InputBox(const ACaption, APrompt, ADefault: string): string;
begin
Result := ADefault;
InputQuery(ACaption, APrompt, Result);
end;
{ Initialization and cleanup }
procedure InitGlobals;
var
AtomText: array[0..31] of Char;
begin
HelpMsg := RegisterWindowMessage(HelpMsgString);
FindMsg := RegisterWindowMessage(FindMsgString);
WndProcPtrAtom := GlobalAddAtom(StrFmt(AtomText,
'WndProcPtr%.8X%.8X', [HInstance, GetCurrentThreadID]));
end;
initialization
InitGlobals;
finalization
if WndProcPtrAtom <> 0 then GlobalDeleteAtom(WndProcPtrAtom);
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -