|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to activate an existing programHi all,
I need to activate a running Windows application from my .Net application. (I want to display the application's main form if it's minimized and try to give it focus.) For example: Dim procs() as Process = Process.GetProcessesByName("SomeApp") If procs.Length > 0 Then Procs(0).Activate End If Of course, the Process class doesn't expose an Activate method. My question is: Is there anything baked into .Net that does this, or do I need to get the unmanaged process handle and fiddle around with Windows API calls? TIA - Bob Hi Bob,
First of all, I would like to confirm my understanding of your issue. From your description, I understand that you need to get the main windows of a certain process, and show it in foreground. If there is any misunderstanding, please feel free to let me know. In this, we have to accomplish 2 things. 1. Get the main window of the process. If the window is minimized, show it as normal. 2. If the window is in background, bring it to front. In this case, we have to use P/Invoke with 2 functions. ShowWindow and SetForegroundWindow. Here is an example. Imports System.Diagnostics Imports System.Runtime.InteropServices Public Class Form1 <DllImport("user32.dll")> _ Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) _ As <MarshalAs(UnmanagedType.Bool)> Boolean End Function <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _ Shared Function ShowWindow(ByVal hwnd As IntPtr, ByVal nCmdShow As Int32) As Long End Function Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim procs() As Process = Process.GetProcessesByName("RedAlerter") If procs.Length > 0 Then Form1.ShowWindow(procs(0).MainWindowHandle, 1) Form1.SetForegroundWindow(procs(0).MainWindowHandle) End If End Sub End Class If anything is unclear, please feel free to reply to the post. Kevin Yu Microsoft Online Community Support ================================================== Get notification to my posts through email? Please refer to http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif ications. Note: The MSDN Managed Newsgroup support offering is for non-urgent issues where an initial response from the community or a Microsoft Support Engineer within 1 business day is acceptable. Please note that each follow up response may take approximately 2 business days as the support professional working with you may need further investigation to reach the most efficient resolution. The offering is not appropriate for situations that require urgent, real-time or phone-based interactions or complex project analysis and dump analysis issues. Issues of this nature are best handled working with a dedicated Microsoft Support Engineer by contacting Microsoft Customer Support Services (CSS) at http://msdn.microsoft.com/subscriptions/support/default.aspx. ================================================== (This posting is provided "AS IS", with no warranties, and confers no rights.) Actually, depending on what program you're going to run, there is a third
way. If the program is a signle-instance program, running it again will re-activate it. (Try running Outlook.exe again when it's minimized) Show quote "Kevin Yu [MSFT]" <v-k***@online.microsoft.com> ¼¶¼g©ó¶l¥ó·s»D:EI1ar8h$GHA.5***@TK2MSFTNGXA01.phx.gbl... > Hi Bob, > > First of all, I would like to confirm my understanding of your issue. From > your description, I understand that you need to get the main windows of a > certain process, and show it in foreground. If there is any > misunderstanding, please feel free to let me know. > > In this, we have to accomplish 2 things. > > 1. Get the main window of the process. If the window is minimized, show it > as normal. > 2. If the window is in background, bring it to front. > Thanks Kevin, that works like a champ!
- Bob Show quote "Kevin Yu [MSFT]" <v-k***@online.microsoft.com> wrote in message news:EI1ar8h$GHA.5920@TK2MSFTNGXA01.phx.gbl... > Hi Bob, > > First of all, I would like to confirm my understanding of your issue. From > your description, I understand that you need to get the main windows of a > certain process, and show it in foreground. If there is any > misunderstanding, please feel free to let me know. > > In this, we have to accomplish 2 things. > > 1. Get the main window of the process. If the window is minimized, show it > as normal. > 2. If the window is in background, bring it to front. > > In this case, we have to use P/Invoke with 2 functions. ShowWindow and > SetForegroundWindow. Here is an example. > > Imports System.Diagnostics > Imports System.Runtime.InteropServices > > Public Class Form1 > > <DllImport("user32.dll")> _ > Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) _ > As <MarshalAs(UnmanagedType.Bool)> Boolean > End Function > > <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _ > Shared Function ShowWindow(ByVal hwnd As IntPtr, ByVal nCmdShow As > Int32) As Long > End Function > > Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As > System.EventArgs) Handles Button1.Click > Dim procs() As Process = Process.GetProcessesByName("RedAlerter") > If procs.Length > 0 Then > Form1.ShowWindow(procs(0).MainWindowHandle, 1) > Form1.SetForegroundWindow(procs(0).MainWindowHandle) > End If > End Sub > End Class > > If anything is unclear, please feel free to reply to the post. > > Kevin Yu > Microsoft Online Community Support > > ================================================== > Get notification to my posts through email? Please refer to > http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif > ications. > Note: The MSDN Managed Newsgroup support offering is for non-urgent issues > where an initial response from the community or a Microsoft Support > Engineer within 1 business day is acceptable. Please note that each follow > up response may take approximately 2 business days as the support > professional working with you may need further investigation to reach the > most efficient resolution. The offering is not appropriate for situations > that require urgent, real-time or phone-based interactions or complex > project analysis and dump analysis issues. Issues of this nature are best > handled working with a dedicated Microsoft Support Engineer by contacting > Microsoft Customer Support Services (CSS) at > http://msdn.microsoft.com/subscriptions/support/default.aspx. > ================================================== > > (This posting is provided "AS IS", with no warranties, and confers no > rights.) > |
|||||||||||||||||||||||