高分求助,已知一个窗体的句柄,如何得到这个窗体中某个控件的句柄?

天锋 2007-05-11 12:35:41
在网上看到一些资料,需要将鼠标拖上去可以得到该句柄,但是下次重启的时候句柄就变掉了。我想让程序直接得到它的句柄该如何做啊?


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

...全文
656 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
UltraBejing 2008-05-01
  • 打赏
  • 举报
回复
我也想了解,谢谢LZ.
hpygzhx520 2007-09-18
  • 打赏
  • 举报
回复
这个 天锋 ……
一点信用都没有,不说也罢。我在网络上接触过他
嗷嗷叫的老马 2007-09-12
  • 打赏
  • 举报
回复
强制结帖!!!!

这100应该给暴风老大
luoxinxin1988 2007-05-26
  • 打赏
  • 举报
回复
学习
嗷嗷叫的老马 2007-05-22
  • 打赏
  • 举报
回复
......................................................
beal_p 2007-05-17
  • 打赏
  • 举报
回复
学习~~
fishmans 2007-05-17
  • 打赏
  • 举报
回复
QQ的文本输入框放在第四层容器里,一层层找吧~~
rainstormmaster 2007-05-13
  • 打赏
  • 举报
回复
这个问题,可能是控件处于某个容器之中,需要这样解决:
dim mhwnd as long
mhwnd =GetDlgItem(窗体句柄, 容器的控件id)
mhwnd=GetDlgItem(mhwnd, 控件id)

也就是说,用GetDlgItem你也需要从上至下,一级级的查找
天锋 2007-05-13
  • 打赏
  • 举报
回复
我试了下,打开QQ的对话框
kk = &H3EA0
可以得到句柄,为什么
kk = &HE39
不能得到句柄呢?而且得不到的占多数,返回值为0
天锋 2007-05-12
  • 打赏
  • 举报
回复
http://www.tf163.com/33.bmp
这个是我用spy取的控件的图。
天锋 2007-05-12
  • 打赏
  • 举报
回复
我把kk改成了等于 &h3e9
没有提示溢出错误了,但是返回值是0
rainstormmaster 2007-05-12
  • 打赏
  • 举报
回复
晕啊,改成这样:
Private Sub Command2_Click()
Dim numberhwnd As Long
Dim kk As long

If qqhwnd <> 0 Then
kk = &H3e9
numberhwnd = GetDlgItem(qqhwnd, kk) '得到用户号码框的名柄
Text2.Text = cstr(numberhwnd)
'DoEvents这句不用加
End If

End Sub

另外,你要确认qqhwnd为模块级变量
天锋 2007-05-12
  • 打赏
  • 举报
回复
Private Declare Function GetDlgItem Lib "user32" (ByVal hDlg As Long, ByVal nIDDlgItem As Long) As Long
Private Declare Function SendMessage& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any)
Private Declare Function SendMessageBynum& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long)
Private Declare Function SendMessageByString& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String)
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public str As String, nmb As String, qqhwnd As Long, pwdhwnd As Long, temp As Long, numberhwnd As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long




Private Sub Command1_Click()
str = String(80, 0)
qqhwnd = FindWindow(vbNullString, "Fetion 2006 Beta")
Text1.Text = Hex(qqhwnd)
End Sub



Private Sub Command2_Click()
Dim numberhwnd As Long
Dim kk As String

If qqhwnd <> 0 Then
kk = "3e9"
numberhwnd = GetDlgItem(qqhwnd, kk) '得到用户号码框的名柄
Text2.Text = numberhwnd
DoEvents
End If

End Sub


一用就溢出。。。。。。。。。。。。
chenhui530 2007-05-11
  • 打赏
  • 举报
回复
提示你知道了主句柄还可以用EnumChildWindows函数来完成更简单些,加上GetClassName函数基本上就搞定了
chenhui530 2007-05-11
  • 打赏
  • 举报
回复
这是前几天给别人类似的回答可以完成你的要求

首先用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
rainstormmaster 2007-05-11
  • 打赏
  • 举报
回复
//如何用spy++得到控件的ID呢?spy++我还不是很熟悉用,能讲讲吗

看他的帮助文件,不懂英文的话,可以看他的中文使用手册
还想懒够 2007-05-11
  • 打赏
  • 举报
回复
使用FindWindowEx这个API也可以获取
天锋 2007-05-11
  • 打赏
  • 举报
回复
如何用spy++得到控件的ID呢?spy++我还不是很熟悉用,能讲讲吗
rainstormmaster 2007-05-11
  • 打赏
  • 举报
回复
声明:
private Declare Function GetDlgItem Lib "user32" (ByVal hDlg As Long, ByVal nIDDlgItem As Long) As Long
调用:
dim mhwnd as long
mhwnd=GetDlgItem(窗口句柄,控件ID号)
rainstormmaster 2007-05-11
  • 打赏
  • 举报
回复
先用spy++看看控件有没有id,如果有的话,不用枚举,用findwindow结合 GetDlgItem 就足够了
加载更多回复(1)

1,486

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧