Harsh
Harsh

Reputation: 3751

How to close a window programmatically in VB.net?

How can I close a window of external application programmatically in VB.net. I just want to close the current window without closing the whole process.

Upvotes: 3

Views: 3279

Answers (1)

Davide Piras
Davide Piras

Reputation: 44605

use FindWindow and SendMessage APIs

here in C#, should be trivial to convert:

using Microsoft.Win32;

[DllImport("user32.dll")]
public static extern int FindWindow(string lpClassName,string lpWindowName);
[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);

public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;

private void closeWindow()
{
    // retrieve the handler of the window  
    int iHandle = FindWindow("Notepad", "Untitled - Notepad");
    if (iHandle > 0)
    {
        // close the window using API        
        SendMessage(iHandle, WM_SYSCOMMAND, SC_CLOSE, 0);
    }  
}

source: http://www.codeproject.com/KB/dialog/closewindow.aspx

Upvotes: 5

Related Questions