微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

一个密码输入控件,能防止大多数查看*号密码内容的软件取密码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.jb51.cc/vb/261896.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐