很多API函数需要什么什么句柄
请问句柄怎么获取?
请写一个例子看下:_)
感谢了
很通俗的跟你解释一下
系统里有很多东西,这些东西都有一个身份证
身份证就是“句柄”(也不知道谁给取的这个奇怪的名字)
不是说一个程序 只有一个句柄
句柄绝对比你想象的要多的多 也就是说 分的很细
最简单的例子,打开系统自带的计算器
上面每一个按钮 那些什么1234 加减乘除 每个按钮都有自己的身份证
这样一来 你就可以很有针对性的对系统中的某一个“东西” 来进行操作了。
应该能理解句柄是怎么回事了吧?
获取句柄 最基本的 两条思路,一就是那个你很感兴趣的“东西”,它自己就有一个属性(一般属性名都是 Hwnd),里面就存放着他自己的句柄,比如你输入 Msgbox Me.Hwnd 运行一下 就可以获得当前窗体的句柄
二就是 你通过一些专门设计出来,用来获得XX东西句柄的API函数
那个你查阅下API的相关书籍 就可以了,
VB里面 最简单的句柄么 就是窗体本身自己的句柄咯
最简单的代码 上面已经说了 就是这么一句话 Msgbox Me.Hwnd 你可以去试试
系统里有很多东西,这些东西都有一个身份证
身份证就是“句柄”(也不知道谁给取的这个奇怪的名字)
不是说一个程序 只有一个句柄
句柄绝对比你想象的要多的多 也就是说 分的很细
最简单的例子,打开系统自带的计算器
上面每一个按钮 那些什么1234 加减乘除 每个按钮都有自己的身份证
这样一来 你就可以很有针对性的对系统中的某一个“东西” 来进行操作了。
应该能理解句柄是怎么回事了吧?
获取句柄 最基本的 两条思路,一就是那个你很感兴趣的“东西”,它自己就有一个属性(一般属性名都是 Hwnd),里面就存放着他自己的句柄,比如你输入 Msgbox Me.Hwnd 运行一下 就可以获得当前窗体的句柄
二就是 你通过一些专门设计出来,用来获得XX东西句柄的API函数
那个你查阅下API的相关书籍 就可以了,
VB里面 最简单的句柄么 就是窗体本身自己的句柄咯
最简单的代码 上面已经说了 就是这么一句话 Msgbox Me.Hwnd 你可以去试试
温馨提示:答案为网友推荐,仅供参考
第1个回答 2009-02-02
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 * 1024
End Type
Const TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPTHREAD = &H4
Const TH32CS_SNAPMODULE = &H8
Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Const TH32CS_INHERIT = &H80000000
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
'查找进程的函数
Private Function fun_FindProcess(ByVal ProcessName As String) As Long
Dim strdata As String
Dim my As PROCESSENTRY32
Dim l As Long
Dim l1 As Long
Dim mName As String
Dim i As Integer, pid As Long
l = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If l Then
my.dwSize = 1060
If (Process32First(l, my)) Then
Do
i = InStr(1, my.szExeFile, Chr(0))
mName = LCase(Left(my.szExeFile, i - 1))
If mName = LCase(ProcessName) Then
pid = my.th32ProcessID
fun_FindProcess = pid
Exit Function
End If
Loop Until (Process32Next(l, my) < 1)
End If
l1 = CloseHandle(l)
End If
fun_FindProcess = 0
End Function
Private Sub Form_Load()
if fun_FindProcess("123.exe") <> 0 then
Text1.Text="123.exe程序已启动"
Else
Text1.Text = ""
End If
End Sub
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 * 1024
End Type
Const TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPTHREAD = &H4
Const TH32CS_SNAPMODULE = &H8
Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Const TH32CS_INHERIT = &H80000000
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
'查找进程的函数
Private Function fun_FindProcess(ByVal ProcessName As String) As Long
Dim strdata As String
Dim my As PROCESSENTRY32
Dim l As Long
Dim l1 As Long
Dim mName As String
Dim i As Integer, pid As Long
l = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If l Then
my.dwSize = 1060
If (Process32First(l, my)) Then
Do
i = InStr(1, my.szExeFile, Chr(0))
mName = LCase(Left(my.szExeFile, i - 1))
If mName = LCase(ProcessName) Then
pid = my.th32ProcessID
fun_FindProcess = pid
Exit Function
End If
Loop Until (Process32Next(l, my) < 1)
End If
l1 = CloseHandle(l)
End If
fun_FindProcess = 0
End Function
Private Sub Form_Load()
if fun_FindProcess("123.exe") <> 0 then
Text1.Text="123.exe程序已启动"
Else
Text1.Text = ""
End If
End Sub