Restore Window After Duplicate Instance Check

S

scorpion53061

Credit to Herfried...

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
 
G

Glenn

I can determine if another instance is running, but how do I get it's
minimized window to restore (in VBNET). Using the standard win32 functions
is not working.

Thanks
 
C

Crouchie1998

Herfried's code is extremely long when you can produce the same result is
around 3-5 lines.

Why not use appActivate using the process handle?

Crouchie1998
BA (HOS) MCP MCSE
 
C

Crouchie1998

Herfried

Why didn't you just tell the user to Import System.Runtime.InteropServices
to shorten then API calls?

Crouchie1998
BA (HONS) MCP MCSE
 
G

Glenn

Sorry for delay, I was away:

AppActivate does not restore the window according to the documentation and
also from trying.

Thanks for your comment.

Best,
Glenn
 
G

Glenn

Thanks for your response. I will give it a try. Seems amazing that for
something that one might think was a fairly common need, there would not be
something simple built in
like App.Activate, something like, App.Activat(RestoreWindow), etc.

Best,
Glenn
 
G

Guest

Finally got to test. Restoring from icon does not work. If the window is
maximized or normal, the window comes to foreground, but if minimized, then
not. IsIconic() reports false when window minimized. Also, ShowWindow does
not restore and SetToForground does not even highlight the window.

Any further ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top