句柄 这一概念是MFC里的一个类,MFC有专门的函数来获取窗口句柄。 HWND hwnd; hwnd=CreateWindow("my own windowshandle","jaycnvip",WS_OVERLAPPEDWINDOW,0,0,600,400,NULL,NULL,hInstance,NULL); 这是用一个句柄变量来保存新创建的窗口句柄。
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
'这是定义获得窗口句柄的API函数
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long,lParam As Any) As Long '这句是定义向获得窗口句柄发送按键消息的API函数
例子:你先建一个工程
form1caption="12345
画一个command1 名字为 "确定1"
事件
Private Sub Command1_Click()
msgbox MeHwnd '显示确定按钮的句柄
End Sub
生成12345exe
关闭VB6
打开VB6
再建一个工程 代码如下:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function PostMessage& Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any)
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
Private Const MK_LBUTTON = &H1
Dim hpwnd As Long, hcwnd As Long
Dim iResult As Long
Private Sub Command1_Click()
hpwnd = FindWindow(vbNullString, "12345")
hcwnd = FindWindowEx(hpwnd, 0, vbNullString, "确定")
SetForegroundWindow hcwnd
iResult = SendMessage(hcwnd, WM_LBUTTONDOWN, 0, 0&)
iResult = SendMessage(hcwnd, WM_LBUTTONUP, 0, 0&)
End Sub
然后先运行 事先生成好的12345exe
再运行后建的那个
你单击确定 就会使12345exe的确定按钮被单击 返回它的hwnd
我的方法是先找到12345exe的主窗口 然后根据主窗口的句柄获得其确定按钮的句柄 再使用sendmessage 发送模拟鼠标左键单击
VB控件的句柄 一般可用mehwnd 方法获得
如果窗口是现有程序的,使用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就是你实例化的窗口句柄
你获取句柄的方法好像没什么问题,关键看你取到句柄后具体要做什么 *** 作?
我在MFC 中试了一下,没有问题 ,把桌面关闭掉
HWND desktopHwnd = ::FindWindowEx( ::FindWindowEx( ::FindWindow(L"Progman", L"Program Manager"), NULL, L"SHELLDLL_DefView", NULL), NULL, L"SysListView32", L"FolderView" );::SendMessage( desktopHwnd,WM_CLOSE,0,0 );
1、启动VS,新建C# WinForm项目。
2、在Form1中添加4个Label控件,并布局如下。
3、在Form1中添加代码,如下。
4、完成之后,调试运行,结果如下。
注意事项:
C++不仅拥有计算机高效运行的实用性特征,同时还致力于提高大规模程序的编程质量与程序设计语言的问题描述能力。
以上就是关于如何获取Windows整个系统中当前在最前面的窗口的句柄全部的内容,包括:如何获取Windows整个系统中当前在最前面的窗口的句柄、VB怎样获得桌面指定程序的窗口句柄、如何获取某个应用程序的窗体句柄等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)