我试图在最大化或最小化时获取应用程序的状态。我重写了脚本,以便它特定于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
答案 0 :(得分: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进程,则会抛出错误。