用
FindWindow Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
获得句柄
如知道进程ID 用OpenProcess
Public Declare Function OpenProcess Lib "kernel32" Alias "OpenProcess" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
HWND FindWindowA(类名,标题);返回HWND句柄
HWND FindWindowExA(HWND hwndParent,//要查找子窗口的父窗口句柄。
//如果hwndParent为0,则函数以桌面窗口为父窗口,查找桌面窗口的所有子窗口。
HWND hwndChildAfter,//子窗口句柄。如果HwndChildAfter为0,查找从hwndParent的第一个子窗口开始。
LPCTSTR lpszClass, //指向一个指定了类名
LPCTSTR lpszWindow);//指向一个指定了窗口标题
其中一个应用程序的类名和标题,可以由vs自带的spy++工具来获得
可以用
FindWindow(//获取指定窗口名称句柄
LPCSTR
lpClassName
,//要获得窗口的类型,一般为NULL
LPCSTR
lpWindowName);//要获得窗口的句柄
如:
HWND
hwnd
=
FindWindow(NULL,"计算器");
VB的话得先申明一下函数
第一步:获取目标窗口句柄
首先引用命名空间:
using SystemRuntimeInteropServices; [DllImport("user32dll", EntryPoint = "FindWindow")]public static extern IntPtr FindWindow(
string lpClassName,
string lpWindowName
);
利用FindWindow获得目标窗口句柄
第一个参数是类名,第二个参数是窗口原来的标题
以下代码则是获得目标窗口代码:
IntPtr window = FindWindow(null,"Microsoft SQL Server Management Studio");//我这里是以SQL为例第二步:改变窗口标题
[DllImport("user32dll", EntryPoint = "SetWindowText")]public static extern int SetWindowText(
IntPtr hwnd,
string lpString
);
以下代码则是改变目标句柄的窗口标题:
SetWindowText(window,"你好啊");Ok,窗口标题成功修改了!!!!
附加根据进程名称修改标题:
Process [] ps= ProcessGetProcessesByName("Ssms");//根据进程名称获得进程数组foreach(Process p in ps)//遍历进程
{
SetWindowText(pMainWindowHandle, "Microsoft SQL Server Management Studio免费共享版");
}
windows下获取当前进程的话可以用dos命令tasklist
1
2
3
4
5
6
7
Runtime r=RuntimegetRuntime();
Process p=rexec("cmd /C tasklist");
BufferedReader reader=new BufferedReader(new InputStreamReader(pgetInputStream(),"gbk"));//windows的默认系统中文编码是gbk所以从cmd控制台的信息已gbk来解码
String line=null;
while((line=readerreadLine())!=null)
Systemoutprintln(line);
以上就是关于获得句柄全部的内容,包括:获得句柄、C++ 怎么在程序中获得 另外一个程序的句柄、VB如何取得程序的句柄等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)