API關閉指定表單

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

        public static void CloseProcessByName(string lpWindowName)
        {
            IntPtr hwnd_win; hwnd_win = FindWindow(null, lpWindowName);//lpWindowName "要找的表單名稱"
            SendMessage(hwnd_win, WM_CLOSE, 0, 0);
        }

你可能感兴趣的:(API關閉指定表單)