VB获取制定进程的窗口名字

VB获取制定进程的窗口名字,第1张

因为一个程序的hwnd可能会有很多所以除了你现在所要求的有text1 text2 和command1之外 还得再加一个list1 用来保存所有的hwnd恩至于哪个hwnd是主窗口你可能得自己想想方法了 窗体中的代码 Option Explicit Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Const MAX_PATH As Integer = 260 Private Type PROCESSENTRY32 dwSize As Long cntUsage As Long th32ProcessID As Long th32DefaultHeapID As Long th32ModuleID As Long cntThreads As Long th32ParentProcessID As Long pcPriClassBase As Long dwFlags As Long szExeFile As String MAX_PATH End Type Const TH32CS_SNAPheaplist = &H1 Const TH32CS_SNAPPROCESS = &H2 Const TH32CS_SNAPthread = &H4 Const TH32CS_SNAPmodule = &H8 Const TH32CS_SNAPall = TH32CS_SNAPPROCESS + TH32CS_SNAPheaplist + TH32CS_SNAPthread + TH32CS_SNAPmodule Private Sub Command1_Click() Dim i As Long, lPid As Long Dim Proc As PROCESSENTRY32 Dim hSnapShot As Long Dim temp As String Dim Pid As String hSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPall, 0) '获得进程"快照"的句柄 ProcdwSize = Len(Proc) lPid = ProcessFirst(hSnapShot, Proc) '获取第一个进程的PROCESSENTRY32结构信息数据 i = 0 Do While lPid <> 0 '当返回值非零时继续获取下一个进程 temp = ProcszExeFile '取得将进程名 If Left(temp, Len(Text1Text)) = Text1Text Then Pid = Procth32ProcessID '进程ID End If i = i + 1 lPid = ProcessNext(hSnapShot, Proc) '循环获取下一个进程的PROCESSENTRY32结构信息数据 Loop CloseHandle hSnapShot '关闭进程"快照"句柄 If Pid <> 0 Then '找到进程pid的话 Text2Text = Pid Find_Window Val(Text2Text) End If End Sub Private Sub Form_Load() End Sub 模块中的代码 Option Explicit Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId 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 Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long Dim IfPid As Long Private Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long Dim Pid1 As Long Dim wText As String 255 GetWindowThreadProcessId hwnd, Pid1 If IfPid = Pid1 Then GetWindowText hwnd, wText, 100 If GetParent(hwnd) = 0 And Len(Replace(wText, Chr(0), "")) <> 0 Then Form1List1AddItem "句柄:" & hwnd & " 标题:" & wText End If EnumWindowsProc = True End Function Public Sub Find_Window(ByVal Pid As Long) IfPid = Pid EnumWindows AddressOf EnumWindowsProc, 0 End Sub

当前窗口有一个(多个)数据窗口,只是想得到当前的数据窗口(得到焦点的数据窗口)可以用如下办法:

1、先在窗口里里面声明一个实例变量: datawindow idw_cur

2、如果窗口里面有三个数据窗口,分别在三个数据窗口的getfocus事件里面都写上同样的代码 idw_cur=this

3、这样的话通过idw_cur就可以得到当前的数据窗口了,通过ClassName(idw_cur)就可以得到它的数据窗口名字了。

给你个思路:

1、在Main函数中定义一个全局变量userName,用来存放用户名;

2、在登录窗体中,读取用户名并存放到userName中;

3、那么打开主窗体,在主窗体中就可以调用这个全局变量userName并在适当的控件中显示这个“用户名”,当然也可以写入数据库。ok

以上就是关于VB获取制定进程的窗口名字全部的内容,包括:VB获取制定进程的窗口名字、PB 如何获得窗口中当前datawindow的名称、C#中获取登录窗口的用户名等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9803257.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-02
下一篇 2023-05-02

发表评论

登录后才能评论

评论列表(0条)

保存