首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

高分,已知一个窗体的句柄,怎么得到这个窗体中某个控件的句柄

2012-01-16 
高分求助,已知一个窗体的句柄,如何得到这个窗体中某个控件的句柄?在网上看到一些资料,需要将鼠标拖上去可

高分求助,已知一个窗体的句柄,如何得到这个窗体中某个控件的句柄?
在网上看到一些资料,需要将鼠标拖上去可以得到该句柄,但是下次重启的时候句柄就变掉了。我想让程序直接得到它的句柄该如何做啊?


有高手提供下详细点的代码不?



[解决办法]
这是前几天给别人类似的回答可以完成你的要求

首先用findwindow找到窗体句柄
然后用我写的这个函数“FindControlHwndByClsName”找到combobox的句柄
然后用sendmessage 发送cb_findstring消息如果返回非-1就可以再用sendmessage发送cb_selectstring就可以了

Option Explicit
Private Const GW_HWNDNEXT = 2
Private Const GW_CHILD = 5
Public Declare Function EnumWindows Lib "user32 " (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
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 GetWindowTextLength Lib "user32 " Alias "GetWindowTextLengthA " (ByVal hwnd As Long) As Long
Public Declare Function FindWindow Lib "user32 " Alias "FindWindowA " (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindow Lib "user32 " (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetClassName Lib "user32 " Alias "GetClassNameA " (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32 " (ByVal hwnd As Long, lpdwProcessId As Long) As Long

Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim h As Long, strArr() As String, pid As Long, i As Integer
If InStr(GetWindowCaption(hwnd), "辉煌在线管理团队 - 群 ") Then
FindControlHwndByClsName hwnd, "SysListView32 ", h
GetWindowThreadProcessId hwnd, pid
If h <> 0 Then
strArr = GetListViewTextArray(h, pid)
For i = 0 To UBound(strArr)
MsgBox strArr(i)
Next
End If
End If
EnumWindowsProc = True
End Function

Private Function GetWindowCaption(ByVal hwnd As Long) As String
Dim strText As String, ret As Long
ret = GetWindowTextLength(hwnd)
If ret > 0 Then
strText = Space(ret)
GetWindowText hwnd, strText, ret + 1
strText = Left(strText, ret)
GetWindowCaption = strText
Else
GetWindowCaption = " "
End If
End Function

Private Function FindControlHwndByCaption(ByVal nHwnd As Long, ByVal findStr As String, outHwnd As Long)
Dim fHwnd As Long, myStr As String, sHwnd As Long
fHwnd = GetWindow(nHwnd, GW_CHILD)
If fHwnd = 0 Then Exit Function
Do While fHwnd > 0
myStr = String(100, Chr$(0))
GetWindowText fHwnd, myStr, 100

If Left(myStr, InStr(myStr, Chr$(0)) - 1) = findStr Then
outHwnd = fHwnd
Exit Function
End If
sHwnd = GetWindow(fHwnd, GW_CHILD)
If sHwnd > 0 Then
FindControlHwndByCaption fHwnd, findStr, outHwnd
End If
fHwnd = GetWindow(fHwnd, GW_HWNDNEXT)
Loop
End Function

Private Function FindControlHwndByClsName(ByVal nHwnd As Long, ByVal clsName As String, outHwnd As Long)
Dim fHwnd As Long, myStr As String, sHwnd As Long, ret As Long, iss As Boolean
fHwnd = GetWindow(nHwnd, GW_CHILD)
If fHwnd = 0 Then Exit Function
Do While fHwnd > 0
myStr = String(100, Chr$(0))
GetClassName fHwnd, myStr, 100
If Left(myStr, InStr(myStr, Chr$(0)) - 1) = clsName Then


outHwnd = fHwnd
Exit Function
End If
sHwnd = GetWindow(fHwnd, GW_CHILD)
If sHwnd > 0 Then
FindControlHwndByClsName fHwnd, clsName, outHwnd
End If
fHwnd = GetWindow(fHwnd, GW_HWNDNEXT)
Loop
End Function
[解决办法]
提示你知道了主句柄还可以用EnumChildWindows函数来完成更简单些,加上GetClassName函数基本上就搞定了
[解决办法]
先用spy++看看控件有没有id,如果有的话,不用枚举,用findwindow结合 GetDlgItem 就足够了
[解决办法]
声明:
private Declare Function GetDlgItem Lib "user32 " (ByVal hDlg As Long, ByVal nIDDlgItem As Long) As Long
调用:
dim mhwnd as long
mhwnd=GetDlgItem(窗口句柄,控件ID号)

热点排行