
一般用FindWindow。。。
Delphi中获取其它进程的窗口句柄,在Delphi中获取其它进程的窗口句柄,绝大部分人首先想到的会使用:FindWindow或者用GetWindow来遍历查找,如:
Delphi/Pascal code
handle := FindWindow(nil,PChar('窗口的标题'));
或者:
Delphi/Pascal code
procedure TForm1Button1Click(Sender: TObject);
var
hCurrentWindow: HWnd;
WndText:String;
begin
hCurrentWindow := GetWindow(Handle, GW_HWNDFIRST);
while hCurrentWindow <> 0 do
begin
WndText:=GetWndText(hCurrentWindow);
if UpperCase(WndText)='窗口的标题' then begin
end;
hCurrentWindow:=GetWindow(hCurrentWindow, GW_HWNDNEXT);
end;
end;
因为目前网络上绝大部分的代码都是介绍用这两种方法取得其它进程的窗口句柄。虽这两种方法都可以达到查找其它进程的窗口句柄的目的,但本人认为这两都方法存在较大的弊端。因为这两种方法都是根据其它进程的标题来查找的,如果其它进程的标题在运行时不断的发生变化,那么这两种方法就无法没办法用了。
介绍第三种通过进程的文件名来查找窗口句柄。首先通过进程快照得到要查找的进程ID(ProcessId),其次,再跟据ProcessId获取进程的窗口句柄。以下为本文章的代码:
Delphi/Pascal code
uses TLHelp32;
procedure TForm1Button1Click(Sender: TObject);
var
ProcessName : string; //进程名
FSnapshotHandle:THandle; //进程快照句柄
FProcessEntry32:TProcessEntry32; //进程入口的结构体信息
ContinueLoop:BOOL;
MyHwnd:THandle;
begin
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); //创建一个进程快照
FProcessEntry32dwSize:=Sizeof(FProcessEntry32);
ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32); //得到系统中第一个进程
//循环例举
while ContinueLoop do
begin
ProcessName := FProcessEntry32szExeFile;
if(ProcessName = '要找的应用程序名exe') then begin
MyHwnd := GetHWndByPID(FProcessEntry32th32ProcessID);
end;
ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;
CloseHandle(FSnapshotHandle); // 释放快照句柄
end;
//跟据ProcessId获取进程的窗口句柄
function TForm1GetHWndByPID(const hPID: THandle): THandle;
type
PEnumInfo = ^TEnumInfo;
TEnumInfo = record
ProcessID: DWORD;
HWND: THandle;
end;
function EnumWindowsProc(Wnd: DWORD; var EI: TEnumInfo): Bool; stdcall;
var
PID: DWORD;
begin
GetWindowThreadProcessID(Wnd, @PID);
Result := (PID <> EIProcessID) or
(not IsWindowVisible(WND)) or
(not IsWindowEnabled(WND));
if not Result then EIHWND := WND;
end;
function FindMainWindow(PID: DWORD): DWORD;
var
EI: TEnumInfo;
begin
EIProcessID := PID;
EIHWND := 0;
EnumWindows(@EnumWindowsProc, Integer(@EI));
Result := EIHWND;
end;
begin
if hPID<>0 then
Result:=FindMainWindow(hPID)
else
Result:=0;
end;
我说的是组件的句柄,不是窗体的句柄
findwindowex();获取指定句柄窗口下的子控件,当然是有句柄的控件
窗体的句柄都知道,还能不知道里面组件的句柄?——windows标准组件
procedure TForm1Button2Click(Sender: TObject);
var
canvas1: TCanvas;
begin
Canvas1 := TCanvasCreate;
//这是OK。这真要感谢baidu,google
canvas1Handle := GetDc(panel1Handle);
canvas1TextOut(1,1,'hello');
canvas1Free ;
Form1CanvasTextOut(10, 10, 'fff');
end;
有的组件是没有句柄的
ShowMessage(IntToStr(TWinControl(Form1FindChildControl('Panel1'))Handle));
C#获取当前窗体句柄及该窗体里的信息可以参考以下的代码:
FindWindow(
lpClassName, {窗口的类名}
lpWindowName: PChar {窗口的标题}
): HWND; {返回窗口的句柄; 失败返回 0}
//FindWindowEx 比 FindWindow 多出两个句柄参数:
FindWindowEx(
Parent: HWND; {要查找子窗口的父窗口句柄}
Child: HWND; {子窗口句柄}
ClassName: PChar; {}
WindowName: PChar {}
): HWND;
扩展资料:
句柄语句
HINSTANCE hInstance;
可以改成:
HANDLE hInstance;
上面的2条语句都是对的,表达的是一个意义
一个WINDOWS应用程序可以用不同的方法获得一个特定项的句柄。许多API函数,诸如CreateWindow,GlobalAlloc,OpenFile的返回值都是一个句柄值。
另外,WINDOWS也能通过应用程序的引出函数将一个句柄作为参数传送给应用程序,应用程序一旦获得了一个确定项的句柄,便可在WINDOWS环境下的任何地方对这个句柄进行 *** 作。其实句柄的大量使用已经影响到了每一个WINDOWS的程序设计。
参考资料来源:百度百科-句柄
看看你身后的质疑,使用ATL。
HWND HWND函数GetDlgItem(IDC_LIST)在,
CListCtrl的LC(HWND); / /或者使用附加要求。
在手柄上一般使用ATL,MFC类指针一般。
例如,函数GetDlgItem返回HWND,ATL,MFC返回的CWnd
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) 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 Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Const GW_HWNDFIRST = 0
Private Const GW_HWNDNEXT = 2
Private Const GW_CHILD = 5
Private Sub Command1_Click()
Call List1_Click
End Sub
Private Sub Command2_Click()
Dim hwnd As Long
Dim s As String, t As String
List1Clear
hwnd = GetDesktopWindow()
s = String(256, Chr(0))
GetClassName hwnd, s, 255
s = Replace(s, Chr(0), "")
t = String(256, Chr(0))
GetWindowText hwnd, t, 255
t = Replace(t, Chr(0), "")
List1AddItem "桌面:" & hwnd & " 类名:" & s & " 标题:" & t & vbCrLf
hwnd = GetWindow(hwnd, GW_CHILD Or GW_HWNDFIRST)
s = String(256, Chr(0))
GetClassName hwnd, s, 255
s = Replace(s, Chr(0), "")
t = String(256, Chr(0))
GetWindowText hwnd, t, 255
t = Replace(t, Chr(0), "")
List1AddItem "窗口:" & hwnd & " 类名:" & s & " 标题:" & t & vbCrLf
While hwnd <> 0
hwnd = GetWindow(hwnd, GW_HWNDNEXT)
s = String(256, Chr(0))
GetClassName hwnd, s, 255
s = Replace(s, Chr(0), "")
t = String(256, Chr(0))
GetWindowText hwnd, t, 255
t = Replace(t, Chr(0), "")
List1AddItem "窗口:" & hwnd & " 类名:" & s & "标题:" & t & vbCrLf
Wend
End Sub
Private Sub Form_Load()
Command1Caption = "获取所有控件"
Command2Caption = "遍历所有窗体"
End Sub
Private Sub EnumAllHandles(ByVal hwnd As Long)
Dim hn As Long
Dim firsthd As Long
Dim s As String, t As String
firsthd = GetWindow(hwnd, GW_CHILD)
firsthd = GetWindow(firsthd, GW_HWNDFIRST)
hn = firsthd
Do While hn <> 0
s = String(256, Chr(0))
GetClassName hn, s, 255
s = Replace(s, Chr(0), "")
t = String(256, Chr(0))
GetWindowText hn, t, 255
t = Replace(t, Chr(0), "")
Text1Text = Text1Text & "句柄:" & hn & " 父句柄:" & hwnd & " 类名:" & s & "标题:" & t & vbCrLf
TreeView1NodesAdd "k" & hwnd, tvwChild, "k" & hn, "句柄:" & hn & " 类名:" & s & "标题:" & t
EnumAllHandles hn
hn = GetWindow(hn, GW_HWNDNEXT)
If hn = firsthd Then Exit Do
Loop
End Sub
Private Sub List1_Click()
If List1ListIndex = -1 Then Exit Sub
TreeView1NodesClear
TreeView1NodesAdd , , "k" & Trim(Str(Val(Mid(List1Text, 4)))), List1Text
Text1Text = ""
EnumAllHandles Val(Mid(List1Text, 4))
TreeView1Nodes("k" & Trim(Str(Val(Mid(List1Text, 4)))))Expanded = True
End Sub
'添加两个按钮一个文本框一个列表框和一个树形图
如果窗口是现有程序的,使用VS自带的spy++获取窗口的相关信息,然後使用WinAPI获取句柄,具体参考spy++的使用方法和winapi的使用
FindWindow(
lpClassName, {窗口的类名}
lpWindowName: PChar {窗口的标题}
): HWND; {返回窗口的句柄; 失败返回 0}
//FindWindowEx 比 FindWindow 多出两个句柄参数:
FindWindowEx(
Parent: HWND; {要查找子窗口的父窗口句柄}
Child: HWND; {子窗口句柄}
ClassName: PChar; {}
WindowName: PChar {}
): HWND;
如果窗口是你的程序动态生成的,使用如下语句
Form _FORM=new Form();
IntPtr _P = _FORMHandle;
_P就是你实例化的窗口句柄
以上就是关于如何使用FindWindowEx获取窗体内控件句柄全部的内容,包括:如何使用FindWindowEx获取窗体内控件句柄、C#获取当前窗体句柄及该窗体里的信息、(VC++ 问题)spy++ 获取了控件句柄,如何提取控件里的数据等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)