VCL 內建 InputQuery (由 InputBox 呼叫) 固定英文按鈕、無星號遮罩、字體隨系統。透過複製 dialogs.pas 至專案目錄並修改,可實現中文按鈕、密碼模式、指定字體。
$(BCB)\Source\Vcl\dialogs.pas 複製一份到專案目錄。InputQuery 函數 (約 1857-1927 行):
Form.Font.Name := '新細明體'; Font.Size := 9; (中文環境建議新細明體/微軟正黑體)。'確定'、'取消'。'Password',則 Edit.PasswordChar := '*' 啟用密碼模式。dialogs.pas (Project | Add to Project)。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
Font.Name := '新細明體'; Font.Size := 9; Canvas.Font := Font;
DialogUnits := GetAveCharSize(Canvas);
BorderStyle := bsDialog; Caption := ACaption;
ClientWidth := MulDiv(180, DialogUnits.X, 4);
Position := poScreenCenter;
Prompt := TLabel.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 := TEdit.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;
if Text = 'Password' then PasswordChar := '*'; // 密碼模式觸發條件
end;
ButtonTop := Edit.Top + Edit.Height + 15;
ButtonWidth := MulDiv(50, DialogUnits.X, 4);
ButtonHeight := MulDiv(14, DialogUnits.Y, 8);
with TButton.Create(Form) do begin Parent := Form; Caption := '確定';
ModalResult := mrOk; Default := True;
SetBounds(MulDiv(38, DialogUnits.X, 4), ButtonTop, ButtonWidth, ButtonHeight); end;
with TButton.Create(Form) do begin Parent := Form; Caption := '取消';
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;
// 一般輸入
void __fastcall TForm1::Button1Click(TObject *Sender)
{ String s = ""; InputQuery("標題", "提示", s); ShowMessage(s); }
// 密碼輸入 (預設字串設為 "Password")
void __fastcall TForm1::Button2Click(TObject *Sender)
{ String s = "Password"; InputQuery("標題", "請輸入密碼", s); ShowMessage(s); }
'Password' 可自行修改為其他不易衝突的字串。