How do I find a Window by it's Caption and then Close it!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wormsnout
    New Member
    • May 2007
    • 1

    How do I find a Window by it's Caption and then Close it!

    Hi all,

    I'm looking to write some Visual Basic .NET code to find a Window given its Caption and then kill it entirely (not just minimise it).

    I think I have found the correct Windows Handle using the following code (see eg below which uses just a new Notepad window), however I am struggling majorly trying to find the code to actually close the window. I can't even find code that will correctly return the PID of the window. Please help! ...


    Module modMain

    'API for picking up the Windows Handle of a Window
    Private Declare Function FindWindow Lib "user32" Alias "FindWindow A" (ByVal ByVallpClassNam e As String, ByVal lpWindowName As String) As Integer

    Public Sub Main()

    Dim wHnd As Integer = GetWindowHandle ("Untitled - Notepad")

    MsgBox("wHnd = " & wHnd)


    End Sub

    Private Function GetWindowHandle (ByVal strCaption As String) As Long
    'Return the Windows Handle of the Window with Caption = strCaption

    Return FindWindow(vbNu llString, strCaption)

    End Function

    End Module
  • chinu
    New Member
    • Jun 2007
    • 36

    #2
    Originally posted by wormsnout
    Hi all,

    I'm looking to write some Visual Basic .NET code to find a Window given its Caption and then kill it entirely (not just minimise it).

    I think I have found the correct Windows Handle using the following code (see eg below which uses just a new Notepad window), however I am struggling majorly trying to find the code to actually close the window. I can't even find code that will correctly return the PID of the window. Please help! ...


    Module modMain

    'API for picking up the Windows Handle of a Window
    Private Declare Function FindWindow Lib "user32" Alias "FindWindow A" (ByVal ByVallpClassNam e As String, ByVal lpWindowName As String) As Integer

    Public Sub Main()

    Dim wHnd As Integer = GetWindowHandle ("Untitled - Notepad")

    MsgBox("wHnd = " & wHnd)


    End Sub

    Private Function GetWindowHandle (ByVal strCaption As String) As Long
    'Return the Windows Handle of the Window with Caption = strCaption

    Return FindWindow(vbNu llString, strCaption)

    End Function

    End Module
    Try the below code. here i am trying to close a notepad which is opened which have the window title as "testing.tx t"
    [code=c]
    Process[] allProc = Process.GetProc esses();

    foreach (Process proc in allProc)
    {
    string procName = proc.ProcessNam e;

    procName = proc.MainWindow Title;
    int len = procName.IndexO f('-', 0);
    string newString = String.Empty;
    if (-1 != len)
    {
    newString = procName.Substr ing(0, len - 1);
    newString.Trim( );
    }

    if (newString.Equa ls("testing.txt "))
    {
    proc.Kill();
    }
    }
    [/code]
    It is in C#, but not a big deal to change it to VB.net
    Hope it helped.

    Comment

    Working...