最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c# 調(diào)用Win32Api關(guān)閉當(dāng)前應(yīng)用的方法

 更新時間:2021年03月03日 11:50:52   作者:louzi  
這篇文章主要介紹了c# 調(diào)用Win32Api關(guān)閉當(dāng)前應(yīng)用的方法,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下

Win32 API

Win32 API即為Microsoft 32位平臺的應(yīng)用程序編程接口(Application Programming Interface)。所有在Win32平臺上運行的應(yīng)用程序都可以調(diào)用這些函數(shù)

  • 使用Win32 API,應(yīng)用程序可以充分挖掘Windows的32位操作系統(tǒng)的潛力。 Microsoft的所有32位平臺都支持統(tǒng)一的API,包括函數(shù)、結(jié)構(gòu)、消息、宏及接口。使用 Win32 API不但可以開發(fā)出在各種平臺上都能成功運行的應(yīng)用程序,而且也可以充分利用每個平臺特有的功能和屬性。
  • 在具體編程時,程序?qū)崿F(xiàn)方式的差異依賴于相應(yīng)平臺的底層功能的不同。最顯著的差異是某些函數(shù)只能在更強大的平臺上實現(xiàn)其功能。例如,安全函數(shù)只能在Windows NT操作系統(tǒng)下使用。另外一些主要差別就是系統(tǒng)限制,比如值的范圍約束,或函數(shù)可管理的項目個數(shù)等等。

本文介紹Windows系統(tǒng)下使用Win32API獲取當(dāng)前應(yīng)用并關(guān)閉的方法。

思路

  1. 使用EnumWindows接口枚舉當(dāng)前窗口;
  2. 過濾掉不可用、隱藏、最小化的窗口;
  3. 過濾掉子窗口;
  4. 通過標(biāo)題、類名過濾掉系統(tǒng)窗口;
  5. 使用PostMessage發(fā)送關(guān)閉窗口信息。

具體實現(xiàn)

// 過濾掉系統(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;

 // 過濾掉子窗口
 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("====進程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} 數(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)用的方法的詳細內(nèi)容,更多關(guān)于c# 調(diào)用Win32Api關(guān)閉應(yīng)用的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#使用CefSharp實現(xiàn)內(nèi)嵌網(wǎng)頁詳解

    C#使用CefSharp實現(xiàn)內(nèi)嵌網(wǎng)頁詳解

    這篇文章主要介紹了C# WPF里怎么使用CefSharp嵌入一個網(wǎng)頁,并給出一個簡單示例演示C#和網(wǎng)頁(JS)的交互實現(xiàn),感興趣的小伙伴可以了解一下
    2023-04-04
  • C#實現(xiàn)石頭剪刀布游戲

    C#實現(xiàn)石頭剪刀布游戲

    這篇文章主要為大家詳細介紹了C#實現(xiàn)石頭剪刀布游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • C# this關(guān)鍵字的四種用法

    C# this關(guān)鍵字的四種用法

    這篇文章主要為大家詳細介紹了C# this關(guān)鍵字的四種用法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • c#中合并DataTable重復(fù)行的值

    c#中合并DataTable重復(fù)行的值

    c#中合并DataTable重復(fù)行的值,需要的朋友可以參考一下
    2013-05-05
  • C#編寫Windows服務(wù)實例代碼

    C#編寫Windows服務(wù)實例代碼

    本篇文章主要介紹使用Microsoft Visual Studio2012可以很方便的創(chuàng)建一個Windows服務(wù),本例實現(xiàn)一個向D盤的txt文件里,寫入系統(tǒng)時間的Windows服務(wù)
    2013-10-10
  • c# Linq查詢詳解

    c# Linq查詢詳解

    這篇文章主要介紹了c# Linq查詢的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-04-04
  • c# 類成員初始化順序的特殊情況

    c# 類成員初始化順序的特殊情況

    這篇文章主要介紹了c# 類成員初始化順序的特殊情況,文中講解非常細致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • C#的FileInfo類實現(xiàn)文件操作實例

    C#的FileInfo類實現(xiàn)文件操作實例

    這篇文章主要介紹了C#的FileInfo類實現(xiàn)文件操作實例,比較實用的功能,需要的朋友可以參考下
    2014-07-07
  • c# rsa注冊實現(xiàn)加密文字

    c# rsa注冊實現(xiàn)加密文字

    這篇文章主要介紹了c# rsa注冊實現(xiàn)加密文字,需要的朋友可以參考下
    2014-04-04
  • C#中{get;set;}的具體使用

    C#中{get;set;}的具體使用

    本文主要介紹了C#中{get;set;}的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02

最新評論

江都市| 东阳市| 黄骅市| 新化县| 昌吉市| 镇坪县| 通榆县| 鹤山市| 颍上县| 河曲县| 巩留县| 甘肃省| 文成县| 宁晋县| 白河县| 苍梧县| 平顺县| 襄汾县| 商水县| 合山市| 蒙阴县| 鄂托克前旗| 简阳市| 宝清县| 岚皋县| 呼玛县| 剑河县| 衡水市| 樟树市| 馆陶县| 嘉兴市| 慈溪市| 旬邑县| 兴城市| 丰城市| 衡南县| 寿阳县| 龙海市| 顺昌县| 宜黄县| 琼海市|