请问各位大虾?如何获得任意窗体的句柄???比如将鼠标指象某一个窗体就能得到他的名称

解决方案 »

  1.   

    类似VC++中的SPY++,当然简单多了:) 
    新建工程,窗体上放置一个Button,一个Edit.使用方法:先点击button,然后点击想要得到句柄的窗体.下面是代码
    unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type 
      TForm1 = class(TForm) 
        Button1: TButton; 
        Edit1: TEdit; 
        procedure Button1Click(Sender: TObject); 
        procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); 
      private 
      { Private declarations } 
      public 
      { Public declarations } 
      end; var Form1: TForm1; implementation {$R *.DFM} 
    procedure TForm1.Button1Click(Sender: TObject); 
    begin 
      setcapture(handle); // 设置捕获鼠标输入 
    end; procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); 
    var point : TPoint; 
        hwnd : THandle; 
    begin 
      point.x := x; 
      point.y := y; 
      point := ClientToScreen(point); // 客户区坐标转换到屏幕坐标 
      hwnd := WindowFromPoint(point); // 取鼠标点击的窗体句柄 
      ReleaseCapture; // 终止捕获鼠标输入 
      if hwnd=handle then edit1.text := '没有点击其他窗体!' 
      else 
        edit1.Text := inttostr(hwnd); // 将捕捉到的窗体句柄显示在edit1中
    end; end.  
      

  2.   

    使用勾子:
    procedure TForm1.Button1Click(Sender: TObject);
    var lppe: TProcessEntry32;
        found : boolean;
        Hand : THandle;
    begin
      Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
      found := Process32First(Hand,lppe);
      if found then
      begin
        found := Process32Next(Hand,lppe);
      end;
    end;
    或作用下面的方法:
    procedure GetOtherText;
    var TextLen: Integer;
        TextStr: PChar;
        PassHandle1,PassHandle:Hwnd;
        PointPos: TPoint;
    begin
     try
      GetCursorPos(PointPos);
      PassHandle:=WindowFromPoint(PointPos);
      if PassHandle=NULL then exit;
      TextLen:=GetWindowTextLength(PassHandle)+1;  //得到名字长度,并将长度加1
      if TextLen <= 1 then exit;
      GetMem(TextStr, 255);
      try
        SendMessage(PassHandle, WM_GETTEXT, 255, Longint(TextStr));
        Label1.Caption := (TextStr);
      finally
        FreeMem(TextStr);
      end;
     except
     end;
    end;