C#调用API函数查找窗体并关闭

下面的代码使用API函数FindWindow查找名称为窗体a的窗体,并调用API函数SendMessage向目标窗体发送消息关闭目标窗体。

    public const int WM_CLOSE = 0x10;
    [DllImport("user32.dll", EntryPoint = "SendMessageA")]
    public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
    [DllImport("User32.dll", EntryPoint = "FindWindow")]
    private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    IntPtr hwnd_win;
    hwnd_win = FindWindow(null, "窗体a");
    SendMessage(hwnd_win, WM_CLOSE, 0, 0);

你可能感兴趣的:(FindWindow,SendMessag,使用API关闭窗体,C#调用API)