
我只学过Delphi,C++和VB函数差不多,
定义:
Handle : HWND;//记录句柄
GetClassName(Handle,arr,length(arr));//获取当前句柄类名
Handle := WindowFromPoint(CurPos);//获取当前鼠标坐标
如果是静态获取可以遍历子窗口控件的类名,然后获取句柄 答案补充 hHandle:=findWindow(nil,'窗口名');
1、启动VS,新建C# WinForm项目。
2、在Form1中添加4个Label控件,并布局如下。
3、在Form1中添加代码,如下。
4、完成之后,调试运行,结果如下。
注意事项:
C++不仅拥有计算机高效运行的实用性特征,同时还致力于提高大规模程序的编程质量与程序设计语言的问题描述能力。
获取鼠标位置处窗口句柄,需要使用到Win32Api函数WindowFromPoint,用来根据坐标获取窗口句柄,C#引用如下:
[DllImport("user32dll", EntryPoint = "WindowFromPoint")]//指定坐标处窗体句柄
public static extern int WindowFromPoint(
int xPoint,
int yPoint
);
只要能够获取鼠标的位置,然后调用该函数就可以得到窗口句柄。
在VC的窗口类中有一成员变量:m_hWnd
,它代表这个窗口的句柄
。因此在VC中通过一些得到窗口指针的函数,然后再访问它的成员变量,应该可以得到所要的句柄。
比如用这个函数得到窗口指针,然后访问它的m_hWnd
。
AfxGetMainWnd(
);
获取所有句柄,源码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
btn_listwindow: TButton;
procedure btn_listwindowClick(Sender: TObject);
private
public
end;
var
Form1: TForm1;
function EnumWindowsProc(AhWnd:LongInt;AForm:TForm1):boolean;stdcall;
implementation
function EnumWindowsProc(AhWnd:LongInt;AForm:TForm1):boolean;
var
lpszClassName,lpszWindowText:array[0254] of char;
begin
GetWindowText(AhWnd,lpszWindowText,254);
GetClassName(AhWnd,lpszClassName,254);
Aformmemo1linesadd(StrPas(lpszWindowText));
Aformmemo1linesadd(StrPas(lpszClassName));
Aformmemo1linesadd( '-------------------- ');
Result:=True;
end;
procedure TForm1btn_listwindowClick(Sender: TObject);
begin
EnumWindows(@EnumWindowsProc,LongInt(self));
end;
end
F9,运行,看看结果。最好是F7单步跟踪调试一下,看看回调函数是怎么被调用的。
获取句柄不一定要用 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(Mehwnd, 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
以上就是关于如何获取绘图工具的句柄全部的内容,包括:如何获取绘图工具的句柄、C++中如何获取当前窗口句柄、怎么用C#获取指定窗口的句柄等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)