首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> C++ >> 關於C++

C++ Builder 修改 VCL 原始碼自訂輸入對話框

     編輯:關於C++

解決 InputBox/InputQuery 按鈕為英文、無密碼模式、字體過大的問題

VCL 內建 InputQuery (由 InputBox 呼叫) 固定英文按鈕、無星號遮罩、字體隨系統。透過複製 dialogs.pas 至專案目錄並修改,可實現中文按鈕、密碼模式、指定字體。

修改步驟

  1. 從 $(BCB)\Source\Vcl\dialogs.pas 複製一份到專案目錄。
  2. 編輯 InputQuery 函數 (約 1857-1927 行):
    • 設定 Form.Font.Name := '新細明體'; Font.Size := 9; (中文環境建議新細明體/微軟正黑體)。
    • 按鈕標題改為 '確定'、'取消'。
    • 若傳入預設字串為 'Password',則 Edit.PasswordChar := '*' 啟用密碼模式。
  3. 專案加入修改後的 dialogs.pas (Project | Add to Project)。
  4. Project | Options | Packages 取消 Build with runtime packages 勾選 (必須靜態連結修改後的 VCL 代碼)。

修改後關鍵片段

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); }

注意

  • 修改 VCL 來源碼屬進階技巧,升級 BCB 版本需重新套用。
  • 密碼觸發字串 'Password' 可自行修改為其他不易衝突的字串。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved