c# 調(diào)用Win32Api關(guān)閉當(dāng)前應(yīng)用的方法
Win32 API
Win32 API即為Microsoft 32位平臺(tái)的應(yīng)用程序編程接口(Application Programming Interface)。所有在Win32平臺(tái)上運(yùn)行的應(yīng)用程序都可以調(diào)用這些函數(shù)
- 使用Win32 API,應(yīng)用程序可以充分挖掘Windows的32位操作系統(tǒng)的潛力。 Microsoft的所有32位平臺(tái)都支持統(tǒng)一的API,包括函數(shù)、結(jié)構(gòu)、消息、宏及接口。使用 Win32 API不但可以開(kāi)發(fā)出在各種平臺(tái)上都能成功運(yùn)行的應(yīng)用程序,而且也可以充分利用每個(gè)平臺(tái)特有的功能和屬性。
- 在具體編程時(shí),程序?qū)崿F(xiàn)方式的差異依賴(lài)于相應(yīng)平臺(tái)的底層功能的不同。最顯著的差異是某些函數(shù)只能在更強(qiáng)大的平臺(tái)上實(shí)現(xiàn)其功能。例如,安全函數(shù)只能在Windows NT操作系統(tǒng)下使用。另外一些主要差別就是系統(tǒng)限制,比如值的范圍約束,或函數(shù)可管理的項(xiàng)目個(gè)數(shù)等等。
本文介紹Windows系統(tǒng)下使用Win32API獲取當(dāng)前應(yīng)用并關(guān)閉的方法。
思路
- 使用EnumWindows接口枚舉當(dāng)前窗口;
- 過(guò)濾掉不可用、隱藏、最小化的窗口;
- 過(guò)濾掉子窗口;
- 通過(guò)標(biāo)題、類(lèi)名過(guò)濾掉系統(tǒng)窗口;
- 使用PostMessage發(fā)送關(guān)閉窗口信息。
具體實(shí)現(xiàn)
// 過(guò)濾掉系統(tǒng)的一些窗口 private static string[] filterTitles = new string[1] { "program manager"}; private static string[] filterClasses = new string[5] { "shell_traywnd", "workerw", "button", "progman", "windows.ui.core.corewindow"}; private void CloseCurrentApp() { CallBack sort = new CallBack(EnumCallback); EnumWindows(sort, 0); return; } private bool EnumCallback(IntPtr hwnd, int lParam) { string title = GetWindowText(hwnd); StringBuilder className = new StringBuilder(256); int nRet = GetClassName(hwnd, className, className.Capacity); if (nRet == 0) className.Append(""); if (!IsWindowVisible(hwnd)) return true; if (!IsWindowEnabled(hwnd)) return true; if (IsIconic(hwnd)) return true; // 過(guò)濾掉子窗口 IntPtr parent = GetParent(hwnd); string parentTitle = GetWindowText(parent); if (parent != IntPtr.Zero) { if (IsWindowVisible(parent) && IsWindowEnabled(parent)) return true; } IntPtr owner = GetWindow(hwnd, GW_OWNER); if (owner != IntPtr.Zero) { if (IsWindowVisible(owner) && IsWindowEnabled(owner)) return true; } if (!filterTitles.Contains(title.ToLower()) && !filterClasses.Contains(className.ToString().ToLower())) { PostMessage(hwnd, WM_SYSCOMMAND, SC_CLOSE, 0); Console.WriteLine("關(guān)閉窗口(句柄:{0}, 標(biāo)題:{1})!", hwnd, title); #region 獲取窗口信息 int processID = -1; long threadID = -1; processID = GetWindowThreadProcessId(hwnd, out threadID); bool isiconic = IsIconic(hwnd); uint gwlStyle = (uint)GetWindowLong(hwnd, GWL_STYLE); IntPtr hProcess = OpenProcess(ProcessAccessFlags.QueryInformation, false, processID); string fullPath = ""; if (hProcess != IntPtr.Zero) { int capacity = 1024; StringBuilder processName = new StringBuilder(capacity); QueryFullProcessImageName(hProcess, 0, processName, ref capacity); fullPath = processName.ToString(0, capacity); CloseHandle(hProcess); } Console.WriteLine("-------------------窗口info:---------------"); Console.WriteLine("====標(biāo)題:{0} 句柄:{1}====", title, hwnd); Console.WriteLine("====父窗口標(biāo)題:{0} 父窗口句柄:{1}====", parentTitle, parent); Console.WriteLine("====進(jìn)程ID:{0} 類(lèi)名:{1}====", processID, className.ToString()); Console.WriteLine("====進(jìn)程名:{0}====", fullPath); Console.WriteLine("====isiconic:{0} 樣式:{1}====", isiconic, gwlStyle); WINDOWPLACEMENT placement = new WINDOWPLACEMENT(); placement.length = System.Runtime.InteropServices.Marshal.SizeOf(placement); GetWindowPlacement(hwnd, ref placement); Console.WriteLine("====placement:{0}====", placement.showCmd); EnumPropsDelegate prop = new EnumPropsDelegate(EnumPropsProc); EnumProps(hwnd, prop); #endregion 獲取窗口信息 return false; } return true; } private bool EnumPropsProc(IntPtr hwnd, IntPtr lpszString, IntPtr hData) { string propName = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(lpszString); Console.WriteLine("====屬性:{0} 數(shù)據(jù):{1}====", propName, hData); return true; } #region Win32Api public const int GWL_STYLE = (-16); public const int GWL_EXSTYLE = (-20); public const int GW_OWNER = 4; public const int WS_EX_TOOLWINDOW = 0x00000080; public const int WM_SYSCOMMAND = 0x0112; public const int WM_CLOSE = 0x10; public const int SC_CLOSE = 0xF060; public delegate bool CallBack(IntPtr hwnd, int lparam); public delegate bool EnumPropsDelegate(IntPtr hwnd, IntPtr lpszString, IntPtr hData); [DllImport("user32.dll")] public static extern int EnumWindows(CallBack x, int y); [DllImport("user32.dll", CharSet = CharSet.Auto)] internal static extern int GetWindowText(IntPtr hWnd, System.Text.StringBuilder lpString, int nMaxCount); [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern int GetWindowTextLength(IntPtr hWnd); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] public static extern int GetClassName(IntPtr hWnd, System.Text.StringBuilder lpClassName, int nMaxCount); [DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr hwnd); [DllImport("user32.dll")] public static extern bool IsWindowEnabled(IntPtr hwnd); [DllImport("user32.dll", EntryPoint = "IsIconic")] public static extern bool IsIconic(IntPtr hWnd); [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr GetParent(IntPtr hwnd); [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr GetWindow(IntPtr hwndParent, int nCmd); [DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)] public static extern long GetWindowLong(IntPtr hwnd, int nIndex); [DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)] public static extern bool PostMessage(IntPtr hwnd, uint Msg, uint wParam, uint lParam); [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern int GetWindowThreadProcessId(IntPtr hWnd, out long lpdwProcessId); [DllImport("kernel32.dll", SetLastError = true)] public static extern IntPtr OpenProcess( ProcessAccessFlags processAccess, bool bInheritHandle, int processId ); [DllImport("kernel32.dll", SetLastError = true)] public static extern bool QueryFullProcessImageName([In]IntPtr hProcess, [In]int dwFlags, [Out]System.Text.StringBuilder lpExeName, ref int lpdwSize); [DllImport("coredll.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool CloseHandle(IntPtr hObject); [DllImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl); [DllImport("user32.dll")] public static extern int EnumProps(IntPtr hWnd, EnumPropsDelegate lpEnumFunc); public struct WINDOWPLACEMENT { public int length; public int flags; public int showCmd; public System.Drawing.Point ptMinPosition; public System.Drawing.Point ptMaxPosition; public System.Drawing.Rectangle rcNormalPosition; } [Flags] public enum ProcessAccessFlags : uint { All = 0x001F0FFF, Terminate = 0x00000001, CreateThread = 0x00000002, VirtualMemoryOperation = 0x00000008, VirtualMemoryRead = 0x00000010, VirtualMemoryWrite = 0x00000020, DuplicateHandle = 0x00000040, CreateProcess = 0x000000080, SetQuota = 0x00000100, SetInformation = 0x00000200, QueryInformation = 0x00000400, QueryLimitedInformation = 0x00001000, Synchronize = 0x00100000 } public static string GetWindowText(IntPtr hwnd) { int capacity = GetWindowTextLength(hwnd) * 2; System.Text.StringBuilder lpString = new System.Text.StringBuilder(capacity); GetWindowText(hwnd, lpString, lpString.Capacity); if (lpString.Length > 0) { return lpString.ToString(); } return string.Empty; } #endregion Win32Api
以上就是c# 調(diào)用Win32Api關(guān)閉當(dāng)前應(yīng)用的方法的詳細(xì)內(nèi)容,更多關(guān)于c# 調(diào)用Win32Api關(guān)閉應(yīng)用的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- c#使用win32api實(shí)現(xiàn)獲取光標(biāo)位置
- C# 執(zhí)行CMD命令并接收返回結(jié)果的操作方式
- C#異步方法返回void與Task的區(qū)別詳解
- C# DataSet查看返回結(jié)果集的實(shí)現(xiàn)
- C# ODP.NET 調(diào)用Oracle函數(shù)返回值時(shí)報(bào)錯(cuò)的一個(gè)解決方案
- C# WebApi 接口返回值不困惑:返回值類(lèi)型詳解
- C#微信開(kāi)發(fā)之接收 / 返回文本消息
- webBrowser執(zhí)行js的方法,并返回值,c#后臺(tái)取值的實(shí)現(xiàn)
- c#基于Win32Api實(shí)現(xiàn)返回Windows桌面功能
相關(guān)文章
C#實(shí)現(xiàn)去除Strings中空格的方法
這篇文章主要介紹了C#實(shí)現(xiàn)去除Strings中空格的方法,較為詳細(xì)的介紹了C#實(shí)現(xiàn)去除字符串首尾及中間空格的方法,是非常實(shí)用的技巧,需要的朋友可以參考下2014-10-10C#調(diào)用執(zhí)行外部程序的實(shí)現(xiàn)方法
這篇文章主要介紹了C#調(diào)用執(zhí)行外部程序的實(shí)現(xiàn)方法,涉及C#進(jìn)程調(diào)用的相關(guān)使用技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-04-04

Winform實(shí)現(xiàn)調(diào)用asp.net數(shù)據(jù)接口實(shí)例

c#實(shí)現(xiàn)KTV點(diǎn)歌系統(tǒng)