如何更改另一个应用程序的组合框选择值? Visual Basic

时间:2016-03-25 04:10:16

标签: vb.net visual-studio winapi combobox

我知道如何使用以下代码点击其他应用程序的按钮。但现在我需要知道如何更改组合框的选定值。

'Declaration
Private Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Private Declare Auto Function FindWindowEx Lib "user32.dll" (ByVal hWndParent As IntPtr, ByVal hWndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
Declare Auto Function PostMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByRef lParam As IntPtr) As IntPtr
Private Const BM_CLICK = &HF5

'Usage
Dim ButtonHandle As IntPtr
Dim MainWindowHandle As IntPtr
MainWindowHandle = FindWindow(FormClass, FormCaption)
ButtonHandle = FindWindowEx(MainWindowHandle, IntPtr.Zero, TargetClass, TargetCaption)
If ButtonHandle <> 0 Then
    PostMessage(ButtonHandle, BM_CLICK, 0, 0)
End If

我需要做的就是将不同应用程序的组合框选定值从其默认值Off更改为On值。很感谢任何形式的帮助。谢谢。

1 个答案:

答案 0 :(得分:1)

最后我现在知道如何做到这一点。我使用的代码如下所示。

'Declaration
Private Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Private Declare Auto Function FindWindowEx Lib "user32.dll" (ByVal hWndParent As IntPtr, ByVal hWndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
Declare Auto Function PostMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByRef lParam As IntPtr) As IntPtr

Private Const BM_CLICK = &HF5
Private Const CB_SETCURSEL = &H14E

'Usage
Dim MainWindowHandle As IntPtr
Dim ChildAfter As IntPtr
Dim ComboBoxHandle As IntPtr
'Get the Handle
MainWindowHandle = FindWindow(FormClass, FormCaption)
'Get the ChildAfter of the Combo Box
ChildAfter = FindWindowEx(MainWindowHandle, IntPtr.Zero, ChildClass, ChildCaption)
'Get the handle of the combobox dropdown
ComboBoxHandle = FindWindowEx(MainWindowHandle, ChildAfter, "ComboBox", vbNullString)
'Select combo box index(1)
PostMessage(ComboBoxHandle, CB_SETCURSEL, 1, 0)

来自MSDN documentation of FindWindowEx

  

hwndChildAfter [in,optional]

     

类型:HWND

     

子窗口的句柄。 搜索从Z顺序中的下一个子窗口开始。子窗口必须是hwndParent的直接子窗口,而不仅仅是后代窗口。

     

如果hwndChildAfter为NULL,则搜索从hwndParent的第一个子窗口开始。

@RemyLebeau非常感谢你帮助我做到这一点。