delphi InputBox 입력은 * 로 대체하고, delphi의 inputbox 입력상자에 암호 대표자를 표시하는 방법 *

3230 단어 delphi
Mydialogs 정의pas
unit Mydialogs;

interface

uses Windows, Graphics, Forms, StdCtrls, Consts, Dialogs, Controls,SysUtils,Types;

function GetAveCharSize(Canvas: TCanvas): TPoint;
function InputBoxEx(const ACaption, APrompt, ADefault: string;PassWordChar: char = #0): string;

implementation


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;
function InputQueryEx(const ACaption, APrompt: string;
  var Value: string; APassWordChar: char = #0): 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);
      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
        passwordchar := APassWordChar;
        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 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), 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;

function InputBoxEx(const ACaption, APrompt, ADefault: string;PassWordChar: char = #0): string;
begin
  Result := ADefault;
  InputQueryEx(ACaption, APrompt, Result, PassWordChar);
end;

end.



버튼 이벤트에서 다음과 같이 사용합니다.
procedure TForm1.Button5Click(Sender: TObject);
var str :string;
begin
 //str := InputBox('', '   6-8   ', '');
 str:=InputBoxEx('','   6-8   ','','*');
 ackey.Text:=str;
end;

효과 보기

좋은 웹페이지 즐겨찾기