|
-
Aug 25th, 2015, 07:12 AM
#12
Re: AppActivate and FindWindow
 Originally Posted by robertx
I have used this to invoke the calculator app (by replacing "notepad.exe" with "calc.exe"). It opens the calculator OK if it is not already open but if it is open and minimised, it doesn't maximise the window so to the user, it may appear that nothing is happening. I don't want to kill the process first either in case the user was doing something on the calculator previously. Is there a way to maximise the window if it is minimised?
IsIconic will tell you if window is minimized, ShowWindow will let you change windowstate.. could use GetProcessesByName to get handle also,..
Tested on Windows 7
Code:
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
Private Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As Boolean
End Function
Private Declare Auto Function IsIconic Lib "user32.dll" (ByVal hwnd As IntPtr) As Boolean
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function ShowWindow(ByVal hwnd As IntPtr, ByVal nCmdShow As Int32) As Boolean
End Function
Public Enum ShowWindowCommands As Integer
'for info on these commands see, http://msdn.microsoft.com/en-us/library/windows/desktop/ms633548%28v=vs.85%29.aspx
SW_HIDE = 0
SW_SHOWNORMAL = 1
SW_NORMAL = 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 'similar to SW_SHOW, except the window is not activated.
SW_RESTORE = 9
SW_SHOWDEFAULT = 10
SW_FORCEMINIMIZE = 11
End Enum
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim hWnd As IntPtr = GetWindowHandle("Calc.exe")
If Not hWnd = IntPtr.Zero Then ' found
' if minimized then restore.
If IsIconic(hWnd) Then
ShowWindow(hWnd, ShowWindowCommands.SW_RESTORE)
Else
' bring to foreground
SetForegroundWindow(hWnd)
End If
Else
Process.Start("calc.exe")
End If
End Sub
Private Function GetWindowHandle(processName As String) As IntPtr
processName = System.IO.Path.GetFileNameWithoutExtension(processName)
Dim p() = Process.GetProcessesByName(processName)
Return If(p.Length > 0, p(0).MainWindowHandle, IntPtr.Zero)
End Function
End Class
Last edited by Edgemeal; Aug 26th, 2015 at 11:31 AM.
Reason: Tested on Windows 7
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|