首页 > 编程语言 > C# > QQ大家来找茬助手
2013
09-29

QQ大家来找茬助手

shot

原理就是截取游戏界面,然后图像对比.

用到的类:ScreenShot截图类,来自网络,并参考AAuto的截图代码做了修改,使之更好用

using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace QQ找茬助手
{
    class ScreenShot
    {
        /// <summary>
        /// Creates an Image object containing a screen shot of the entire desktop?
        /// </summary>
        /// <returns></returns>
        public Image CaptureScreen()
        {
            return CaptureWindow(User32.GetDesktopWindow());
        }

        public Image CaptureWindow(IntPtr handle)
        {
            MessageBox.Show("CaptureScreen()");
            return CaptureWindow(handle,0,0,0,0);
        }
        /// <summary>
        /// Creates an Image object containing a screen shot of a specific window?
        /// </summary>
        /// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param>
        /// <returns></returns>
        public Image CaptureWindow(IntPtr handle,int x=0,int y=0,int w=0,int h=0)
        {
            // get te hDC of the target window
            IntPtr hdcSrc = User32.GetWindowDC(handle);
            // get the size
            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(handle, ref windowRect);
            int width = w > 0 ? w : (windowRect.right - windowRect.left - x);
            int height = h > 0 ? h : (windowRect.bottom - windowRect.top - y);
            // create a device context we can copy to
            IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
            // create a bitmap we can copy it to,
            // using GetDeviceCaps to get the width/height
            IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
            // select the bitmap object
            IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);
            // bitblt over
            GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, x, y, GDI32.SRCCOPY | GDI32.CAPTUREBLT);
            // restore selection
            GDI32.SelectObject(hdcDest, hOld);
            // clean up 
            GDI32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle, hdcSrc);
            // get a .NET image object for it
            Image img = Image.FromHbitmap(hBitmap);
            // free up the Bitmap object
            GDI32.DeleteObject(hBitmap);
            return img;
        }
        /// <summary>
        /// Captures a screen shot of a specific window, and saves it to a file?
        /// </summary>
        /// <param name="handle"></param>
        /// <param name="filename"></param>
        /// <param name="format"></param>
        public void CaptureWindowToFile(IntPtr handle, string filename, ImageFormat format)
        {
            Image img = CaptureWindow(handle);
            img.Save(filename, format);
        }
        /// <summary>
        /// Captures a screen shot of the entire desktop, and saves it to a file?
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="format"></param>
        public void CaptureScreenToFile(string filename, ImageFormat format)
        {
            Image img = CaptureScreen();
            img.Save(filename, format);
        }

        /// <summary>
        /// Helper class containing Gdi32 API functions
        /// </summary>
        private class GDI32
        {

            public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter
            public const int CAPTUREBLT = 0x40000000;
            [DllImport("gdi32.dll")]
            public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,
                int nWidth, int nHeight, IntPtr hObjectSource,
                int nXSrc, int nYSrc, int dwRop);
            [DllImport("gdi32.dll")]
            public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth,
                int nHeight);
            [DllImport("gdi32.dll")]
            public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
            [DllImport("gdi32.dll")]
            public static extern bool DeleteDC(IntPtr hDC);
            [DllImport("gdi32.dll")]
            public static extern bool DeleteObject(IntPtr hObject);
            [DllImport("gdi32.dll")]
            public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
        }

        /// <summary>
        /// Helper class containing User32 API functions
        /// </summary>
        private class User32
        {
            [StructLayout(LayoutKind.Sequential)]
            public struct RECT
            {
                public int left;
                public int top;
                public int right;
                public int bottom;
            }
            [DllImport("user32.dll")]
            public static extern IntPtr GetDesktopWindow();
            [DllImport("user32.dll")]
            public static extern IntPtr GetWindowDC(IntPtr hWnd);
            [DllImport("user32.dll")]
            public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
            [DllImport("user32.dll")]
            public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
        }
    }
}

逻辑代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;

namespace QQ找茬助手
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        [DllImport("user32.dll")]
        private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        private extern static bool SetWindowPos(IntPtr hwnd, IntPtr hwndlnsertAfter, int X, int Y, int cx, int cy, int Flags);

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            toolStripStatusLabel2.Text = DateTime.Now.ToLongTimeString();
            Point cp = pictureBox1.PointToClient(new Point(MousePosition.X, MousePosition.Y ));
            string pos = string.Format("{0},{1}", cp.X,cp.Y);
            toolStripStatusLabel4.Text = pos;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            IntPtr hwnd = FindWindow("#32770", "大家来找茬");
            if (hwnd.ToInt32()==0)
            {
                MessageBox.Show("游戏未打开");
                return;
            }
            SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0, 0x1 | 0x4);
            Thread.Sleep(80);
            //Application.DoEvents();
            ScreenShot ss = new ScreenShot();
            Image Lwindow = ss.CaptureWindow(hwnd, 10, 185, 381, 287);
            Image Rwindow = ss.CaptureWindow(hwnd, 403, 185, 381, 287);
            Bitmap left = new Bitmap(Lwindow);
            Bitmap right = new Bitmap(Rwindow);
            Color n = Color.FromName("Green");
            for (int i=1;i<left.Width;i++)
            {
                for (int j=1;j<left.Height;j++)
                {
                    Color Lcolor = left.GetPixel(i,j);
                    Color Rcolor = right.GetPixel(i, j);
                    float Ln = Lcolor.GetHue();
                    float Rn = Rcolor.GetHue();
                    if (Ln!=Rn)
                    {
                        right.SetPixel(i, j, n);
                    }
                }
            }
            pictureBox1.Image = right;
            //left.Dispose();
            //right.Dispose();
            //Lwindow.Dispose();
            //Rwindow.Dispose();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            
        }
    }
}

QQ找茬助手源代码下载
QQ找茬助手编译程序下载

最后编辑:
作者:NINE
这个作者貌似有点懒,什么都没有留下。

留下一个回复

你的email不会被公开。