0

我试图在最大化或最小化时获取我的应用程序的状态。我重写了脚本,使其特定于 Excel 应用程序。但是,我在这一行遇到错误:

 Private Sub GetWindowStats(Process.GetProcessesByName("Excel")(0).MainWindowHandle)

它说它需要一个逗号或“)”。我不明白为什么它需要一个,即使我放了一个,仍然会出现编译错误。这是我的整个代码:

'Handle the minimize and maximize events on Excel.

Private Const SW_SHOWMAXIMIZED As Integer = 3
Private Const SW_SHOWMINIMIZED As Integer = 2
Private Const SW_SHOWNORMAL As Integer = 1

Private Structure POINTAPI
    Public x As Integer
    Public y As Integer
End Structure

Private Structure RECT
    Public Left As Integer
    Public Top As Integer
    Public Right As Integer
    Public Bottom As Integer
End Structure

Private Structure WINDOWPLACEMENT
    Public Length As Integer
    Public flags As Integer
    Public showCmd As Integer
    Public ptMinPosition As POINTAPI
    Public ptMaxPosition As POINTAPI
    Public rcNormalPosition As RECT
End Structure

Private Declare Function GetWindowPlacement Lib "user32" (ByVal hwnd As IntPtr, ByRef lpwndpl As WINDOWPLACEMENT) As Integer
Private Declare Function GetForegroundWindow Lib "user32" Alias "GetForegroundWindow" () As IntPtr

Private Sub GetWindowStats(Process.GetProcessesByName("Excel")(0).MainWindowHandle)
    Dim wp As WINDOWPLACEMENT
    wp.Length = System.Runtime.InteropServices.Marshal.SizeOf(wp)
    GetWindowPlacement(Handle, wp)
    If wp.showCmd = SW_SHOWMAXIMIZED Then ' is window maximized?
        If Handle = GetForegroundWindow Then ' is the window foreground?
            MessageBox.Show("Maximized and forground")
        Else
            MessageBox.Show("Maximized")
        End If
    ElseIf wp.showCmd = SW_SHOWNORMAL Then
        If Handle = GetForegroundWindow Then
            MessageBox.Show("Normal size and forground")
        Else
            MessageBox.Show("Normal")
        End If
    ElseIf wp.showCmd = SW_SHOWMINIMIZED Then
        MessageBox.Show("Window is Minimized")
    End If
End Sub
4

1 回答 1

1

那是因为它需要此子例程的参数类型而不是值。

Private Sub GetWindowStats(Handle As IntPtr)
 Dim wp As WINDOWPLACEMENT
 wp.Length = System.Runtime.InteropServices.Marshal.SizeOf(wp)
 GetWindowPlacement(Handle, wp)
 If wp.showCmd = SW_SHOWMAXIMIZED Then ' is window maximized?
    If Handle = GetForegroundWindow Then ' is the window foreground?
        MessageBox.Show("Maximized and forground")
    Else
        MessageBox.Show("Maximized")
    End If
 ElseIf wp.showCmd = SW_SHOWNORMAL Then
    If Handle = GetForegroundWindow Then
        MessageBox.Show("Normal size and forground")
    Else
        MessageBox.Show("Normal")
    End If
 ElseIf wp.showCmd = SW_SHOWMINIMIZED Then
    MessageBox.Show("Window is Minimized")
 End If
End Sub

调用它:

GetWindowStats(Process.GetProcessesByName("Excel")(0).MainWindowHandle)

请记住,如果没有 Excel 进程,这将引发错误。

于 2013-08-03T16:46:49.747 回答