IonutZ Posted March 1, 2008 Posted March 1, 2008 VB.NET 2008-FORM: Getting a window to forground while only knowing its partial name. Hi guys :) I have the name of an application, it starts with ACI and I want to set it to my forground in order to sendkeys some stuff to it. Now I've read a BUNCH of stuff and could not come up with a solution on my own. This is the code I have for now, a bunch of stuff compiled together [most of the code I've seen out there and help, was provided by someone named Herfried - so 95% credits of what I have here should go to him] :] [[[On the side if you could point me in the right direction, how do I send text to a textbox who's HWND I know?]]] Thanks in advance, Love this forum, I've gotten so many solutions just from other people's posts, however, everything concerning this user32.dll and its api seems to vague for me so I had to ask for help once and for all! ~IonutZ Code: ( text ) Public Class Form1 Public Const GW_HWNDPREV = 3 Private Const SW_SHOW = 5 Private Const SW_RESTORE = 9 Public Shared Function SetForegroundWindow(ByVal handle As IntPtr) As Boolean ' Leave function empty End Function Private Shared Function ShowWindow(ByVal handle As IntPtr, ByVal nCmd As Int32) As Boolean ' Leave function empty End Function Private Shared Function IsIconic(ByVal hWnd As IntPtr) As Boolean ' Leave function empty End Function 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 Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim strPartialTitle As String = "ACI" ' enumerate all processes For Each pProcess As Process In Process.GetProcesses() ' check process' main window title If pProcess.MainWindowTitle.StartsWith(strPartialTitl e) Then ' it's a match Dim strMsg As String = [string].Format("{0}", pProcess.MainWindowTitle) 'MessageBox.Show(pProcess.MainWindowHandle.ToStrin g) SetToForGround(pProcess.MainWindowHandle) End If Next End Sub End Class Quote
Administrators PlausiblyDamp Posted March 1, 2008 Administrators Posted March 1, 2008 As a simpler example (doesn't handle minimized windows etc.) try Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As IntPtr) As Long Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim strPartialTitle As String = "Wind" For Each pProcess As Process In Process.GetProcesses() ' check process' main window title If pProcess.MainWindowTitle.StartsWith(strPartialTitle) Then ' it's a match Dim strMsg As String = [string].Format("{0}", pProcess.MainWindowTitle) 'MessageBox.Show(pProcess.MainWindowHandle.ToStrin g) SetForegroundWindow(pProcess.MainWindowHandle) End If Next End Sub Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts