By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,109 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

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

P: 1
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 "FindWindowA" (ByVal ByVallpClassName 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(vbNullString, strCaption)

End Function

End Module
May 17 '07 #1
Share this Question
Share on Google+
1 Reply


P: 36
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 "FindWindowA" (ByVal ByVallpClassName 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(vbNullString, 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.txt"
Expand|Select|Wrap|Line Numbers
  1. Process[] allProc = Process.GetProcesses();
  2.  
  3.             foreach (Process proc in allProc)
  4.             {
  5.                 string procName =  proc.ProcessName;
  6.  
  7.                 procName = proc.MainWindowTitle;
  8.                 int len = procName.IndexOf('-', 0);
  9.                 string newString = String.Empty;
  10.                 if (-1 != len)
  11.                 {
  12.                     newString = procName.Substring(0, len - 1);
  13.                     newString.Trim();
  14.                 }
  15.  
  16.                 if (newString.Equals("testing.txt"))
  17.                 {
  18.                     proc.Kill();
  19.                 }
  20.             }
  21.  
It is in C#, but not a big deal to change it to VB.net
Hope it helped.
Jun 12 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.