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

前端之家收集整理的这篇文章主要介绍了一个密码输入控件,能防止大多数查看*号密码内容的软件取密码(VB编写,也没有什么技术含量)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
//==============================================================================
// 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.
原文链接:https://www.f2er.com/vb/261896.html

猜你在找的VB相关文章