怎么在c#项目中通过调用Win32Api关闭当前应用

  介绍

今天就跟大家聊聊有关怎么在c#项目中通过调用Win32Api关闭当前应用,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

Win32 API Win32 API即为微软32位平台的应用程序编程接口(应用程序编程接口),所有在Win32平台上运行的应用程序都可以调用这些函数

<李>

使用Win32 API,应用程序可以充分挖掘Windows的32位操作系统的潜力。微软的所有32位平台都支持统一的API,包括函数、结构,消息,宏及接口。使用Win32 API不但可以开发出在各种平台上都能成功运行的应用程序,而且也可以充分利用每个平台特有的功能和属性。

<李>

在具体编程时,程序实现方式的差异依赖于相应平台的底层功能的不同。最显著的差异是某些函数只能在更强大的平台上实现其功能,例如,安全函数只能在Windows NT操作系统下使用。另外一些主要差别就是系统限制,比如值的范围约束,或函数可管理的项目个数等等。

思路

<李>

使用EnumWindows接口枚举当前窗口;

<李>

过滤掉不可用,隐藏,最小化的窗口;

<李>

过滤掉子窗口;

<李>

通过标题,类名过滤掉系统窗口;

<李>

使用PostMessage发送关闭窗口信息。

具体实现

//,过滤掉系统的一些窗口   private  static  string [], filterTitles =, new 字符串[1],{,“program  manager"};   private  static  string [], filterClasses =, new 字符串[5],{,“shell_traywnd",,“workerw",,“button",,“progman",,“windows.ui.core.corewindow"};      private  void  CloseCurrentApp ()   {   ,CallBack  sort =, new 回调(EnumCallback);   ,EnumWindows (,, 0);   ,返回;   }      private  bool  EnumCallback (IntPtr  hwnd, int  lParam)   {   ,string  title =, GetWindowText (hwnd);   ,StringBuilder  className =, new  StringBuilder (256);   ,int  nRet =, GetClassName (hwnd,类名,className.Capacity);   ,if  (nRet ==, 0)   className.Append才能(“;”);      (!,if  IsWindowVisible (hwnd))   return 才能;真实;      (!,if  IsWindowEnabled (hwnd))   return 才能;真实;      ,if  (IsIconic (hwnd))   return 才能;真实;      ,//过滤掉子窗口   ,IntPtr  parent =, GetParent (hwnd);   ,string  parentTitle =, GetWindowText(父);   ,if  (parent  !=, IntPtr.Zero)   ,{   if 才能;(IsWindowVisible(父),,,,IsWindowEnabled(父)   ,,return 真实;   ,}      ,IntPtr  owner =, GetWindow (hwnd, GW_OWNER);   ,if  (owner  !=, IntPtr.Zero)   ,{   if 才能;(IsWindowVisible(所有者),,,,IsWindowEnabled(所有者))   ,,return 真实;   ,}      ,if  (! filterTitles.Contains (title.ToLower ()),,,, ! filterClasses.Contains (className.ToString () .ToLower ()))   ,{   PostMessage才能(hwnd, WM_SYSCOMMAND,, SC_CLOSE,, 0);   Console.WriteLine才能(“关闭窗口(句柄:{0},,标题:{1})!“,,hwnd,,标题);      # region 才能获取窗口信息   int 才能;processID =, 1;   long 才能;threadID =, 1;   时间=processID 才能;GetWindowThreadProcessId (hwnd, out  threadID);   bool 才能;isiconic =, IsIconic (hwnd);   uint 才能;gwlStyle =,(单位)GetWindowLong (hwnd, GWL_STYLE);      IntPtr 才能;hProcess =, OpenProcess (ProcessAccessFlags.QueryInformation,,假的,,processID);   string 才能;fullPath =,““   if 才能;(hProcess  !=, IntPtr.Zero)   {才能   ,,int  capacity =, 1024;   ,,StringBuilder  processName =, new  StringBuilder(能力);   ,,QueryFullProcessImageName (hProcess, 0,, processName,, ref 容量);   ,,fullPath =, processName.ToString(0,,能力);   ,,CloseHandle (hProcess);   ,,}      Console.WriteLine才能(“- - - - - - - - - - - - - - - - - - -窗口信息:- - - - - - - - - - - - -产生绯闻;);   Console.WriteLine才能(“====标题:{0},句柄:{1}====,,,,,hwnd);   Console.WriteLine才能(“====父窗口标题:{0},父窗口句柄:{1}====,,,parentTitle,,父母);   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(位置);   GetWindowPlacement才能(hwnd, ref 位置);   Console.WriteLine才能(“====位置:{0}====,,,placement.showCmd);   EnumPropsDelegate 才能;prop =, new  EnumPropsDelegate (EnumPropsProc);   EnumProps才能(hwnd,道具);   # endregion 才能获取窗口信息      return 才能;假;   ,}      ,return 真实;   }      null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

怎么在c#项目中通过调用Win32Api关闭当前应用