通过使用SendMessage API检索ComboBox计数和项目 [英] Retrieve a ComboBox counts and items by using SendMessage API
问题描述
我想获取不是我的 ComboBox
控件的计数和列表,以使我无法修改代码.
I want to get a count and list of ComboBox
control which is not mine so that I cannot modify the code.
例如,可以使用 SendMessage
API来控制目标应用.
For example, controlling the target app can be done by using SendMessage
API.
但是,如何通过钩子检索目标控件的整个列表?
But, how can I retrieve a whole list of the target control by hooking?
推荐答案
您可以在此处找到 ComboBox
控制消息的列表:
You can find a list of ComboBox
control messages here:
要获取项目计数,您需要使用 CB_GETLBTEXT
消息.
To get items count you need to use CB_GETCOUNT
message and to get text of an item you can use CB_GETLBTEXT
message.
示例
这里我创建了一个 ComboBoxHelper
类,您可以通过传递 ComboBox
的 Handle
并使用其属性来创建其实例:
Here I created a ComboBoxHelper
class which you can create its instance by passing Handle
of the ComboBox
and use its properties:
-
SelectedIndex
asInteger
:返回所选索引,如果未选择任何项目,则返回-1. -
Selectedtext
作为String
:返回所选项目的文本,如果未选择任何项目,则返回String.Empty
. -
ItemsCount
作为Integer
:返回项目计数. -
Items(index)
asString
:返回指定项目的文本(指定索引处的项目) -
Items
asList(of String)
:返回组合框的项目列表.如果没有项目,则返回一个空列表.
SelectedIndex
asInteger
: Returns selected index, returns -1 if no item is selected.Selectedtext
asString
: Returns text of selected item, returnsString.Empty
if no item is selected.ItemsCount
asInteger
: returns count of items.Items(index)
asString
: returns text of specified item (the item at specified index)Items
asList(of String)
: returns the list of items of combo box. If there is no items, it returns an empty list.
Public Class ComboBoxHelper
Private hWnd As IntPtr
Const CB_GETCURSEL As Integer = &H147
Const CB_SETCURSEL As Integer = &H14E
Const CB_GETCOUNT As Integer = &H146
Const CB_GETLBTEXT As Integer = &H148
Const CB_GETLBTEXTLEN As Integer = &H149
<System.Runtime.InteropServices.DllImport("user32.dll")> _
Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, _
ByVal wParam As Integer, ByRef lParam As Integer) As IntPtr
End Function
<System.Runtime.InteropServices.DllImport("user32.dll")> _
Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, _
ByVal wParam As Integer, ByVal lParam As System.Text.StringBuilder) As IntPtr
End Function
Public Sub New(handle As IntPtr)
hWnd = handle
End Sub
Public Property SelectedIndex As Integer
Get
Return SendMessage(hWnd, CB_GETCURSEL, 0, 0).ToInt32()
End Get
Set(ByVal value As Integer)
SendMessage(hWnd, CB_SETCURSEL, value, 0).ToInt32()
End Set
End Property
Public ReadOnly Property ItemsCount As Integer
Get
Return SendMessage(hWnd, CB_GETCOUNT, 0, 0).ToInt32()
End Get
End Property
Public ReadOnly Property SelectedText As String
Get
Dim index = Me.SelectedIndex
If (Me.SelectedIndex = -1) Then
Return String.Empty
End If
Return Me.Items(index)
End Get
End Property
Public ReadOnly Property Items() As List(Of String)
Get
If (ItemsCount > 0) Then
Return Enumerable.Range(0, ItemsCount) _
.Select(Function(index) Items(index)).ToList()
Else
Return New List(Of String)
End If
End Get
End Property
Public ReadOnly Property Items(index As Integer) As String
Get
If (index < 0 OrElse index >= ItemsCount) Then
Throw New ArgumentOutOfRangeException("index")
End If
Dim length = SendMessage(hWnd, CB_GETLBTEXTLEN, index, 0).ToInt32()
Dim text As New System.Text.StringBuilder(length)
SendMessage(hWnd, CB_GETLBTEXT, index, text)
Return text.ToString()
End Get
End Property
End Class
以下是该类用法的示例:
Here is an example of usage of the class:
Dim combo As New ComboBoxHelper(hWnd) 'You have hWnd
MessageBox.Show(combo.ItemsCount.ToString())
MessageBox.Show(combo.SelectedIndex.ToString())
MessageBox.Show(combo.SelectedText.ToString())
combo.Items.ForEach(Function(item) MessageBox.Show(item))
这篇关于通过使用SendMessage API检索ComboBox计数和项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!