
获取句柄不一定要用 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
应该可以用:
HWND GetForegroundWindow(void);
MSDN里的解释:
GetForegroundWindow
This function returns the handle to the foreground window—the window with which the user is currently working
HWND GetForegroundWindow(void);
Return Values
The handle to the foreground window indicates success
百度百科里的解释:
GetForegroundWindow
函数功能:该函数返回前台窗口(用户当前工作的窗口)。系统分配给产生前台窗口的线程一个稍高一点的优先级。
函数原型:HWND GetForegroundWindow(VOID)
参数:无。 返回值:函数返回前台窗回的句柄。
速查:Windows NT:31以上版本;Windows:95以上版本:Windows CE:10以上版本:头文件:Winuserh;库文件:user32lib。
希望是你想要的答案
一个网上的例子
c# 获取鼠标处窗口句柄,程序嵌入桌面
using System;
using SystemCollectionsGeneric;
using SystemComponentModel;
using SystemData;
using SystemDrawing;
using SystemText;
using SystemWindowsForms;
using SystemRuntimeInteropServices;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32dll", EntryPoint = "FindWindow")]
public static extern int FindWindow(
string lpClassName,
string lpWindowName
);
[DllImport("user32dll", EntryPoint = "GetWindow")]//获取窗体句柄,hwnd为源窗口句柄
/wCmd指定结果窗口与源窗口的关系,它们建立在下述常数基础上:
GW_CHILD
寻找源窗口的第一个子窗口
GW_HWNDFIRST
为一个源子窗口寻找第一个兄弟(同级)窗口,或寻找第一个顶级窗口
GW_HWNDLAST
为一个源子窗口寻找最后一个兄弟(同级)窗口,或寻找最后一个顶级窗口
GW_HWNDNEXT
为源窗口寻找下一个兄弟窗口
GW_HWNDPREV
为源窗口寻找前一个兄弟窗口
GW_OWNER
寻找窗口的所有者
/
public static extern int GetWindow(
int hwnd,
int wCmd
);
[DllImport("user32dll", EntryPoint = "SetParent")]//设置父窗体
public static extern int SetParent(
int hWndChild,
int hWndNewParent
);
[DllImport("user32dll", EntryPoint = "GetCursorPos")]//获取鼠标坐标
public static extern int GetCursorPos(
ref POINTAPI lpPoint
);
[StructLayout(LayoutKindSequential)]//定义与API相兼容结构体,实际上是一种内存转换
public struct POINTAPI
{
public int X;
public int Y;
}
[DllImport("user32dll", EntryPoint = "WindowFromPoint")]//指定坐标处窗体句柄
public static extern int WindowFromPoint(
int xPoint,
int yPoint
);
private void timer1_Tick(object sender, EventArgs e)
{
POINTAPI point = new POINTAPI();//必须用与之相兼容的结构体,类也可以
GetCursorPos(ref point);//获取当前鼠标坐标
int hwnd = WindowFromPoint(pointX, pointY);//获取指定坐标处窗口的句柄
thislabel1Text =pointXToString() + ":" + pointYToString() + "-" + hwndToString();//显示效果,此时窗口已经嵌入桌面了
}
const int GW_CHILD = 5;//定义窗体关系
private void Form1_Load(object sender, EventArgs e)
{
int hDesktop = FindWindow("Progman", null);//获取系统句柄
hDesktop = GetWindow(hDesktop, GW_CHILD);//获取其子窗口句柄,就是桌面的句柄
SetParent((int)thisHandle, hDesktop);//设置父窗体,第一个为要被设置的窗口,第二个参数为指定其父窗口句柄
}
}
}
首先要获得窗口的句柄 用下面的函数应该能找到:
FindWindow根据类名\窗口标题寻找窗口
遍历顶层窗口EnumWindows
遍历窗口回调函数EnumWindowProc
遍历父窗口的所有子窗口EnumChildWindows
返回父窗口Point处的子窗口ChildWindowFromPoint
ChildWindowFromPointEx多一个参数UINT来忽略不可见无效透明的子窗口
获取与指定窗口具有莫种关系的窗口GetWindow
获取父窗口的子窗口中Z序最大的子窗口GetTopWindow
获取指定窗口相同层次Z序差1的窗口GetNextWindow
获取桌面窗口句柄GetDesktopWindow
用SendMessage函数不需要窗口标题的,只要句柄
LRESULT SendMessage(
HWND hWnd, // handle of destination window
UINT Msg, // message to send
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
或者用keybd_event函数,要先用SetForegroundWindow函数把目标窗口设置成前台窗口
VOID keybd_event(
BYTE bVk, // virtual-key code
BYTE bScan, // hardware scan code
DWORD dwFlags, // flags specifying various function options
DWORD dwExtraInfo // additional data associated with keystroke
);
补充:
第一个:我进入某个游戏,用CE查到内存地址后,退出游戏,再查内存地址,两个内存地址是不一样的,有什么办法可以让每次的内存地址都一样?或者说下别的解决方法?
不让每次的内存地址都一样应该是做不到的,游戏每次运行由系统分配内存,哪能让你控制啊。
第二个:我想让鼠标在内存数值到某一个数值时,鼠标移动到某点进行鼠标 *** 作,该怎么写源代码?
定义一个指针变量p,类型根据你需要的数值而定
p=该内存地址
if(p==该数值)
{
//mousemove
}
以上就是关于vb如何获取到窗口的句柄全部的内容,包括:vb如何获取到窗口的句柄、win32 dll获取应用程序窗口句柄、C#怎样获得窗口句柄等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)