用鼠标获取任意窗口的句柄, 并把它当作子窗体

Posted 每天进步一点点……

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用鼠标获取任意窗口的句柄, 并把它当作子窗体相关的知识,希望对你有一定的参考价值。

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Edit1: TEdit;
    Button1: TButton;
    Button2: TButton;
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

$R *.dfm

这里主要是设置控件位置
procedure TForm1.FormCreate(Sender: TObject);
begin
  Panel1.Align := alTop;
  Panel1.Height := 30;
  Edit1.Top := 4;
  Edit1.Left := 12;
  Edit1.Width := 72;
  Edit1.Clear;
  Button1.Top := Edit1.Top - 1;
  Button1.Left := Edit1.Left + Edit1.Width + 12;
  Button1.Caption := \'确认捕获, 用回车键\';
  Button1.Width := Canvas.TextWidth(Button1.Caption) + 32;
  Button1.Default := True;
  Button2.Top := Button1.Top;
  Button2.Left := Panel1.ClientWidth - Button2.Width - 12;
  Button2.Anchors := [akRight];
  Button2.Caption := \'过来吧你\';
  Timer1.Interval := 100;
end;

捕获窗口句柄, 显示在标题
procedure TForm1.Timer1Timer(Sender: TObject);
var
  pt: TPoint;
begin
  if Boolean(GetCursorPos(pt)) then
    Text := IntToStr(WindowFromPoint(pt));
end;

把确认的目标窗口的句柄显示在 Edit1
procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit1.Text := Self.Text;
end;

捕获了干什么呢? 譬如让它当自己的"干儿子"吧
procedure TForm1.Button2Click(Sender: TObject);
var
  h: HWND;        这是被捕捉窗口的句柄
  r: TRect;       这是被捕捉窗口的矩形
  pw,ph: Integer; 这是被捕捉窗口的宽度与高度
begin
  h := StrToInt(Edit1.Text);
  Windows.SetParent(h, Self.Handle);
  GetWindowRect(h,r);
  pw := r.Right - r.Left;
  ph := r.Bottom - r.Top;
  MoveWindow(h, 0, Panel1.Height, pw, ph, True);
end;

end.

vb如何获取到窗口的句柄

如果我用getwindow找到窗口的hdc属性,但是我想要它的hwnd,怎么办呢?
求教。。

参考技术A 获取句柄不一定要用 FindWindow ,还可以枚举:

Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Private Const GW_HWNDFIRST = 0
Private Const GW_HWNDNEXT = 2

Private Sub Command1_Click()
Dim lngHwnd As Long, ret As Long
Dim s As String * 255
Dim sName As String

lngHwnd = GetWindow(Me.hwnd, GW_HWNDFIRST)
Do While lngHwnd
ret = GetWindowText(lngHwnd, s, 255)
sName = Blank(s)
If InStr(sName, "记事本") <> 0 Then '这里“记事本”替换成程序标题相同的部分
'这里写 将句柄加入数组的相关代码
End If
lngHwnd = GetWindow(lngHwnd, GW_HWNDNEXT)
Loop
End Sub

Public Function Blank(ByVal szString As String) As String
Dim l As Integer

l = InStr(szString, Chr(0))
If l > 0 Then
Blank = Left(szString, l - 1)
Else
Blank = szString
End If
End Function
参考技术B GetWindow怎么会找到hdc呢?他的返回值就是hwnd啊。
============
话说回来,如果你已知一个hdc想要找他的hwnd,用WindowFromDC函数。本回答被提问者采纳

以上是关于用鼠标获取任意窗口的句柄, 并把它当作子窗体的主要内容,如果未能解决你的问题,请参考以下文章

C# winform鼠标移动到窗口给窗口加边框并获得句柄

如何获取窗体中组件的句柄

C#获取当前窗体句柄及该窗体里的信息

VC++中怎样获取到一个窗体的句柄?

如何用获取当前鼠标所在窗口的句柄及窗口标题用c或c++?

C#获取当前窗体句柄及该窗体里的信息