VB.net findwindow / findwindowex

时间:2010-10-11 02:13:28

标签: vb.net visual-studio-2008 findwindow

嘿所有,我试图找出当控件名称与程序中所有其他标签相同时如何找到此窗口的标签。

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

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

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

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

大卫

2 个答案:

答案 0 :(得分:0)

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

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

不太明显的答案(如果Windows API对您的要求过于繁琐)将使用Microsoft UI Automation API

答案 1 :(得分: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