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

Har*_*rsh 3 .net c# vb.net

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

Dav*_*ras 5

使用FindWindowSendMessageAPI

在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);
    }  
}
Run Code Online (Sandbox Code Playgroud)

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