![](/img/trans.png)
[英]Retrieve a ComboBox counts and items by using SendMessage API
[英]sendmessage api to select a specific index of combobox control
我正在編寫一個應用程序,它可以選擇另一個應用程序的具有特定索引的ComboBox
。 例如,我想通過掛鈎從我的應用程序中選擇第二個列出的項目“Adobe flash player”。
ComboBox
應用程序不是我的,因此我無法修改目標應用程序。
通常,在 VB.Net 中使用Sendmessage
API 可以完成放置文本或單擊按鈕。
可以檢索該ComboBox
的句柄值 (hWnd)。 我想知道要使用哪個函數 (api) 以及應該使用哪個值。
謝謝你。
您可以將CB_SETCURSEL
消息發送到組合框。 SendMessage
的wParam
參數應該是要設置為選定索引的項目的從零開始的索引, lParam
在這里無用。
應用程序發送
CB_SETCURSEL
消息以選擇組合框列表中的字符串。 如有必要,列表會將字符串滾動到視圖中。 組合框的編輯控件中的文本會發生變化以反映新的選擇,並且刪除列表中任何先前的選擇。
wParam
:指定要選擇的字符串的從零開始的索引。 如果此參數為 –1,則刪除列表中的任何當前選擇並清除編輯控件。lParam
:不使用此參數。
C# 示例
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
const int CB_SETCURSEL = 0x014E;
void SetSelectedIndex(IntPtr comboBoxHandle, int index)
{
SendMessage(comboBoxHandle, CB_SETCURSEL, index, 0);
}
VB.NET 范例
<System.Runtime.InteropServices.DllImport("user32.dll")> _
Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, _
ByVal wParam As Integer, ByVal lParam As Integer) As IntPtr
End Function
Const CB_SETCURSEL As Integer = &H14E
Sub SetSelectedIndex(comboBoxHandle As IntPtr, index As Integer)
SendMessage(comboBoxHandle, CB_SETCURSEL, index, 0)
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.