Hier der code einer einfachen aber sicheren Passwort Eingabekomponente.
Da es nicht von TEdit abstammt kann man es auch nichts auslesen.
Code:
type
TWPGeheim = class(TCustomControl)
private
FText : String;
protected
procedure CreateParams(var Params: TCreateParams); override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState;X, Y: Integer); override;
public
procedure Paint; override;
procedure KeyPress(var Key: Char); override;
property Text : String read FText write FText;
end;
{ TWPGeheim }
procedure TWPGeheim.KeyPress(var Key: Char);
begin
if Key=#8 then FText := Copy(FText,1,Length(FText)-1)
else if Key>#32 then FText := FText + Key;
invalidate;
end;
procedure TWPGeheim.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
begin
inherited;
SetFocus;
end;
procedure TWPGeheim.Paint;
var s : string;
i : Integer;
begin
inherited;
if Focused then
Canvas.Font.Color := clHighlight
else Canvas.Font.Color := clWindowText;
SetLength(s, Length(FText));
for I := 1 to Length(s) do s[i] := '*';
Canvas.TextOut( 1,1,s);
end;
procedure TWPGeheim.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.Style := Params.Style or WS_BORDER or WS_TABSTOP;
end;
Kann man natürlich erweitern, z.b. um CTRL+V für Einfügen zu unterstützen:
Code:
procedure TWPGeheim.KeyPress(var Key: Char);
begin
if Key=#8 then FText := Copy(FText,1,Length(FText)-1)
else if Key>#32 then FText := FText + Key
else if Key=Char(22) then // ^V
begin
Clipboard.Open;
try
SetLength(FText,30);
SetLength(FText,Clipboard.GetTextBuf(PChar(FText),30));
finally
Clipboard.Close;
end;
end;
invalidate;
end;