Delphi 中 InputQuery 实现密码输入

这篇博客介绍了如何在Delphi中通过自定义消息处理,利用标准输入对话框InputBox实现密码输入功能。通过PostMessage发送InputBoxMessage并处理该消息,设置编辑框为密码字符显示,从而达到安全修改用户口令的目的。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Delphi 中 InputQuery 实现密码输入

修改用户口令时为了避免自己建立新的口令修改窗口,借用delphi中的标准输入对话框。

实现代码如下:

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls;

const

InputBoxMessage = WM_USER + 200;

type

TForm1 = class(TForm)

Edit1: TEdit;

Button1: TButton;

procedure Button1Click(Sender: TObject);

private

{ Private declarations }

procedure InputBoxSetPasswordChar(var Msg: TMessage); message InputBoxMessage;

public

{ Public declarations }

end;

var

Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

var

InputString: string;

begin

PostMessage(Handle, InputBoxMessage, 0, 0);

if InputQuery('Input Box', 'Please Enter a Password', InputString ) then

ShowMessage(InputString);

end;

procedure TForm1.InputBoxSetPasswordChar(var Msg: TMessage);

var

hInputForm, hEdit: HWND;

begin

hInputForm := Screen.Forms[0].Handle;

if (hInputForm <> 0) then

begin

hEdit := FindWindowEx(hInputForm, 0, 'TEdit', nil);

SendMessage(hEdit, EM_SETPASSWORDCHAR, Ord('*'), 0);

end;

end;

end.

这种方法实现的比较巧妙!重点是PostMessage(Handle, InputBoxMessage, 0, 0);

和对InputBoxMessage消息的处理!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值