Fase

How can I determine if an application such as notepad or calculator is Minimized/Maximize/Normal


Re: Visual Basic Express Edition Determine Application State

jo0ls

P/Invoke GetWindowPlacement

Imports System.Runtime.InteropServices
Imports System.Environment
Imports System.IO
Imports System.ComponentModel
 
Public Class Form1
 
    Private WithEvents b As New Button
 
    Sub New()
        InitializeComponent()
        Process.Start(Path.Combine(GetFolderPath(SpecialFolder.System), "notepad"))
        Me.Controls.Add(b)
    End Sub
 
    Private Sub b_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles b.Click
        ' FindWindow is going to find the first instance...
        Me.Text = "Notepad is " & NativeMethods.GetWindowState("Untitled - NotePad").ToString
    End Sub
 
End Class
 
 
Public Class NativeMethods
 
    <Flags()> _
    Private Enum PlacementFlags As UInteger
        WPF_SETMINPOSITION = 1
        WPF_RESTORETOMAXIMIZED = 2
        WPF_ASYNCWINDOWPLACEMENT = 4
    End Enum
 
    Private Enum ShowState As UInteger
        SW_HIDE = 0 '
        SW_SHOWNORMAL = 1 '
        SW_SHOWMINIMIZED = 2 '
        SW_SHOWMAXIMIZED = 3 '
        SW_MAXIMIZE = 3 '
        SW_SHOWNOACTIVATE = 4 '
        SW_SHOW = 5 '
        SW_MINIMIZE = 6 '
        SW_SHOWMINNOACTIVE = 7 '
        SW_SHOWNA = 8 '
        SW_RESTORE = 9 '
    End Enum
 
    ' friendly ones, just for the common ones...
    Public Enum WindowState
        Normal
        Maximized
        Minimized
    End Enum
 
    <DllImport("user32.dll")> _
    Private Shared Function GetWindowPlacement( _
        ByVal hWnd As IntPtr, _
        ByRef windowPlacement As WindowPlacement) As Boolean
    End Function
 
    <DllImport("user32.dll", EntryPoint:="FindWindowA", CharSet:=CharSet.Ansi)> _
    Public Shared Function FindWindow( _
        ByVal className As String, _
        ByVal WindowName As String) As IntPtr
    End Function
 
    Private Structure WindowPlacement
        Public Length As UInteger
        Public Flags As PlacementFlags
        Public ShowCmd As ShowState
        Public MinPosition As Point
        Public MaxPosition As Point
        Public NormalPosition As Rectangle
    End Structure
 
    Private Structure NativePoint
        Public X As Integer
        Public Y As Integer
    End Structure
 
    Private Structure NativeRectangle
        Public X As Integer
        Public Y As Integer
        Public Width As Integer
        Public Height As Integer
    End Structure
 
    Public Shared Function GetWindowState(ByVal WindowCaption As String) As WindowState
        ' So get the window handle with findwindow. I'm using the caption...
        Dim hwnd As IntPtr = FindWindow(Nothing, WindowCaption)
        If hwnd = IntPtr.Zero Then Throw New Exception("Window not found")
        ' Create the structure we pass to the api function.
        Dim wp As New WindowPlacement
        ' Set its size parameter
        wp.Length = Convert.ToUInt32(Marshal.SizeOf(GetType(WindowPlacement)))
        ' Send it off
        If GetWindowPlacement(hwnd, wp) = False Then Throw New Win32Exception
        ' It has other useful information...
        Select Case wp.ShowCmd
            Case ShowState.SW_SHOWMAXIMIZED
                Return WindowState.Maximized
            Case ShowState.SW_SHOWMINIMIZED
                Return WindowState.Minimized
            Case ShowState.SW_SHOWNORMAL
                Return WindowState.Normal
            Case Else
                ' this is just a demo...
                Throw New Exception("wasn't a friendly one, windows state is: " & wp.ShowCmd.ToString)
        End Select
        Return CType(wp.ShowCmd, WindowState)
    End Function
 
End Class