3

大家好,我想弄清楚当控件名称与程序上的所有其他标签相同时如何查找此窗口的标签。

WindowsForms10.STATIC.app.0.378734a
WindowsForms10.STATIC.app.0.378734a
WindowsForms10.STATIC.app.0.378734a

所有 3 个标签的名称都相同。我最感兴趣的是进度百分比计数器(1%、2%、3% 等)

我如何在任何给定时间不知道它的标题的情况下从该标签中获取值(当然使用计时器)?

任何帮助都会很棒!:o)

大卫

4

2 回答 2

0

显而易见的答案是从所有三个标签中获取文本并检查哪一个看起来像“1%”、“55%”等。

If strText Like "#%" Or strText Like "##%" Or strText = "100%" Then
' ...

不太明显的答案(如果 Windows API 对您的要求来说太麻烦)是使用Microsoft UI Automation API

于 2010-10-15T00:56:59.363 回答
0

不确定您是否只是在寻找更完整的代码示例,但您可以去。

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    'This block of code creates a list of all the labels on my form.
    'Replace this with code to get a list of labels on the form you are scraping
    Dim LblList As New List(Of Label)

    For Each ctrl As Control In Me.Controls
        If TypeOf ctrl Is Label Then
            LblList.Add(CType(ctrl, Label))
        End If
    Next
    'End

    Dim ProgressLblTxt As String = String.Empty
    For Each lbl As Label In LblList
        If lbl.Text.Contains("%") Then 'You could use several different criteria here as mentioned in the previous answer
            ProgressLblTxt = lbl.Text
        End If

        If ProgressLblTxt <> String.Empty Then Exit For
    Next

    'Do something with ProgressLblTxt
    MsgBox(ProgressLblTxt)
End Sub
于 2010-10-20T22:07:37.427 回答