c#枚举所有的窗体

       最近在玩一个游戏,发现开着游戏挂机比较碍事,因此希望做一个程序可以将该游戏隐藏。当时想到两种方法

       1、直接查找游戏窗口,找到后作处理。2、枚举所有窗口,列表显示,然后再处理。

        我这里按第二种方式做。首先是一些准备工作,如,了解如何调用系统API,见以前的博文。枚举窗口要用的一些

API:EnumWindows,GetWindowText,GetParent,IsWindowVisible.

         EnumWindows:枚举窗口

        GetWindowText:取得窗口标题

      GetParent:取得当前窗体的父窗体(非常重要,用于判断是否为顶级窗体)

      IsWindowVisible:判断窗体是否可见,用于过滤到不可见窗体。

    代码如下:

    namespace HideProcess
{
    public delegate bool CallBack(int hwnd, int y);
    public partial class Form1 : Form
    {


        [DllImport("user32.dll")]

        public static extern int EnumWindows(CallBack x, int y);
        [DllImport("user32")]
        public static extern int GetWindowText(int hwnd, StringBuilder lptrString, int nMaxCount);
        [DllImport("user32")]
        public static extern int GetParent(int hwnd);
        [DllImport("user32")]
        public static extern int IsWindowVisible(int hwnd);

        public  bool Report(int hwnd, int lParam)
        {
            int pHwnd;
            pHwnd = GetParent(hwnd);

            if (pHwnd == 0 && IsWindowVisible(hwnd)==1)
            {
                StringBuilder sb = new StringBuilder(512);
 
                GetWindowText(hwnd, sb, sb.Capacity);
                if (sb.Length > 0)
                {
                    this.comboBox1.Items.Add(sb.ToString());
                }
            }
            return true;
        }      
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Process[] ProcArray = Process.GetProcesses();
            comboBox1.Items.Clear();
            EnumWindows(this.Report, 0);
        }
    }
}

   有一个combobox和button,点击按钮,将所有窗口列举显示在下拉框。接下来的工作就是设置窗体为隐藏。但是有一个缺点

隐藏后无法显示。留待以后解决。

   

posted on 2009-07-16 00:44  网络小筑  阅读(2998)  评论(7编辑  收藏  举报

导航