最近在玩一個遊戲,發現開著遊戲掛機比較礙事,因此希望做一個程式可以將該遊戲隱藏。當時想到兩種方法
1、直接尋找遊戲視窗,找到後作處理。
2、枚舉所有視窗,列表顯示,然後再處理。
我這裡按第二種方式做。首先是一些準備工作,如,瞭解如何調用系統API,見以前的博文。枚舉視窗要用的一些
API:EnumWindows,GetWindowText,GetParent,IsWindowVisible.
EnumWindows:枚舉視窗
GetWindowText:取得視窗標題
GetParent:取得當前表單的父表單(非常重要,用於判斷是否為頂級表單)
IsWindowVisible:判斷表單是否可見,用於過濾到不可見表單。
代碼如下:
namespace HideProcess<br />{<br /> public delegate bool CallBack(int hwnd, int y);<br /> public partial class Form1 : Form<br /> {</p><p> [DllImport("user32.dll")]</p><p> public static extern int EnumWindows(CallBack x, int y);<br /> [DllImport("user32")]<br /> public static extern int GetWindowText(int hwnd, StringBuilder lptrString, int nMaxCount);<br /> [DllImport("user32")]<br /> public static extern int GetParent(int hwnd);<br /> [DllImport("user32")]<br /> public static extern int IsWindowVisible(int hwnd);</p><p> public bool Report(int hwnd, int lParam)<br /> {<br /> int pHwnd;<br /> pHwnd = GetParent(hwnd);</p><p> if (pHwnd == 0 && IsWindowVisible(hwnd)==1)<br /> {<br /> StringBuilder sb = new StringBuilder(512);</p><p> GetWindowText(hwnd, sb, sb.Capacity);<br /> if (sb.Length > 0)<br /> {<br /> this.comboBox1.Items.Add(sb.ToString());<br /> }<br /> }<br /> return true;<br /> }<br /> public Form1()<br /> {<br /> InitializeComponent();<br /> }</p><p> private void button1_Click(object sender, EventArgs e)<br /> {<br /> Process[] ProcArray = Process.GetProcesses();<br /> comboBox1.Items.Clear();<br /> EnumWindows(this.Report, 0);<br /> }<br /> }<br />}</p><p>
有一個combobox和button,點擊按鈕,將所有視窗列舉顯示在下拉框。接下來的工作就是設定表單為隱藏。但是有一個缺點
隱藏後無法顯示。留待以後解決。