Getting a window to forground while only knowing its partial name.

IonutZ

New member
Joined
Mar 1, 2008
Messages
1
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 Ive 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 Ive 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 whos HWND I know?]]]


Thanks in advance,

Love this forum, Ive gotten so many solutions just from other peoples 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 )
Code:
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
                 its 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
 
As a simpler example (doesnt handle minimized windows etc.) try
Code:
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
             its 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
 
Back
Top