1,183
社区成员
发帖
与我相关
我的任务
分享
1. 创建一个VCL Application工程,打开Form1.pas文件,加入下面的全局变量和鼠标钩子函数。
var
HookHandle: HHOOK;
function HookProc(code: integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
var
MousePoint: TPoint;
WindowHandle: THandle;
WindowStyle: Longint;
begin
// 获取鼠标位置
GetCursorPos(MousePoint);
// 获取包含鼠标的窗口句柄
WindowHandle := WindowFromPoint(MousePoint);
// 获取窗口风格
WindowStyle := GetWindowLong(WindowHandle, GWL_STYLE);
if TEventMsg(PEventMsg(lParam)^).message = WM_LBUTTONDOWN then
begin
// 是否为密码框
if WindowStyle and ES_PASSWORD <> 0 then
begin
// 去掉密码属性
PostMessage(WindowHandle, EM_SETPASSWORDCHAR, 0, 0);
end;
end;
Result := CallNextHookEx(HookHandle, code, wParam, lParam);
end;需要注意的是,程序中PostMessage(WindowHandle, EM_SETPASSWORDCHAR, 0, 0);只能使用PostMessage,而不能使用SendMessage代替。
2. 在Form1窗体上放下两个Button控件。并在他的单击事件中加入如下代码:
procedure TForm1.Button1Click(Sender: TObject);
begin
HookHandle := SetWindowsHookEx(WH_JOURNALRECORD, @HookProc, hInstance, 0);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
UnhookWindowsHookEx(HookHandle);
end;
PostMessage(WindowHandle, EM_SETPASSWORDCHAR, 0, 0);
procedure TForm1.Timer1Timer(Sender: TObject);
var
MousePoint: TPoint;
WindowHandle: THandle;
WindowStyle: Longint;
begin
// 获取鼠标位置
GetCursorPos(MousePoint);
// 获取包含鼠标的窗口句柄
WindowHandle := WindowFromPoint(MousePoint);
// 获取窗口风格
WindowStyle := GetWindowLong(WindowHandle, GWL_STYLE);
PostMessage(WindowHandle, EM_SETPASSWORDCHAR, 0, 0);
// 是否为密码框
if WindowStyle and ES_PASSWORD <> 0 then Edit1.Text:= '还是密码框' else Edit1.Text:= '不是密码框'
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function GetWindowText(
const HWnd : HWnd;
const GetPassWord : Boolean = False
):string;
//取得文本且可以取得密码
var
iPwdChar : Integer;
iPwdLast : Integer;
psText : array[0..MAXBYTE] of char;
i : Integer;
begin
Result := '';
if Hwnd = 0 then Exit;
iPwdChar:=SendMessage(HWnd, EM_GETPASSWORDCHAR, 0, 0);
if iPwdChar<>0 then
begin
iPwdLast := 0;
i := 0;
while iPwdLast = 0 do
begin
PostMessage(HWnd, EM_SETPASSWORDCHAR, 0, 0);
Application.ProcessMessages;
Inc(i);
iPwdLast := SendMessage(HWnd, EM_GETPASSWORDCHAR, 0, 0);
if i > 100 then break;
end ;
SendMessage(HWnd, WM_GETTEXT, MAXBYTE, Longint(@psText));
Result := StrPas(psText);
SendMessage(HWnd, EM_SETPASSWORDCHAR, iPwdChar, 0);
end
else begin
SendMessage(HWnd, WM_GETTEXT, MAXBYTE, Longint(@psText));
Result := StrPas(psText);
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
MousePoint: TPoint;
WindowHandle: THandle;
//WindowStyle: Longint;
begin
// 获取鼠标位置
GetCursorPos(MousePoint);
// 获取包含鼠标的窗口句柄
WindowHandle := WindowFromPoint(MousePoint);
// 去掉密码属性
Edit1.Text:=GetWindowText(WindowHandle,true);
end;
end.