c#枚舉所有的表單

來源:互聯網
上載者:User

最近在玩一個遊戲,發現開著遊戲掛機比較礙事,因此希望做一個程式可以將該遊戲隱藏。當時想到兩種方法
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,點擊按鈕,將所有視窗列舉顯示在下拉框。接下來的工作就是設定表單為隱藏。但是有一個缺點

隱藏後無法顯示。留待以後解決。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.