在外部应用程序 VS 2012 中查看复选框的选中状态
Viewing checkedstatus of checkbox in external application VS 2012
构建一个工具来自动与海量数据库的前端界面进行交互。通过在选中和未选中之间切换复选框并点击保存来修改许多字段。无法进入数据库本身直接进行更新或查看设置,一切都必须通过前端应用程序。
勾选框一点也不麻烦,只是一个简单的 Sendmessage(hndl, BM_CLICK, 0, 0)
但问题是这只是切换框。我需要确保它被选中,如果我发送点击一个已经选中的框,它会取消选中它。
我试过使用 BM_SETCHECK
强制检查状态,但没有效果。我还尝试使用 BM_GETCHECK
和 BM_GETSTATE
来判断在发送我的 BM_CLICK 命令之前该框是否已经被选中。不幸的是,我创建的用于检查状态的函数总是 returns false。
Imports System.Runtime.InteropServices
Imports vb = Microsoft.VisualBasic
Public Class ucCheckBoxChecker
Private Const BM_CLICK As Integer = &HF5
Private Const BST_CHECKED As Integer = &H1
Private Const BM_GETSTATE As Integer = &HF2
Private Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, _
ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
Private Declare Function SendMessageA Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
ByVal lParam As String) As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim hndlCheckBox As Integer = FindWindowEx(hndlParent, 0, "WindowsForms10.BUTTON.app.0.141b42a_r11_ad1", vbNullString)
If IsChecked(hndlCheckBox) = False Then SendMessageA(hndlCheckBox, BM_CLICK, 0, 0)
End Sub
Public Function IsChecked(ByVal hWnd As Integer) As Boolean
Dim intHndlVal As Integer
intHndlVal = SendMessage(hWnd, BM_GETSTATE, 0, 0)
Debug.Print(intHndlVal)
IsChecked = ((intHndlVal And BST_CHECKED) = BST_CHECKED)
End Function
End Class
我已经验证句柄是正确的,但无论我尝试什么,我似乎都无法让 intHndlVal
成为除 0 以外的任何值。
您可以尝试使用 AutomationElement class。
https://msdn.microsoft.com/en-us/library/system.windows.automation.automationelement(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/ms750424(v=vs.110).aspx
下面的代码是 c#,但您应该可以使用在线代码转换器将其转换为 vb.net。我从上面列出的网站之一获取了 IsElementToggledOn 代码。
AutomationElement element = AutomationElement.FromHandle( hwnd );
/// <summary>
/// Gets the toggle state of an element in the target application.
/// </summary>
/// <param name="element">The target element.</param>
private bool IsElementToggledOn(AutomationElement element)
{
if (element == null)
{
// TODO: Invalid parameter error handling.
return false;
}
Object objPattern;
TogglePattern togPattern;
if (true == element.TryGetCurrentPattern(TogglePattern.Pattern, out objPattern))
{
togPattern = objPattern as TogglePattern;
return togPattern.Current.ToggleState == ToggleState.On;
}
// TODO: Object doesn't support TogglePattern error handling.
return false;
}
构建一个工具来自动与海量数据库的前端界面进行交互。通过在选中和未选中之间切换复选框并点击保存来修改许多字段。无法进入数据库本身直接进行更新或查看设置,一切都必须通过前端应用程序。
勾选框一点也不麻烦,只是一个简单的 Sendmessage(hndl, BM_CLICK, 0, 0)
但问题是这只是切换框。我需要确保它被选中,如果我发送点击一个已经选中的框,它会取消选中它。
我试过使用 BM_SETCHECK
强制检查状态,但没有效果。我还尝试使用 BM_GETCHECK
和 BM_GETSTATE
来判断在发送我的 BM_CLICK 命令之前该框是否已经被选中。不幸的是,我创建的用于检查状态的函数总是 returns false。
Imports System.Runtime.InteropServices
Imports vb = Microsoft.VisualBasic
Public Class ucCheckBoxChecker
Private Const BM_CLICK As Integer = &HF5
Private Const BST_CHECKED As Integer = &H1
Private Const BM_GETSTATE As Integer = &HF2
Private Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, _
ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
Private Declare Function SendMessageA Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
ByVal lParam As String) As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim hndlCheckBox As Integer = FindWindowEx(hndlParent, 0, "WindowsForms10.BUTTON.app.0.141b42a_r11_ad1", vbNullString)
If IsChecked(hndlCheckBox) = False Then SendMessageA(hndlCheckBox, BM_CLICK, 0, 0)
End Sub
Public Function IsChecked(ByVal hWnd As Integer) As Boolean
Dim intHndlVal As Integer
intHndlVal = SendMessage(hWnd, BM_GETSTATE, 0, 0)
Debug.Print(intHndlVal)
IsChecked = ((intHndlVal And BST_CHECKED) = BST_CHECKED)
End Function
End Class
我已经验证句柄是正确的,但无论我尝试什么,我似乎都无法让 intHndlVal
成为除 0 以外的任何值。
您可以尝试使用 AutomationElement class。
https://msdn.microsoft.com/en-us/library/system.windows.automation.automationelement(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/ms750424(v=vs.110).aspx
下面的代码是 c#,但您应该可以使用在线代码转换器将其转换为 vb.net。我从上面列出的网站之一获取了 IsElementToggledOn 代码。
AutomationElement element = AutomationElement.FromHandle( hwnd );
/// <summary>
/// Gets the toggle state of an element in the target application.
/// </summary>
/// <param name="element">The target element.</param>
private bool IsElementToggledOn(AutomationElement element)
{
if (element == null)
{
// TODO: Invalid parameter error handling.
return false;
}
Object objPattern;
TogglePattern togPattern;
if (true == element.TryGetCurrentPattern(TogglePattern.Pattern, out objPattern))
{
togPattern = objPattern as TogglePattern;
return togPattern.Current.ToggleState == ToggleState.On;
}
// TODO: Object doesn't support TogglePattern error handling.
return false;
}