unit khnPassEdit;
{$mode objfpc}{$H+}
interface
uses
LCLType, LMessages, Classes, SysUtils, Graphics, Controls, StdCtrls, ExtCtrls;
type
{ THiddenPassEdit }
THiddenPassEdit = class(TCustomEdit)
private
FCanvas : TControlCanvas;
FPasswordChar: char;
procedure setPasswordChar(AValue: char);
protected
procedure Change;override;
procedure WMPaint(var Msg: TLMPaint); message LM_PAINT;
procedure WndProc(var Message: TLMessage); override;
public
constructor Create(aOwner:TComponent);override;
destructor Destroy;override;
published
// we cant use real PasswordChar because it will set ES_PASSWORD flag
property PasswordChar : char read FPasswordChar write setPasswordChar default '*';
end;
implementation
uses
Windows;
{ THiddenPassEdit }
procedure THiddenPassEdit.setPasswordChar(AValue: char);
begin
if FPasswordChar=AValue then Exit;
FPasswordChar:=AValue;
Invalidate;
end;
procedure THiddenPassEdit.Change;
begin
inherited Change;
Repaint;
end;
constructor THiddenPassEdit.Create(aOwner: TComponent);
begin
inherited Create(aOwner);
ControlStyle:=ControlStyle+[csOpaque];
FCanvas:=TControlCanvas.Create;
FPasswordChar:='*';
Font.Name:='Courier New';
Font.Size:=9;
Width:=132;
MaxLength:=16;
end;
destructor THiddenPassEdit.Destroy;
begin
inherited;
FCanvas.Free;
end;
procedure THiddenPassEdit.WMPaint(var Msg: TLMPaint);
var
I : Integer;
X : Integer;
begin
FCanvas.Handle:=Msg.DC;
FCanvas.Font.Assign(Font);
X:=1;
for I:=1 to Length(Text) do
begin
if (I>SelStart) and (I<=SelStart+SelLength) then
begin
FCanvas.Font.Color:=clHighlightText;
FCanvas.Brush.Color:=clHighlight;
end
else
begin
FCanvas.Font.Color:=Font.Color;
FCanvas.Brush.Color:=clWindow;
end;
FCanvas.TextOut(X,1,FPasswordChar);
X:=X+FCanvas.TextWidth(Text[I]);
end;
Msg.Result:=0;
end;
procedure THiddenPassEdit.WndProc(var Message: TLMessage);
begin
inherited WndProc(Message);
case Message.msg of
LM_KEYFIRST..LM_KEYLAST,
LM_LBUTTONDOWN..LM_XBUTTONDBLCLK,
LM_SETFOCUS,LM_KILLFOCUS:
Repaint;
end;
end;
end.