您的位置:首页 > 编程语言 > VB

一个密码输入控件,能防止大多数查看*号密码内容的软件取密码(VB编写,也没有什么技术含量)

2010-07-01 11:59 696 查看
//==============================================================================
// 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, UM_SETPASSWORDCHAR, Ord(FPasswordChar), 0);
SetTextBuf(PChar(PasswordText));
end;
end;
end;

procedure TPasswordEdit.SetPasswordText(const Value: string);
begin
Text  :=  Value;
end;

end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐