螃
螃蟹
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
+ ', 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.
程序创建一个窗口。在窗体上按下鼠标后,开始获得鼠标所指的任何窗口的句柄。
在某个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
+ ', 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.