如何通过句柄获取外部程序的窗口的内容

合集下载

.net 通过句柄获取窗口信息的方法

.net 通过句柄获取窗口信息的方法

.net 通过句柄获取窗口信息的方法嘿,朋友们!今天咱就来唠唠.net 通过句柄获取窗口信息这档子事儿。

咱就说啊,这窗口就好比是一个神秘的小盒子,里面装着各种信息呢。

而句柄呢,就像是打开这个小盒子的钥匙。

你想想,要是没有这把钥匙,你咋能知道盒子里有啥宝贝呀!
要通过句柄获取窗口信息,首先你得找到那个对的句柄呀。

这就跟找宝藏似的,得有耐心,还得有那么点技巧。

你得知道从哪儿开始找,怎么找才能又快又准。

然后呢,当你拿到句柄了,就可以开始施展魔法啦!你可以获取窗口的标题呀,大小呀,位置呀等等这些信息。

就好像你打开盒子后,能清楚地看到里面的各种宝贝一样。

比如说,你可以用它来看看某个程序的窗口标题是不是你想要的。

要是不对,那你就知道可能出问题啦!或者你想知道这个窗口在屏幕上的位置,好安排其他东西的摆放,这多方便呀!
哎呀,这可真是太有用了!你能想象如果没有这种方法,我们得费多大劲儿才能知道这些信息吗?那可真是像无头苍蝇一样乱撞呀!
在实际操作中呢,可不能马虎。

就像做饭一样,调料放多了放少了味道都不对。

你得小心谨慎地处理每一个步骤,不然可能就得不到你想要的结果啦。

而且呀,这还能帮我们解决一些看似很难的问题呢。

比如有时候程序出了点小毛病,通过获取窗口信息,说不定就能找到问题所在,然后轻松解决啦!
总之呢,.net 通过句柄获取窗口信息这招可真是太绝啦!它就像我们的秘密武器,能帮我们在编程的世界里披荆斩棘。

大家可得好好掌握呀,别浪费了这么好的工具!这就是我想说的,大家觉得怎么样呢?是不是很有道理呀!。

利用句柄查出进程的详细信息

利用句柄查出进程的详细信息

//利用句柄查出进程的详细信息#include <windows.h>#include <tlhelp32.h>#include <iostream.h>#pragma comment(lib, "kernel32.lib")//当在用户模式机内核模式下都提供所耗时间时,在内核模式下进行所耗时间的64位计算的帮助方法DWORD GetKernelModePercentage(const FILETIME &ftKernel, //在核心状态下消耗的时间const FILETIME &ftUser)//在用户状态下消耗的时间{//将FILETIME结构化为64位整数ULONGLONG qwKernel = (((ULONGLONG) ftKernel.dwHighDateTime)<<32) + ftKernel.dwLowDateTime;ULONGLONG qwUser = (((ULONGLONG) ftUser.dwHighDateTime)<<32) + ftUser.dwLowDateTime;//将消耗时间相加,然后计算消耗在内核模式下的时间百分比ULONGLONG qwTotal = qwKernel + qwUser;DWORD dwPct = (DWORD)(((ULONGLONG) 100*qwKernel) / qwTotal);return(dwPct);}//以下是将当前运行进程名和消耗在内核模式下的时间百分比分数都显示出来的应用程序void main(){HANDLE hSnapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,//提取当前进程0);//如果是当前进程,就将其忽略//初始化进程入口PROCESSENTRY32 pe; //用来存放快照进程信息的一个结构体,引用所需包含的头文件#include “tlhelp32.h”::ZeroMemory(&pe,sizeof(pe));//为pe分配存储空间,并将其清0pe.dwSize = sizeof(pe);//按所有进程循环BOOL bMore = ::Process32First(hSnapshot, &pe);//获得进程快照中的第一个进程信息while(bMore){//打开用于读取的进程HANDLE hProcess = ::OpenProcess(PROCESS_QUERY_INFORMA TION,//指明要得到的信息FALSE,//不必继承这一句柄pe.th32ProcessID);//要打开的进程if(hProcess!=NULL){//找出进程的时间FILETIME ftCreation, ftExit, ftKernelMode, ftUserMode;::GetProcessTimes(hProcess,//所感兴趣的进程&ftCreation,//进程的启动时间(绝对的)&ftExit,//结束时间(如果有的话)&ftKernelMode,//在内核模式下消耗的时间&ftUserMode);//用户模式下消耗的时间//计算内核模式消耗的时间百分比DWORD dwPctKernel = ::GetKernelModePercentage(ftKernelMode,//内核模式上消耗的时间ftUserMode);//在用户模式下消耗的时间//向用户显示进程的某些信息cout<<"Process ID:"<<pe.th32ProcessID<<",EXE file: "<<pe.szExeFile<<", % in kernel mode:"<<dwPctKernel<<endl;//消除句柄::CloseHandle(hProcess);}//转向下一个进程bMore = ::Process32Next(hSnapshot, &pe);}}。

C#利用句柄操作窗口

C#利用句柄操作窗口

C#写个类操作窗口(句柄操作)实现过程:过程一:找到当前鼠标位置的句柄您的使用2个WinAPI(俺喜欢自己封装下来用):View Code[DllImport("user32.dll", EntryPoint = "GetCursorPos")] public static extern bool GetCursorPos(out Point pt);[DllImport("user32.dll", EntryPoint = "WindowFromPoint")] public static extern IntPtr WindowFromPoint(Point pt);//鼠标位置的坐标public static Point GetCursorPosPoint(){Point p = new Point();if (GetCursorPos(out p)){return p;}return default(Point);}///<summary>///找到句柄///</summary>///<param name="p">坐标</param>///<returns></returns>public static IntPtr GetHandle(Point p){return WindowFromPoint(p);}过程二:改变窗口的Text您的使用1个WinAPI:View Code[DllImport("user32.dll", EntryPoint = "SendMessage")] private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);///<summary>///给窗口发送内容///</summary>///<param name="hWnd">句柄</param>///<param name="lParam">要发送的内容</param>public static void SetText(IntPtr hWnd, string lParam){SendMessage(hWnd, WM_SETTEXT, IntPtr.Zero, lParam);}private const int WM_SETTEXT = 0x000C;通过这个方法就能改变Text的值了。

利用句柄操作窗口

利用句柄操作窗口

利用句柄操作窗口
要利用句柄操作窗口,可以使用以下步骤:
1. 获取窗口句柄:使用FindWindow或FindWindowEx函数来查找窗
口句柄。

这些函数接受窗口类名、窗口标题等参数,根据参数信息找到对
应窗口的句柄。

2. 操作窗口:通过窗口句柄可以执行多种操作,如最小化、最大化、还原、关闭窗口等。

可以使用ShowWindow函数来改变窗口的可见状态,
使用SendMessage或PostMessage函数发送消息给窗口来模拟按键操作或
执行其他更复杂的任务。

3. 获取窗口属性:通过窗口句柄可以获取窗口的属性,如位置、大小、标题等。

可以使用GetWindowRect函数来获取窗口的坐标信息,使用GetWindowText函数来获取窗口的标题。

需要注意的是,操作窗口需要有足够的权限,有些窗口可能受到系统
保护,无法直接操作。

此外,窗口句柄是一个唯一标识符,所以需要确保
获得的句柄是正确的窗口句柄才能进行操作。

vba读取外部程序窗体内容

vba读取外部程序窗体内容

vba读取外部程序窗体内容要读取外部程序窗体的内容,你可以使用Windows API函数来实现。

下面是一个示例代码,可以读取外部程序的窗体标题:vbaOption ExplicitPrivate 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 nMaxCount As Long) As LongSub GetExternalWindowContent()Dim externalWindowhWnd As LongDim windowTitle As StringDim windowText As String' 获取外部程序窗口句柄externalWindowhWnd = FindWindow(vbNullString, "外部程序窗口标题")If externalWindowhWnd <> 0 Then' 获取窗口标题windowTitle = Space(255)GetWindowT ext externalWindowhWnd, windowTitle,Len(windowTitle)windowTitle = Left(windowTitle, InStr(windowTitle, vbNullChar) - 1)' 获取窗口内容windowText = Space(4096)GetWindowT ext externalWindowhWnd, windowText,Len(windowText)windowText = Left(windowText, InStr(windowText, vbNullChar) - 1)MsgBox "窗口标题: " & windowTitle & vbNewLine & vbNewLine & "窗口内容: " & windowTextElseMsgBox "外部程序窗口未找到"End IfEnd Sub示例代码中的`FindWindow`函数用于获取外部程序窗口的句柄,`GetWindowText`函数用于获取窗口的标题和内容。

python获取应用程序窗口程序内容的方法

python获取应用程序窗口程序内容的方法

python获取应用程序窗口程序内容的方法
在Python中,可以使用第三方库来获取应用程序窗口的内容。

其中最常用的是pyautogui和pygetwindow。

1. pyautogui
pyautogui是一个Python模块,可以模拟键盘和鼠标操作,也可以截取屏幕上的图像。

使用pyautogui可以截取应用程序窗口的图像,并保存为文件。

具体步骤如下:
```python
import pyautogui
截取整个屏幕的图像并保存为文件
().save('')
截取特定窗口的图像并保存为文件
首先需要找到窗口的句柄或标题,可以使用pygetwindow模块来获取import pygetwindow as gw
window = ()
screenshot = (region=(, , , ))
('window_')
```
2. pygetwindow
pygetwindow是一个Python模块,可以获取应用程序窗口的信息,包括窗口的句柄、标题、大小、位置等。

具体步骤如下:
```python
import pygetwindow as gw
获取当前活动窗口的信息
window = ()
print() 输出窗口标题
print() 输出窗口左侧位置
print() 输出窗口顶部位置
print() 输出窗口宽度
print() 输出窗口高度。

c#获取当前活动窗口句柄,获取窗口大小及位置

c#获取当前活动窗口句柄,获取窗口大小及位置

c#获取当前活动窗⼝句柄,获取窗⼝⼤⼩及位置需调⽤API函数需在开头引⼊命名空间using System.Runtime.InteropServices;获取当前窗⼝句柄:GetForegroundWindow()[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]public static extern IntPtr GetForegroundWindow();返回值类型是IntPtr,即为当前获得焦点窗⼝的句柄使⽤⽅法 : IntPtr myPtr=GetForegroundWindow();获取到该窗⼝句柄后,可以对该窗⼝进⾏操作.⽐如,关闭该窗⼝或在该窗⼝隐藏后,使其显⽰[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);其中ShowWindow(IntPtr hwnd, int nCmdShow);nCmdShow的含义0 关闭窗⼝1 正常⼤⼩显⽰窗⼝2 最⼩化窗⼝3 最⼤化窗⼝使⽤实例: ShowWindow(myPtr, 0);获取窗⼝⼤⼩及位置:需要调⽤⽅法GetWindowRect(IntPtr hWnd, ref RECT lpRect)[DllImport("user32.dll")][return: MarshalAs(UnmanagedType.Bool)]static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);[StructLayout(LayoutKind.Sequential)]public struct RECT{public int Left; //最左坐标public int Top; //最上坐标public int Right; //最右坐标public int Bottom; //最下坐标}⽰例:InPtr awin = GetForegroundWindow(); //获取当前窗⼝句柄RECT rect = new RECT();GetWindowRect(awin, ref rect);int width = rc.Right - rc.Left; //窗⼝的宽度int height = rc.Bottom - rc.Top; //窗⼝的⾼度int x = rc.Left;int y = rc.Top;------------------------------------------------------------------------C#中的FindWindow[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint="FindWindow")]public static extern int FindWindow (string lpClassName,string lpWindowName);已知窗⼝标题"abc",怎么得到窗⼝句柄?IntPtr hWnd = FindWindow(null, "abc");-------------------------------------------------------C#使⽤FindWindow()函数:[DllImport("coredll.dll", EntryPoint = "FindWindow")]private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);这个函数有两个参数,第⼀个是要找的窗⼝的类,第⼆个是要找的窗⼝的标题。

易语言获取窗口信息的方法(一)

易语言获取窗口信息的方法(一)

易语言获取窗口信息的方法(一)易语言获取窗口信息的方法在易语言中,我们可以通过一些方法来获取窗口的信息。

下面是一些常用的方法和函数,供您参考。

方法一:使用窗口标题获取窗口句柄使用窗口标题可以获取到指定窗口的句柄(handle)。

以下是获取窗口句柄的函数:hwnd = 资源.查找窗口(窗口标题)将窗口标题替换为您要查找的窗口的标题。

该函数会返回一个窗口句柄,您可以将其保存在一个变量中以便后续使用。

方法二:使用窗口类名获取窗口句柄与方法一类似,您还可以通过窗口类名来获取窗口句柄。

以下是获取窗口句柄的函数:hwnd = 资源.查找窗口类(窗口类名)将窗口类名替换为您要查找的窗口的类名。

同样,该函数会返回一个窗口句柄。

获取窗口的位置和大小可以帮助我们确定窗口在屏幕上的位置以及窗口的大小。

以下是获取窗口位置和大小的函数:x, y, width, height = 资源.获取窗口位置和大小(hwnd)将 hwnd 替换为您要获取位置和大小的窗口的句柄。

函数会返回窗口的横坐标、纵坐标、宽度和高度,分别保存在 x、y、width 和height 变量中。

方法四:获取窗口的标题获取窗口的标题可以用于确认窗口的具体信息。

以下是获取窗口标题的函数:标题 = 资源.获取窗口标题(hwnd)将 hwnd 替换为您要获取标题的窗口的句柄。

函数会返回窗口的标题,保存在标题变量中。

方法五:获取窗口的进程ID获取窗口的进程ID可以用于确定窗口所属的进程。

以下是获取窗口进程ID的函数:进程ID = 资源.获取窗口进程ID(hwnd)将 hwnd 替换为您要获取进程ID的窗口的句柄。

函数会返回窗口所属的进程ID,保存在进程ID变量中。

获取窗口的父窗口句柄可以用于确定窗口的层次结构。

以下是获取窗口父窗口句柄的函数:父窗口句柄 = 资源.获取窗口父窗口句柄(hwnd)将 hwnd 替换为您要获取父窗口句柄的窗口的句柄。

函数会返回窗口的父窗口句柄,保存在父窗口句柄变量中。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

如何通过句柄获取外部程序的窗口的内容Option Explicit'常数申明Public Const LVM_FIRST As Long = &amp;H1000Public Const LVM_GETHEADER As Long = LVM_FIRST + 31Public ConstLVM_GETITEMCOUNT As Long = LVM_FIRST + 4Public Const LVM_GETITEMTEXT As Long = LVM_FIRST +45Public Const HDM_FIRST As Long =&amp;H1200Public Const HDM_GETITEMCOUNT As Long = (HDM_FIRST + 0)Public ConstPROCESS_VM_OPERATION As Long = &amp;H8Public Const PROCESS_VM_READ As Long = &amp;H10Public Const PROCESS_VM_WRITE As Long = &amp;H20Public Const MAX_LVMSTRING As Long = 255Public Const MEM_COMMIT As Long = &amp;H1000Public Const MEM_RELEASE As Long = &amp;H8000&amp;Public Const PAGE_READWRITE As Long = &amp;H4Public Const LVIF_TEXT As Long = &amp;H1'类型申明Public Type LV_ITEMA mask As Long iItemAs Long iSubItem As Long State As Long stateMask As Long pszText As Long cchTextMax As LongEnd Type'API申明PublicDeclare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long '打开进程Public Declare Function VirtualAllocEx Lib "kernel32" (ByVal hProcess As Long, ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, _ByVal flProtect As Long) As Long '获取内存空间Public Declare Function VirtualFreeEx Lib "kernel32" (ByVal hProcess As Long, ByVal lpAddress As Long, ByVal dwSize As Long, ByVal dwFreeType As Long) As Long '释放内存空间Public Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByRef lpBuffer As LV_ITEMA, ByVal nSize As Long, _lpNumberOfBytesWritten As Long) As Long '向内存写数据Public Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByRef lpBuffer As Any, ByVal nSize As Long, _lpNumberOfBytesWritten As Long) As Long '向内存读数据Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg AsLong, ByVal wParam As Long, ByRef lParam As Any) As Long '发送消息Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long '关闭进程Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long '获得进程IDPublic Function GetListViewTextArray(ByVal hWindow As Long) As String() Dim myItem() As LV_ITEMA Dim PHandleAs Long Dim ProcessId As Long Dim PStrBufferMemory As Long Dim PMyItemMemoryAs Long Dim StrBuffer(MAX_LVMSTRING) As ByteDim TmpString As String Dim Ih As Long, J As Long, HCount As Long Dim StrArr() As String, ItemString As String Dim Ji As Long, MyItemLength() As Long GetWindowThreadProcessId hWindow, ProcessId HCount = SendMessage(hWindow, LVM_GETHEADER, 0, 0) '获取列数If HCount &gt; 0 Then HCount = SendMessage(HCount, HDM_GETITEMCOUNT, 0, 0) - 1 Else 'NOT HCOUNT... HCount = 0 End If PHandle = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, 0, ProcessId) ReDim myItem(HCount) ReDimMyItemLength(HCount) PStrBufferMemory = VirtualAllocEx(PHandle, 0, MAX_LVMSTRING,MEM_COMMIT, PAGE_READWRITE)PMyItemMemory = VirtualAllocEx(PHandle, 0,MAX_LVMSTRING, MEM_COMMIT, PAGE_READWRITE) Ji = SendMessage(hWindow, LVM_GETITEMCOUNT, 0, 0) - 1 On Error GoTo err1 ReDim StrArr(Ji)For Ih = 0 To HCount myItem(Ih).mask =LVIF_TEXT myItem(Ih).iSubItem = IhmyItem(Ih).pszText = PStrBufferMemorymyItem(Ih).cchTextMax = MAX_LVMSTRING MyItemLength(Ih) = Len(myItem(Ih)) Next IhFor J = 0 To Ji ItemString = "" For Ih = 0 To HCount WriteProcessMemory PHandle, PMyItemMemory, myItem(Ih), MyItemLength(Ih), 0If SendMessage(hWindow, LVM_GETITEMTEXT, J, ByVal PMyItemMemory) &gt; 0 ThenReadProcessMemory PHandle, PStrBufferMemory, StrBuffer(0), MAX_LVMSTRING, 0TmpString = StrConv(StrBuffer, vbUnicode)TmpString = Left(TmpString, InStr(TmpString, vbNullChar) - 1) ItemString = ItemString &amp;TmpString &amp; Chr(9) ' Chr$(32) End If Next Ih If ItemString &lt;&gt; "" ThenStrArr(J) = Left(ItemString, Len(ItemString) - 1)End If Next J VirtualFreeEx PHandle, PMyItemMemory, 0, MEM_RELEASE VirtualFreeEx PHandle, PStrBufferMemory, 0, MEM_RELEASE CloseHandle (PHandle) ItemString = "" GetListViewTextArray = StrArr Exit Functionerr1: MsgBox "不是Listview类吧?", vbInformationEnd Function。

相关文档
最新文档