Bring another Window to Front

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

DraguVaso

Hi,

My application has some interaction with an other application. I sometimes
need to put the other application to the front. The problem is that I'm not
able to get a nice solution to work in every case. I tryed two ways: one way
it disn't maximazi the Windowd when it was Minimized, in the other way it
resized the application when it was maximzed.

First I take a Handle to the other Window with the
"Process.GetProcessesByName("EXTRA")" and with the "Dim handle As IntPtr =
Process.MainWindowHandle"

First 'solution':
SetForegroundWindow(handle)
This gives me the problem with Mimized Window that gets the focus, but

Second 'solution':
ShowWindow(handle, SW_RESTORE) 'SW_RESTORE = 9
SetForegroundWindow(handle)
This gives me the problem with the Maximized Window that is suddenly
resized.

What I actually need is a solution that works always:
When the Windowd is Maximized, it has to keep Maximized. When it is Resized
it has to stay Resized. When it is Minimized it should be Restored (to the
last Status: Maxmimized or Resized).
I found some stuff about the IsIconic-API, but it didn't seem to work.

Does anybody knows a solution for my problem? I'm really stuck with it :-/

thanks a lot in advance,

Pieter
 
I found it! Thanks 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
 
Back
Top