如何在VB.net中以编程方式关闭窗口?

时间:2011-10-24 10:00:20

标签: c# .net vb.net

如何在VB.net中以编程方式关闭外部应用程序窗口。 我只想关闭当前窗口而不关闭整个过程。

1 个答案:

答案 0 :(得分:5)

使用FindWindowSendMessage API

这里用C#,转换应该是微不足道的:

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);
    }  
}

来源:http://www.codeproject.com/KB/dialog/closewindow.aspx