2010-10-22 14 views
2

我意识到这是一个非常具体的问题,在这种情况之外并没有什么帮助,尽管我确信它适用于其他问题。我有一个递归搜索窗口(及其子窗口)的函数来查找特定的函数,它的工作原理与预期完全相同,但它会导致“函数不会在所有路径上返回值”警告。这是我整个程序中唯一的警告,虽然它可能很愚蠢,但我有兴趣知道是否有办法阻止这种错误发生,但仍然允许该功能正常工作。如何让此函数不会生成“不会返回所有路径上的值”警告?

Public Function FindQWidgetWindows() As Integer 
    Dim hWndStart As Integer = 0 
    Dim WindowText As String = "*" 
    Dim Classname As String = "QWidget" 
    Dim hwnd As Integer 
    Dim sWindowText As String 
    Dim sClassname As String 
    Dim r As Integer 
    Static level As Integer 

    If level = 0 Then 
     If hWndStart = 0 Then hWndStart = GetDesktopWindow() 
    End If 

    level = level + 1 

    hwnd = GetWindow(hWndStart, GW_CHILD) 

    Do Until hwnd = 0 
     Call FindQWidgetWindows() 

     'Get the window text and class name' 
     sWindowText = Space$(255) 
     r = GetWindowText(hwnd, sWindowText, 255) 
     sWindowText = Microsoft.VisualBasic.Left(sWindowText, r) 
     sClassname = Space$(255) 
     r = GetClassName(hwnd, sClassname, 255) 
     sClassname = Microsoft.VisualBasic.Left(sClassname, r) 

     If (sWindowText Like WindowText) And (sClassname Like Classname) Then 
      Dim aRECT As RECT 
      Dim hwndInt As Int32 = hwnd 
      GetWindowRect(hwndInt, aRECT) 
      FindQWidgetWindows = hwnd 

      'uncommenting the next line causes the routine to' 
      'only return the first matching window.' 
      'Exit Do' 

     End If 

     hwnd = GetWindow(hwnd, GW_HWNDNEXT) 

    Loop 

    level = level - 1 
End Function 
+0

让我给你的意见作出回应,并澄清。首先,感谢您的回答。但是,我明白为什么会发出警告,我只是无法解决警告。 @paxdiablo - 是的,但如果我把它作为一个子,它不会工作 - 事实上它会返回一行“FindQWidgetWindows = hwnd” @Shoban上的错误,这是我想要的行为,如果它发现一个窗口它返回的值,如果它不应该返回任何东西(或者,我想应该是没有返回) – 2010-10-22 06:28:19

回答

4

你依靠的事实,即VB自动声明一个返回变量与您的函数的名称。该变量可以用作函数中的其他变量。所以它也可以得到一个默认的初始化。

如前所述,您只能在非常嵌套的If语句中分配一个值。 你应该只是外面初始化变量,而Do -loop的东西之前,像

FindQWidgetWindows = Nothing 
+0

谢谢,我试图修复(非常)嵌套If的返回,但在循环之前分配Nothing值修复了警告。我很感谢你的回答。 – 2010-10-22 06:34:11

2

是的,你可以通过确保每个路径返回一个值来摆脱这个错误。

这可以通过简单地在函数的顶部初始化返回值来完成:

FindQWidgetWindows = Nothing 

但是你有一个你可能没有看到,因为你想要的窗口是在顶层的另一个问题。如果递归到您的功能,hWndStart将再次被设置为桌面,而不是子窗口。

+0

是的,这就是为什么它是一个函数,它返回父窗口正常递归地工作。 – 2010-10-22 06:29:38

0

在您的代码中,只有满足If条件时才会执行下面的代码。

FindQWidgetWindows = hwnd 

这也意味着如果不满足If条件什么都不会返回。

+0

我最初试图将其改为 FindQWidgetWindows = hwnd Else FindQWidgetWindows = Nothing 但这并没有解决警告。 - 显然我不够聪明在评论中使用代码标签。 – 2010-10-22 06:30:55

0

您已声明函数(Public Function FindQWidgetWindows() As Integer)返回一个整数,但该函数不返回任何内容。只需确保您使用Return语句返回一个整数。

相关问题