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

 

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

一.        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.Text;

using System.Runtime.InteropServices;

 

namespace ConsoleApplication1

{

    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);

 

        }

        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 WindowsApplication4

{

    public struct WindowInfo

    {

        public IntPtr hWnd;

        public string szWindowName;

        public string szClassName;

     }

 

  

 

    public partial class Form1 : Form

    {

        [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);

 

        private IntPtr Game;

 

        public Form1()

        {

            InitializeComponent();

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

 

        }

 

        //寻找系统的全部窗口

        public 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();

                //add it into list

                wndList.Add(wnd);

                return true;

            },0);

 

        return wndList.ToArray();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            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 = new IntPtr(a[index].hWnd);

        }

    }

}

 

你可能感兴趣的:(技术)