📄 unit1.pas
字号:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TPasswordEdit = class(TEdit)
private
FFalsePassword: TCaption;
FAllowPasswordRead: Boolean;
FAllowPasswordCharChange: Boolean;
function GetPasswordChar: Char;
function GetText: TCaption;
procedure SetPasswordChar(const Value: Char);
procedure SetText(const Value: TCaption);
public
constructor Create(AOwner: TComponent); override;
procedure DefaultHandler(var Message); override;
published
property AllowPasswordCharChange: Boolean read FAllowPasswordCharChange write FAllowPasswordCharChange;
property AllowPasswordRead: Boolean read FAllowPasswordRead write FAllowPasswordRead;
property PasswordChar: Char read GetPasswordChar write SetPasswordChar default '*';
property FalsePassword: TCaption read FFalsePassword write FFalsePassword;
property Text: TCaption read GetText write SetText;
end;
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Label2: TLabel;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
PasswordEdit1: TPasswordEdit;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
constructor TPasswordEdit.Create(AOwner: TComponent);
begin
AllowPasswordCharChange := true;
AllowPasswordRead := true;
inherited Create(AOwner);
AllowPasswordCharChange := false;
AllowPasswordRead := false;
PasswordChar := '*'; {显示*}
end;
procedure TPasswordEdit.SetPasswordChar(const Value: Char);
var
OldAPCC, OldAPR: boolean;
begin
OldAPCC := FAllowPasswordCharChange;
OldAPR := FAllowPasswordRead;
FAllowPasswordCharChange := true;
FAllowPasswordRead := true;
if HandleAllocated then
inherited PasswordChar := Char(Sendmessage(Handle, EM_GETPASSWORDCHAR, 0, 0));
inherited PasswordChar := Value;
FAllowPasswordCharChange := OldAPCC;
FAllowPasswordRead := OldAPR;
end;
function TPasswordEdit.GetPasswordChar: Char;
begin
if HandleAllocated then
Result := Char(Sendmessage(Handle, EM_GETPASSWORDCHAR, 0, 0))
else
Result := inherited PasswordChar;
end;
procedure TPasswordEdit.SetText(const Value: TCaption);
begin
inherited Text := Value;
end;
function TPasswordEdit.GetText: TCaption;
var
OldAPCC, OldAPR: boolean;
begin
OldAPCC := FAllowPasswordCharChange;
OldAPR := FAllowPasswordRead;
FAllowPasswordCharChange := true;
FAllowPasswordRead := true;
Result := inherited Text;
FAllowPasswordCharChange := OldAPCC;
FAllowPasswordRead := OldAPR;
end;
procedure TPasswordEdit.DefaultHandler(var Message);
var
P: PChar;
begin
if (csDesigning in ComponentState) or (csCreating in ControlState) then
inherited
else
with TMessage(Message) do
case msg of
EM_SETPASSWORDCHAR: if FAllowPasswordCharChange then
inherited;
WM_GETTEXT: if FAllowPasswordRead then inherited
else begin
P := PChar(FFalsePassword);
Result := StrLen(StrLCopy(PChar(LParam), P, WParam - 1));
end;
WM_GETTEXTLENGTH: if FAllowPasswordRead then inherited
else
if PChar(FFalsePassword) = nil then Result := 0
else Result := StrLen(PChar(FFalsePassword));
else
inherited;
end
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
PasswordEdit1 := TPasswordEdit.Create(self);
PasswordEdit1.parent := form1;
PasswordEdit1.Width := 150;
PassWordEdit1.Height := 21;
PasswordEdit1.Top := 115;
PasswordEdit1.Left := 80;
PasswordEdit1.PasswordChar := '*';
PasswordEdit1.AllowPasswordRead := false;
PasswordEdit1.Visible := true;
PasswordEdit1.Text:='Hello';
PasswordEdit1.FalsePassword:= '想读我?没门!'
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -