在C#中调用Win32函数EnumWindows枚举所有窗口。

原文 http://www.cnblogs.com/mfm11111/archive/2009/06/30/1514322.html

开发旺旺群发软件,难点及重要技术点分析(一)

一.        在C#中调用Win32函数EnumWindows枚举所有窗口。

EnumWindows 函数通过借助于应用程序定义的回调函数传递每个窗口句柄枚举所有顶层的屏幕窗口。直到最后一个顶层窗口被枚举或者回调函数返回false ,EnumWindows 函数才会退出停止枚举过程。

下面例子说明如何在 C# 中调用 Win32 API - EnumWindows 枚举所有窗口:

1.首先需要声明一个委托函数用于 Win32 API - EnumWindows 的回调函数:
public delegate bool CallBack(int hwnd, int lParam);

2.然后利用 C# 中的平台调用声明从 USER32.DLL 库中调用 API - EnumWindows,具体参数请参考 MSDN - Win32 API。

[DllImport("user32")]
public static extern int EnumWindows(CallBack x, int y);

3.最后实例化委托,调用 EnumWindows。
CallBack myCallBack = new CallBack(EnumWindowsApp.Report);

4.代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Runtime.InteropServices; 

namespace WindowPosDemo

{

    public delegate bool CallBack(int hwnd, int lParam);



    class Program

    {

        [DllImport("user32")]

        public static extern int EnumWindows(CallBack x, int y);



        static void Main(string[] args)

        {

            CallBack myCallBack = new CallBack(Program.Report);

            EnumWindows(myCallBack, 0);



            Console.ReadKey();

        }

        public static bool Report(int hwnd, int lParam)

        {

            Console.Write("Window handle is :"); 

            Console.WriteLine(hwnd); 

            Console.Read();

            return true; 

        }

    }

}

 

 

二.          现在我们用一个winform来演示查找旺旺窗口的句柄

代码如下:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Text.RegularExpressions;

using System.Diagnostics;

using System.Threading;

using System.Windows.Forms;

using System.Runtime.InteropServices;



namespace WindowPosDemo

{

    public struct WindowInfo

    {

        public IntPtr hWnd;

        public string szWindowName;

        public string szClassName;

    }

    class Program

    {

        [DllImport("shell32.dll")]

        public static extern int ShellExecute(IntPtr hwnd, StringBuilder lpszOp, StringBuilder lpszFile, StringBuilder lpszParams, StringBuilder lpszDir, int FsShowCmd);

        [DllImport("user32.dll")]

        private static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]

        public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, string lparam);

        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]//查找窗口

        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]

        public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

        [DllImport("user32.dll")]

        private static extern int GetWindowTextW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);

        [DllImport("user32.dll")]

        private static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);

        private delegate bool WNDENUMPROC(IntPtr hWnd, int lParam);

        static IntPtr Game;



        static void Main(string[] args)

        {

            WindowInfo[] a = GetAllDesktopWindows();

            int i = 0;

            int index = 0;

            for (i = 0; i < a.Length; i++)

            {

                // MessageBox.Show(a[i].szWindowName.ToString());

                if (a[i].szWindowName.ToString().Contains("mafangmin888"))

                {

                    MessageBox.Show(a[i].szClassName.ToString());

                    index = i;

                }

            }

            Game = a[index].hWnd;

            Console.ReadKey();

        }



        //寻找系统的全部窗口

        static WindowInfo[] GetAllDesktopWindows()

        {

            List<WindowInfo> wndList = new List<WindowInfo>();

            EnumWindows(delegate(IntPtr hWnd, int lParam)

            {

                WindowInfo wnd = new WindowInfo();

                StringBuilder sb = new StringBuilder(256);

                //get hwnd

                wnd.hWnd = hWnd;

                //get window name

                GetWindowTextW(hWnd, sb, sb.Capacity);

                wnd.szWindowName = sb.ToString();

                //get window class

                GetClassNameW(hWnd, sb, sb.Capacity);

                wnd.szClassName = sb.ToString();

                Console.WriteLine("Window handle=" + wnd.hWnd.ToString().PadRight(20) + " szClassName=" + wnd.szClassName.PadRight(20) + " szWindowName=" + wnd.szWindowName);

                //add it into list

                wndList.Add(wnd);

                return true;

            }, 0);

            return wndList.ToArray();

        }

    }

}

 

 

你可能感兴趣的:(windows)