请帮忙看看这段程序,为什么我不能得到指定edit控件中的内容?(100分)

  • 主题发起人 主题发起人 螃蟹
  • 开始时间 开始时间

螃蟹

Unregistered / Unconfirmed
GUEST, unregistred user!
目的:通过得到鼠标所指的edit控件的内容
程序创建一个窗口。在窗体上按下鼠标后,开始获得鼠标所指的任何窗口的句柄。
在某个edit控件上释放鼠标后得到那个edit控件的文字。


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Label2: TLabel;
Edit2: TEdit;
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
//加粗当前鼠标移过的窗口
procedure InvertTracker(hwndDest : Integer);
end;

var
Form1: TForm1;
mlngHwndCaptured: Integer;
hWndLast: Integer;
implementation

{$R *.dfm}

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if SetCapture(handle) <> 0 then // 开始捕捉
Cursor := crUpArrow;
end;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var pt : TPoint;
begin
if GetCapture() <> 0 then // 处于捕捉状态
begin
pt.X := X;
pt.Y := Y;
getcursorpos(pt);
// ClientToScreen(pt); // 获得鼠标的屏幕位置
// 获得窗口句柄
mlngHwndCaptured := WindowFromPoint(pt);

if hWndLast <> mlngHwndCaptured then
begin
if hWndLast <> 0 then // 使窗口边框加粗
InvertTracker(hWndLast);
InvertTracker(mlngHwndCaptured);
hWndLast := mlngHwndCaptured;
end
end;
// 显示坐标和窗口句柄
Caption := 'X: ' + IntToStr(x) + ', Y: ' + IntToStr(y)
+ ', hWnd: ' + IntToStr(mlngHwndCaptured);

end;
procedure TForm1.InvertTracker(hwndDest: Integer);
var
hdcDest, hPen, hOldPen, hOldBrush : Integer;
cxBorder, cxFrame, cyFrame, cxScreen, cyScreen, cr : Integer;
rc : TRect;
Const NULL_BRUSH = 5;
Const R2_NOT = 6;
Const PS_INSIDEFRAME = 6;
begin
cxScreen := GetSystemMetrics(0);
cyScreen := GetSystemMetrics(1);
cxBorder := GetSystemMetrics(5);
cxFrame := GetSystemMetrics(32);
cyFrame := GetSystemMetrics(33);
GetWindowRect(hwndDest, rc);

hdcDest := GetWindowDC(hwndDest);

SetROP2(hdcDest, R2_NOT);
cr := clBlack;
hPen := CreatePen(PS_INSIDEFRAME, 3 * cxBorder, cr);

hOldPen := SelectObject(hdcDest, hPen);
hOldBrush := SelectObject(hdcDest, GetStockObject(NULL_BRUSH));
Rectangle(hdcDest, 0, 0, rc.Right - rc.Left, rc.Bottom - rc.Top);
SelectObject(hdcDest, hOldBrush);
SelectObject(hdcDest, hOldPen);

ReleaseDC(hwndDest, hdcDest);
DeleteObject(hPen);
end;
procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var strCaption: PChar;
pass,classname:array [0..254] of char;
lens,style_mh:longint;
begin
If mlngHwndCaptured <> 0 Then
begin // 获得窗口标题
strCaption := StrAlloc(1000);
GetWindowText(mlngHwndCaptured, strCaption, 1000);
Caption := StrPas(strCaption);
InvalidateRect(0, PRect(0), True);
mlngHwndCaptured := 0;
Cursor := crDefault;
ReleaseCapture;
StrDispose(strCaption);
hWndLast := 0;
/////////////////////////////////////////////////////////////////
//取得文字
/////////////////////////////////////////////////////////////////
getclassname(mlngHwndCaptured,classname,sizeof(classname));
lens:=sendmessage(mlngHwndCaptured,wm_gettextlength,0,0)+1;
sendmessage(mlngHwndCaptured,wm_gettext,lens,longint(@pass));//取得文字
edit1.Text:=string(pass);
end
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Left := 0;
Top :=0;
ClientHeight := 76;
end;

end.
 
你是不是想得到密码?
 
没有我只是想试试那些个api,为要密码做准备而已。呵呵
 
请注意你的操作系统,win2000把这个功能屏蔽了,请在win98下试.
 
在Windows 2000中,对有密码保护风格的Edit control调用SendMessage(hwnd, WM_GETTEXT,...)试图得到密码内容时,
系统会检查调用SendMessage的进程和Edit control所在的进程是否相同,不同则返回空字符串,调用失败

我自己回答吧。嗨。。。。。。。。。。。。。。
 
接受答案了.
 
后退
顶部