How to see if another application/Window is minimized?

  • Thread starter Thread starter DraguVaso
  • Start date Start date
D

DraguVaso

Hi,

How can you see with a VB.NET-application if the Window of another
Application (for exemple an opened Word-Document) is Mimized?
Is there also a way to see if it is Maximized?

Thanks a lot,

Pieter
 
[DllImport("user32")]
public static extern bool IsIconic (int hwnd);

usage

int hwnd = //get msword application handle

bool bMinimized = IsIconic (hwnd);

bMinimized = true means window is in minized state
 
* "Shakir Hussain said:
[DllImport("user32")]
public static extern bool IsIconic (int hwnd);

usage

int hwnd = //get msword application handle

bool bMinimized = IsIconic (hwnd);

bMinimized = true means window is in minized state

This will allow you to check the window state, but it won't give you a
notification when the window state changes. 'hwnd' should be 'IntPtr'.
 
I tried it, but unfortunately it didn't work :-(
This is the code I used:

<System.Runtime.InteropServices.DllImport("user32.dll", _
EntryPoint:="IsIconic", _
CallingConvention:=Runtime.InteropServices.CallingConvention.StdCall, _
CharSet:=Runtime.InteropServices.CharSet.Unicode, SetLastError:=True)> _
Private Function _
IsIconic(ByVal hWnd As IntPtr) As Boolean
End Function


Public Sub pbenmin()
Dim clsProc As New clsProcesses
Dim p As Process
p = clsProc.ProcessExtra()
Dim IsNormal As Boolean

'Dim HWND As Integer
Dim hwnd As IntPtr
hwnd = p.Handle

If hwnd.ToInt64 = 0 Then
IsNormal = False
Exit Sub
End If
If IsIconic(hwnd) <> 0 Then
IsNormal = False
End If
End Sub


The result of IsIconic was always different than 0...

Any idea what I did wrong?

Herfried K. Wagner said:
* "Shakir Hussain said:
[DllImport("user32")]
public static extern bool IsIconic (int hwnd);

usage

int hwnd = //get msword application handle

bool bMinimized = IsIconic (hwnd);

bMinimized = true means window is in minized state

This will allow you to check the window state, but it won't give you a
notification when the window state changes. 'hwnd' should be 'IntPtr'.
 
* "DraguVaso said:
I tried it, but unfortunately it didn't work :-(
This is the code I used:

<System.Runtime.InteropServices.DllImport("user32.dll", _
EntryPoint:="IsIconic", _
CallingConvention:=Runtime.InteropServices.CallingConvention.StdCall, _
CharSet:=Runtime.InteropServices.CharSet.Unicode, SetLastError:=True)> _
Private Function _
IsIconic(ByVal hWnd As IntPtr) As Boolean
End Function


Public Sub pbenmin()
Dim clsProc As New clsProcesses
Dim p As Process
p = clsProc.ProcessExtra()
Dim IsNormal As Boolean

'Dim HWND As Integer
Dim hwnd As IntPtr
hwnd = p.Handle

'p#Handle' will return a /process/ handle, you need a /window/ handle.
Use 'p.MainWindowHandle' instead.
 
Thanks a lot!!! It works Great! I never thought it would be possible, hehe
:-)
I finally did it like this:

Public Const GW_HWNDPREV = 3
Private Const SW_SHOW = 5
Private Const SW_RESTORE = 9

<System.Runtime.InteropServices.DllImport("user32.dll", _
EntryPoint:="SetForegroundWindow", _
CallingConvention:=Runtime.InteropServices.CallingConvention.StdCall, _
CharSet:=Runtime.InteropServices.CharSet.Unicode, SetLastError:=True)> _
Public Shared Function SetForegroundWindow(ByVal handle As IntPtr) As
Boolean
' Leave function empty
End Function

<System.Runtime.InteropServices.DllImport("user32.dll", _
EntryPoint:="ShowWindow", _
CallingConvention:=Runtime.InteropServices.CallingConvention.StdCall, _
CharSet:=Runtime.InteropServices.CharSet.Unicode, SetLastError:=True)> _
Private Shared Function ShowWindow(ByVal handle As IntPtr, ByVal nCmd As
Int32) As Boolean
' Leave function empty
End Function

<System.Runtime.InteropServices.DllImport("user32.dll", _
EntryPoint:="IsIconic", _
CallingConvention:=Runtime.InteropServices.CallingConvention.StdCall, _
CharSet:=Runtime.InteropServices.CharSet.Unicode, SetLastError:=True)> _
Private Shared Function IsIconic(ByVal hWnd As IntPtr) As Boolean
' Leave function empty
End Function

<System.Runtime.InteropServices.DllImport("user32.dll", _
EntryPoint:="IsIconic", _
CallingConvention:=Runtime.InteropServices.CallingConvention.StdCall, _
CharSet:=Runtime.InteropServices.CharSet.Unicode, SetLastError:=True)> _
Private Shared Function IsZoomed(ByVal hWnd As IntPtr) As Boolean
' Leave function empty
End Function


Public Shared Sub SetToForGround(ByVal hwnd As IntPtr)
Dim strStatus As String
'Dim hwnd As IntPtr
'hwnd = p.MainWindowHandle

If IntPtr.Zero.Equals(hwnd) Then
strStatus = ""
Exit Sub
End If
If IsIconic(hwnd) Then
strStatus = "MIN"
End If
'If IsZoomed(hwnd) Then
' IsNormal = True
'End If
'If IsIconic(hwnd) And IsZoomed(hwnd) Then
' IsNormal = True
'End If

If strStatus = "MIN" Then
'mimized
ShowWindow(hwnd, SW_RESTORE)
SetForegroundWindow(hwnd)
Else
'maximzed or restored
SetForegroundWindow(hwnd)
End If
End Sub
 
Back
Top