//==============================================================================
// Unit Name: PwdEdit
// Author : ysai
// Purpose : 密码输入框控件
// History : 2007-04-24
//==============================================================================
unit PwdEdit;
interface
uses
Windows,Messages,SysUtils,StdCtrls,Controls,Classes;
const
UM_GetText = WM_USER + $201;
UM_SETPASSWORDCHAR = WM_USER + $202;
type
TPasswordEdit = class(TEdit)
private
FPasswordChar: Char;
function GetPasswordText: string;
procedure SetPasswordText(const Value: string);
procedure SetPasswordChar(const Value: Char);
protected
procedure WndProc(var Msg: TMessage); override;
procedure CreateWnd; override;
published
property PasswordText : string read GetPasswordText Write SetPasswordText;
property PasswordChar: Char read FPasswordChar write SetPasswordChar default #0;
end;
implementation
{ TPasswordEdit }
procedure TPasswordEdit.WndProc(var Msg: TMessage);
procedure GetPasswordText(var Msg: TMessage);
var
ps : PChar;
len : Integer;
begin
ps := Pointer(Msg.lParam);
len := Msg.wParam;
ZeroMemory(ps,len);
if Length(PasswordText) < len then
len := Length(PasswordText);
FillMemory(ps,len,Byte(PasswordChar));
Msg.Result := len;
end;
begin
case Msg.Msg of
WM_GETTEXT :
begin
if PasswordChar = #0 then
inherited
else
GetPasswordText(Msg);
end;
UM_GetText :
begin
Msg.Msg := WM_GETTEXT;
inherited;
end;
EM_GETLINE :
begin
if PasswordChar = #0 then
inherited
else if Msg.wParam = 0 then
Msg.Result := Length(PasswordText)
else
GetPasswordText(Msg);
end;
EM_SETPASSWORDCHAR :;
UM_SETPASSWORDCHAR :
begin
Msg.Msg := EM_SETPASSWORDCHAR;
inherited;
end;
else
inherited;
end;
end;
procedure TPasswordEdit.CreateWnd;
begin
inherited CreateWnd;
if PasswordChar <> #0 then
SendMessage(Handle,UM_SETPASSWORDCHAR,Ord(FPasswordChar),0);
end;
function TPasswordEdit.GetPasswordText: string;
var
ps : array[0..MAXBYTE] of char;
begin
SendMessage(Handle,UM_GetText,MAXBYTE,Longint(@ps));
Result := strpas(ps);
end;
procedure TPasswordEdit.SetPasswordChar(const Value: Char);
begin
if FPasswordChar <> Value then
begin
FPasswordChar := Value;
if HandleAllocated then
begin
SendMessage(Handle,0);
SetTextBuf(PChar(PasswordText));
end;
end;
end;
procedure TPasswordEdit.SetPasswordText(const Value: string);
begin
Text := Value;
end;
end.
原文链接:/vb/261896.html