Win32 API即为Microsoft 32位平台的应用程序编程接口(Application Programming Interface)。所有在Win32平台上运行的应用程序都可以调用这些函数
本文介绍Windows系统下使用Win32API获取当前应用并关闭的方法。
// 过滤掉系统的一些窗口 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; // 过滤掉子窗口 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("关闭窗口(句柄:{0}, 标题:{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("====标题:{0} 句柄:{1}====", title, hwnd); Console.WriteLine("====父窗口标题:{0} 父窗口句柄:{1}====", parentTitle, parent); Console.WriteLine("====进程ID:{0} 类名:{1}====", processID, className.ToString()); Console.WriteLine("====进程名:{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} 数据:{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# 调用Win32Api关闭当前应用的方法的详细内容,更多关于c# 调用Win32Api关闭应用的资料请关注小牛知识库其它相关文章!
我必须实现一个名为的方法(Factory-method),在这里我可以创建具有特定温度单位(摄氏度、开尔文...)的新对象。我还创建了将代码从一个单元转换为另一个单元的方法。问题是我不知道如何将covert-method连接到方法。我试着让我的所有方法和变量,以便我可以调用它们。问题是:如何在create-methods中实现convert-methods? 这是我的代码:
问题内容: 我的网页上有一个关闭链接。我想使用它来关闭当前选项卡。我已经写了 上面的代码在Internet Explorer中似乎运行良好。但它不能在Mozilla Firefox和Google Chrome中使用。请帮助我解决此问题。 问题答案: 您只能关闭自己创建的窗口/选项卡。也就是说,您无法以编程方式关闭用户创建的窗口/选项卡。 例如,如果使用创建一个窗口,则可以使用关闭它。
我做了一个jsf应用程序。这个应用程序有一个包含开始、停止按钮的菜单。当按下开始时,应用程序开始从网站获取数据,并更新其数据库。应用程序还有更新过程的进度条。但是,这个过程需要很长时间才能完成。我希望当我关闭浏览器时,它应该继续更新数据库。此外,当我再次打开它时,我应该得到以前的状态。然而,这并没有发生。当我关闭浏览器时,应用程序也关闭了。我该怎么办? 谢谢。
ap.popWindow(OPTION | data) 关闭当前页面。可直接传入一个对象作为 OPTION.data 参数。 OPTION 参数说明 参数 类型 必填 描述 data Object 否 传递的 data 对象将会被即将露出的页面通过 onResume 事件接收 代码示例 <script src="https://gw.alipayobjects.com/as/g/h5-lib/al
popWindow popWindow用来关闭当前页面 使用方法 // 关闭当前打开的页面 AlipayJSBridge.call('popWindow'); 代码演示 关闭当前页面 <h1>关闭当前页面</h1> <a href="#" class="btn J_demo">执行</a> <script> function ready(callback) { // 如果jsbridge已经
什么在起作用 我已经使用以下Xamarin指南为我们的Xamarin表单应用程序的Android端实现了推送通知: https://developer.xamarin.com/guides/android/application_funcaments/notifications/remote-notifications-with-gcm/ 这非常有效,我们正确地在应用程序运行时和后台调用OnNew