SHOW_OBJECT_PROPS

WinAPI.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;

namespace QBear4A
{
    public static class WinAPI
    {
        //const int PROCESS_ALL_ACCESS = 0x1F0FFF;
/* 十进制值 标识符 IBM兼容键盘
--------------------------------------------------------------------------------
1 VK_LBUTTON 鼠标左键
2 VK_RBUTTON 鼠标右键
3 VK_CANCEL Ctrl+Break(通常不需要处理)
4 VK_MBUTTON 鼠标中键
8 VK_BACK Backspace
9 VK_TAB Tab
12 VK_CLEAR Num Lock关闭时的数字键盘5
13 VK_RETURN Enter(或者另一个)
16 VK_SHIFT Shift(或者另一个)
17 VK_CONTROL Ctrl(或者另一个)
18 VK_MENU Alt(或者另一个)
19 VK_PAUSE Pause
20 VK_CAPITAL Caps Lock
27 VK_ESCAPE Esc
32 VK_SPACE Spacebar
33 VK_PRIOR Page Up
34 VK_NEXT Page Down
35 VK_END End
36 VK_HOME Home
37 VK_LEFT 左箭头
38 VK_UP 上箭头
39 VK_RIGHT 右箭头
40 VK_DOWN 下箭头
41 VK_SELECT 可选
42 VK_PRINT 可选
43 VK_EXECUTE 可选
44 VK_SNAPSHOT Print Screen
45 VK_INSERT Insert
46 VK_DELETE Delete
47 VK_HELP 可选
48~57 无 主键盘上的0~9
65~90 无 A~Z
96~105 VK_NUMPAD0~VK_NUMPAD9 Num Lock打开时数字键盘上的0~9
106 VK_NULTIPLY 数字键盘上的*
107 VK_ADD 数字键盘上的+
108 VK_SEPARATOR 可选
109 VK_SUBTRACT 数字键盘上的-
110 VK_DECIMAL 数字键盘上的.
111 VK_DIVIDE 数字键盘上的/
112~135 VK_F1~VK_F24 功能键F1~F24
144 VK_NUMLOCK Num Lock
145 VK_SCROLL Scroll Lock
*/
        const int WM_KEYDOWN = 0x0100;
        const int WM_KEYUP = 0x0101;
        const int WM_SYSKEYDOWN = 0x0104;
        const int WM_SYSKEYUP = 0x0105;

        const int WM_MOUSEMOVE = 0x0200;
        public const int WM_LBUTTONDOWN = 0x0201;
        public const int WM_LBUTTONUP = 0x0202;
        const int WM_LBUTTONDBLCLK = 0x0203;
        const int WM_RBUTTONDOWN = 0x0204;
        const int WM_RBUTTONUP = 0x0205;
        const int WM_RBUTTONDBLCLK = 0x0206;
        const int WM_USER = 0x0400;

        const int MK_LBUTTON = 0x0001;
        const int MK_RBUTTON = 0x0002;
        const int MK_SHIFT = 0x0004;
        const int MK_CONTROL = 0x0008;
        const int MK_MBUTTON = 0x0010;

        const int MK_XBUTTON1 = 0x0020;
        const int MK_XBUTTON2 = 0x0040;

        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [DllImport("Kernel32.dll")]
        public static extern int GetLastError();

        public const uint PROCESS_VM_READ = 0x0010;
        public const uint PROCESS_VM_WRITE = 0x0020;
        public const uint PROCESS_ALL_ACCESS = 0x001F0FFF;
        //HANDLE OpenProcess(
        //    DWORD dwDesiredAccess,// access flag
        //    BOOL bInheritHandle, // handle inheritance flag
        //    DWORD dwProcessId  // process identifier
        //    );
        [DllImport("kernel32.dll")]
        public static extern
            IntPtr OpenProcess(UInt32 dwDesiredAccess, bool bInheritHandle, UInt32 dwProcessId);

        //BOOL CloseHandle(
        //    HANDLE hObject  // handle to object to close
        //    );
        [DllImport("kernel32.dll")]
        public static extern
            bool CloseHandle(IntPtr hObject);

        //BOOL WriteProcessMemory(
        //    HANDLE hProcess, // handle to process whose memory is written to
        //    LPVOID lpBaseAddress, // address to start writing to
        //    LPVOID lpBuffer, // pointer to buffer to write data to
        //    DWORD nSize, // number of bytes to write
        //    LPDWORD lpNumberOfBytesWritten  // actual number of bytes written
        //    );
        [DllImport("kernel32.dll")]
        public static extern
            bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, UInt32 nSize, IntPtr lpNumberOfBytesWritten);

        //BOOL ReadProcessMemory(
        //    HANDLE hProcess, // handle of the process whose memory is read
        //    LPCVOID lpBaseAddress, // address to start reading
        //    LPVOID lpBuffer, // address of buffer to place read data
        //    DWORD nSize, // number of bytes to read
        //    LPDWORD lpNumberOfBytesRead  // address of number of bytes read
        //    );
        [DllImport("kernel32.dll")]
        public static extern
            bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, UInt32 nSize, IntPtr lpNumberOfBytesRead);
        [DllImport("kernel32.dll")]
        public static extern
            bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, UInt32 nSize, IntPtr lpNumberOfBytesRead);
        [DllImport("kernel32.dll")]
        public static extern
            bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, int[] lpBuffer, UInt32 nSize, IntPtr lpNumberOfBytesRead);

        public static bool ReadProcessMemory(IntPtr hProcess,
            uint nBaseAddress, out string strRead, uint nSize,Encoding encoding)
        {
            strRead = "";
            byte[] bBuffer = new byte[nSize];
            try
            {
                if(!ReadProcessMemory(hProcess,(IntPtr)nBaseAddress,bBuffer,nSize,(IntPtr)0))
                {
                    return false;
                }
                strRead = encoding.GetString(bBuffer);
                if (strRead.IndexOf("\0")>=0)
                {
                    strRead = strRead.Split(new string[1] { "\0" }, StringSplitOptions.None)[0];
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
        public static bool ReadProcessMemory(IntPtr hProcess,
            uint nBaseAddress, out string strRead, uint nSize)
        {
            return ReadProcessMemory(hProcess, nBaseAddress,
                out strRead, nSize, Encoding.Default);
        }

        [DllImport("user32.dll")]
        public static extern int FindWindowEx(IntPtr hwndParent, int hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("user32.dll")]
        public static extern int FindWindow(string strclassName, string strWindowName);

        [DllImport("user32.dll")]
        public static extern int GetLastActivePopup(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern int AnyPopup();

        [DllImport("user32.dll")]
        public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
        [DllImport("user32.dll")]
        public static extern int GetClassName(IntPtr hwnd, StringBuilder lpClassName, int nMaxCount); 
        /// <summary>
        /// 回调函数代理
        /// </summary>
        public delegate bool CallBack(IntPtr hwnd, int lParam);
        [DllImport("user32.dll")]
        public static extern int EnumThreadWindows(int dwThreadId, CallBack lpfn, int lParam);

        [DllImport("user32.dll")]
        public static extern int EnumWindows(CallBack lpfn, int lParam);

        [DllImport("user32.dll")]
        public static extern int EnumChildWindows(IntPtr hWndParent, CallBack lpfn, int lParam);

        public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
        public static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
        public static readonly IntPtr HWND_TOP = new IntPtr(0);
        public static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
        public const int SWP_NOSIZE = 0x0001;
        public const int SWP_NOMOVE = 0x0002;
        public const int SWP_NOZORDER = 0x0004;
        public const int SWP_NOREDRAW = 0x0008;
        public const int SWP_NOACTIVATE = 0x0010;
        public const int SWP_FRAMECHANGED = 0x0020;  /* The frame changed: send WM_NCCALCSIZE */
        public const int SWP_SHOWWINDOW = 0x0040;
        public const int SWP_HIDEWINDOW = 0x0080;
        public const int SWP_NOCOPYBITS = 0x0100;
        public const int SWP_NOOWNERZORDER = 0x0200;  /* Don't do owner Z ordering */
        public const int SWP_NOSENDCHANGING = 0x0400;  /* Don't send WM_WINDOWPOSCHANGING */

        [DllImport("user32.dll")]
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X,
           int Y, int cx, int cy, uint uFlags);       
        /// <summary>
        /// 把指定句柄窗口置于最前
        /// </summary>
        /// <param name="hWnd">窗口句柄</param>
        /// <returns></returns>
        public static bool BringWindowToFront(IntPtr hWnd)
        {
            return SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, 0x4000 | 0x0001 | 0x0002);  
        }

        [Flags]
        public enum MouseEventFlag : uint
        {
            Move = 0x0001,
            LeftDown = 0x0002,
            LeftUp = 0x0004,
            RightDown = 0x0008,
            RightUp = 0x0010,
            MiddleDown = 0x0020,
            MiddleUp = 0x0040,
            XDown = 0x0080,
            XUp = 0x0100,
            Wheel = 0x0800,
            VirtualDesk = 0x4000,
            Absolute = 0x8000
        }
        [DllImport("user32.dll")]
        public static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);

        //[DllImport("user32.dll")]
        //public static extern IntPtr GetDC(IntPtr hwnd);
        [DllImport("user32.dll")]
        public static extern IntPtr ReleaseDC(IntPtr hc, IntPtr hDest);
        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowDC(IntPtr hwnd);
        //[DllImport("user32.dll")]
        //public static extern IntPtr GetForegroundWindow();
        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }
        [DllImport("user32.dll")]
        public static extern int GetWindowRect(IntPtr hwnd, ref  RECT lpRect);
        [DllImport("GDI32.dll")]
        public static extern bool BitBlt(int hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, int hdcSrc, int nXSrc, int nYSrc, int dwRop);
        [DllImport("GDI32.dll")]
        public static extern int CreateCompatibleBitmap(int hdc, int nWidth, int nHeight);
        [DllImport("GDI32.dll")]
        public static extern int CreateCompatibleDC(int hdc);
        [DllImport("GDI32.dll")]
        public static extern bool DeleteDC(int hdc);
        [DllImport("GDI32.dll")]
        public static extern bool DeleteObject(int hObject);
        [DllImport("GDI32.dll")]
        public static extern int GetDeviceCaps(int hdc, int nIndex);
        [DllImport("GDI32.dll")]
        public static extern int SelectObject(int hdc, int hgdiobj);

        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("user32.dll", SetLastError = true)]
        static extern bool PostMessage(HandleRef hWnd, uint Msg, IntPtr wParam,
           IntPtr lParam);
        public static bool PostMessageSafe(HandleRef hWnd, uint msg, IntPtr wParam, IntPtr lParam)
        {
            bool returnValue = PostMessage(hWnd, msg, wParam, lParam);
            if (returnValue == false)
            {
                // An error occured
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
            return returnValue;
        }
        public static int MakeLong(int LoWord, int HiWord)
        {
            return (HiWord << 16) | (LoWord & 0xffff);
        }

        public static IntPtr MakeLParam(int LoWord, int HiWord)
        {
            return (IntPtr)((HiWord << 16) | (LoWord & 0xffff));
        }

        public static int HiWord(int Number)
        {
            return (Number >> 16) & 0xffff;
        }

        public static int LoWord(int Number)
        {
            return Number & 0xffff;
        }

        [DllImport("user32.dll", SetLastError = true)]
        static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
    }
}

 

Helper.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing;

namespace QBear4A
{
    public class Helper
    {
        const string qbProcessName = "QQPets3D";
        const string qbText = "QQPets3D";
        const string qbClassName = "Pet_Win_288";
        const string trayClassName = "ClassName774453";
        const string dgText = "工作学习计划表";
        const string dgClassName = "TWINCONTROL";
        const int windowTextLength = 16;
        const int classNameLength = 16;

        public struct QBearInfo
        {
            public Process process;
            public IntPtr hwnd1;//q熊主形象窗体
            public IntPtr hwnd2;//发送托盘消息的窗体
            public IntPtr hwnd3;//工作学习计划窗体
            public int nBaseAddress1;
            public int nBaseAddress2;
            public int nBaseAddress3;
            public int nBaseAddress4;
            public string NiCheng;
            public string ZhuRen;
            public string ShengRi;
            public int ShengRiDay;
            public int ShengRiMonth;
            public int ZongChengZhang;
            public int DengJi;
            public int YinShi;
            public int YinShiMax;
            public int QingJie;
            public int QingJieMax;
            public int TiLi;
            public int TiLiMax;
            public int XinQing;
            public int XinQingMax;
            public string ZaiXian;
            public int ZaiXianHour;
            public int ZaiXianMinute;
            public string ZhuangTai;
        }
        public static QBearInfo[] qbInfos;
        public static void GetQBearInfo()
        {
            Process[] qbProcess = Process.GetProcessesByName(qbProcessName);
            if (qbProcess.Length > 0)
            {
                qbInfos = new QBearInfo[qbProcess.Length];
                //handle = 0;
                int i = 0;
                foreach (Process p in qbProcess)
                {
                    qbInfos[i].process = p;
                    foreach (ProcessThread pt in p.Threads)
                    {
                        WinAPI.EnumThreadWindows(pt.Id, callBackEnumThreadWindows, i);
                        //if (handle > 0)
                        //{
                        //    qbInfos[i].hwnd1 = handle;
                        //    break;
                        //}
                    }
                    i++;
                }
            }
        }

        /// <summary>
        /// 进程窗口回调函数代理
        /// </summary>
        static WinAPI.CallBack callBackEnumThreadWindows = new WinAPI.CallBack(ThreadWindowProcess);
        static StringBuilder sbTemp = new StringBuilder();
        static string strTemp,strTemp1,strTemp2;
        //static int handle = -1;
        /// <summary>
        /// 进程窗口回调处理函数
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        public static bool ThreadWindowProcess(IntPtr hwnd, int lParam)
        {
            if (hwnd == IntPtr.Zero)
                return false;
            WinAPI.GetClassName(hwnd, sbTemp, classNameLength);
            strTemp1 = sbTemp.ToString();
            WinAPI.GetWindowText(hwnd, sbTemp, windowTextLength);
            strTemp2 = sbTemp.ToString();
            if (strTemp1.IndexOf(trayClassName) == 0)
            {
                qbInfos[lParam].hwnd2 = hwnd;
            }
            else if (strTemp1.IndexOf(qbClassName) == 0&&
                strTemp2.IndexOf(qbText) == 0)
            {
                qbInfos[lParam].hwnd1 = hwnd;
            }
            else if (strTemp1.IndexOf(dgClassName) == 0 &&
                strTemp2.IndexOf(dgText) == 0)
            {
                qbInfos[lParam].hwnd3 = hwnd;
            }
            return true;
        }

        public static void ShowDangAn(int index)
        {
            if (qbInfos == null ||
                qbInfos.Length <= index)
            {
                return;
            }
            WinAPI.SendMessage(qbInfos[index].hwnd2, 0x0465, 0, 0x00000200);
        }

        public static void ShowCtrlBar(int index)
        {
            if (qbInfos == null ||
                qbInfos.Length <= index)
            {
                return;
            }
            ClickWnd(qbInfos[index].hwnd1, 70, 100);
        }
        public static Bitmap Snapshot(int index)
        {
            if (qbInfos == null ||
                qbInfos.Length <= index)
            {
                return null;
            }

            IntPtr winDC = WinAPI.GetWindowDC(Helper.qbInfos[index].hwnd1);
            WinAPI.RECT rect = new WinAPI.RECT();
            WinAPI.GetWindowRect(Helper.qbInfos[index].hwnd1, ref  rect);
            Bitmap bmp = new Bitmap(rect.right - rect.left, rect.bottom - rect.top);
            Graphics g = Graphics.FromImage(bmp);
            IntPtr hdc = g.GetHdc();
            WinAPI.BitBlt((int)hdc, 0, 0, bmp.Width, bmp.Height, (int)winDC, 0, 0, 0x00CC0020);
            g.ReleaseHdc(hdc);
            g.Dispose();
            WinAPI.ReleaseDC(Helper.qbInfos[index].hwnd1, winDC);
            WinAPI.DeleteDC((int)hdc);
            return bmp;

            //int hdcSrc = (int)WinAPI.GetWindowDC(qbInfos[index].hwnd1),
            //    hdcDest = WinAPI.CreateCompatibleDC(hdcSrc),
            //    hBitmap = WinAPI.CreateCompatibleBitmap(hdcSrc,
            //        WinAPI.GetDeviceCaps(hdcSrc, 8), WinAPI.GetDeviceCaps(hdcSrc, 10));
            //WinAPI.SelectObject(hdcDest, hBitmap);
            //WinAPI.BitBlt(hdcDest, 0, 0, WinAPI.GetDeviceCaps(hdcSrc, 8),
            //    WinAPI.GetDeviceCaps(hdcSrc, 10), hdcSrc, 0, 0, 0x00CC0020);
            //Bitmap image = new Bitmap(
            //    Image.FromHbitmap(new IntPtr(hBitmap)),
            //    Image.FromHbitmap(new IntPtr(hBitmap)).Width,
            //    Image.FromHbitmap(new IntPtr(hBitmap)).Height);
            //WinAPI.ReleaseDC(qbInfos[index].hwnd1, (IntPtr)hdcSrc);
            //WinAPI.DeleteDC(hdcDest);
            ////WinAPI.DeleteObject(hBitmap);
            //return image;
        }

        public static void ClickWnd(IntPtr hwnd, int dx, int dy)
        {
            WinAPI.RECT rect = new WinAPI.RECT();
            WinAPI.GetWindowRect(hwnd, ref rect);
            Point p = Cursor.Position;
            Cursor.Hide();
            Cursor.Position = new Point(rect.left+dx, rect.top+dy);
            WinAPI.mouse_event(WinAPI.MouseEventFlag.LeftDown | WinAPI.MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
            Cursor.Position = p;
            Cursor.Show();
        }
        /// <summary>
        /// 搜索几个基地址
        /// </summary>
        /// <param name="nProcessId"></param>
        /// <param name="nBaseAddress1"></param>
        /// <param name="nBaseAddress2"></param>
        /// <param name="nBaseAddress3"></param>
        /// <param name="nBaseAddress4"></param>
        /// <returns></returns>
        public static bool SearchBaseAddress(int nProcessId,out uint nBaseAddress1,
            out uint nBaseAddress2,out uint nBaseAddress3,out uint nBaseAddress4)
        {
            nBaseAddress1 = 0;
            nBaseAddress2 = 0;
            nBaseAddress3 = 0;
            nBaseAddress4 = 0;

            string flag1 = "sqlite_version", flag12 = "P3DLoginMod",
                   flag2 = "Online_Txt",
                   flag3 = "Estate_Txt";
            byte[] bFlag1 = Encoding.Default.GetBytes(flag1);
            byte[] bFlag2 = Encoding.Default.GetBytes(flag2);
            byte[] bFlag3 = Encoding.Default.GetBytes(flag3);
            byte[] bFlag4 = new byte[24];

            uint nStepLength = 0x2000;//内存搜索步长,经测试0x2000时速度最快
            bool bFound = true;
            int nFound = 0;//标志已经搜索到了几个地址
            byte[] bFlag;//暂存当前搜索的标识
            byte[] bBuffer;
            string strBuffer;

            IntPtr hProcess = WinAPI.OpenProcess(
                WinAPI.PROCESS_ALL_ACCESS, false, (uint)nProcessId);
            if ((int)hProcess <= 0)
                return false;
            //DateTime dt = DateTime.Now;

            nFound = 0;
            bFlag = bFlag1;
            bBuffer = new byte[nStepLength + bFlag.Length - 1];
            for (uint nBaseAddress = 0x0400000;//0x01A00000;
                nBaseAddress < 0x7FFFFFFF; nBaseAddress += nStepLength)
            {
                if (WinAPI.ReadProcessMemory(hProcess,
                    (IntPtr)nBaseAddress, bBuffer, (uint)bBuffer.Length, IntPtr.Zero))
                {
                    uint nDeviation = 0;//偏移
                    for (; nDeviation < nStepLength; nDeviation++)
                    {
                        bFound = true;
                        for (int n = 0; n < bFlag.Length; n++)
                        {
                            if (bFlag[n] != bBuffer[nDeviation + n])
                            {
                                bFound = false;
                                break;
                            }
                        }
                        if (bFound)
                        {
                            break;
                        }
                    }
                    if (bFound)
                    {
                        switch (nFound)
                        {
                            case 0:
                                if (WinAPI.ReadProcessMemory(hProcess, nBaseAddress + nDeviation - 0x73,
                                    out strBuffer, (uint)flag12.Length))
                                {
                                    if (strBuffer.IndexOf(flag12) == 0)
                                    {
                                        nFound = 1;
                                        bFlag = bFlag2;
                                        bBuffer = new byte[nStepLength + bFlag.Length - 1];

                                        nBaseAddress1 = nBaseAddress + nDeviation;

                                        WinAPI.ReadProcessMemory(hProcess, (IntPtr)(nBaseAddress1 + 0x188),
                                            bFlag4, (uint)bFlag4.Length, IntPtr.Zero);
                                    }
                                }
                                break;
                            case 1:
                                if (WinAPI.ReadProcessMemory(hProcess, nBaseAddress + nDeviation + 0x2c,
                                    out strBuffer, (uint)flag2.Length))
                                {
                                    if (strBuffer.IndexOf(flag2) == 0)
                                    {
                                        nFound = 2;
                                        bFlag = bFlag3;
                                        bBuffer = new byte[nStepLength + bFlag.Length - 1];

                                        nBaseAddress2 = nBaseAddress + nDeviation;
                                    }
                                }
                                break;
                            case 2:
                                if (WinAPI.ReadProcessMemory(hProcess, nBaseAddress + nDeviation + 0x2c,
                                    out strBuffer, (uint)flag3.Length))
                                {
                                    if (strBuffer.IndexOf(flag3) == 0)
                                    {
                                        nFound = 3;
                                        bFlag = bFlag4;
                                        bBuffer = new byte[nStepLength + bFlag.Length - 1];

                                        nBaseAddress3 = nBaseAddress + nDeviation;
                                    }
                                }
                                break;
                            case 3:
                                nFound = 4;
                                bFlag = bFlag1;
                                bBuffer = new byte[nStepLength + bFlag.Length - 1];

                                nBaseAddress4 = nBaseAddress + nDeviation + 0x29F;
                                goto end;
                            //    break;
                            //case 4:// 所有基地址搜索完毕
                            //    goto end;
                            //    break;
                            default:
                                goto end;
                                //break;
                        }
                        nBaseAddress = nBaseAddress + nDeviation +
                          (uint)bFlag.Length - nStepLength;// 从接下来的地址继续搜索
                    }
                }
            }
        end:
            WinAPI.CloseHandle(hProcess);
            //TimeSpan tp = DateTime.Now - dt;
            //MessageBox.Show(tp.Minutes + "分" + tp.Seconds + "秒" + tp.Milliseconds + "毫秒");
            return nFound==4;
        }
        /// <summary>
        /// 读取昵称
        /// </summary>
        /// <param name="nBaseAddress"></param>
        /// <returns></returns>
        public static bool GetQBearState(ref QBearInfo qbInfo)
        {
            return true;
        }
    }
}

 

QBearHelper.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Collections;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;

namespace QBear4A
{
    public class QBearHelper
    {
        public int id;//序号
        public Process process;//所在进程
        public string qq;//所属qq号
        public IntPtr[] hWnd;//发送托盘消息以刷新熊熊档案显示的窗体
        public IntPtr hWnd1;//q熊主形象窗体
        public IntPtr hWnd2;//熊熊档案窗体
        public IntPtr hWnd3;//工作学习计划窗体
        public IntPtr hWnd5;//ToolBar
        public uint nBaseAddress1;//几个基地址
        public uint nBaseAddress2;
        public uint nBaseAddress3;
        public uint nBaseAddress4;
        public string NiCheng;//昵称
        public string ZhuRen;//主人
        public string ShengRi;//生日
        public int ShengRiDay;//生日-日
        public int ShengRiMonth;//生日-年
        public int ZongChengZhang;//总成长
        public int DengJi;//等级
        public int YinShi;//饮食
        public int YinShiMax;//饮食上限
        public int QingJie;//清洁
        public int QingJieMax;//清洁上限
        public int TiLi;//体力
        public int TiLiMax=1000;//体力上限
        public int XinQing;//心情
        public int XinQingMax=1000;//心情上限
        public string ZaiXian;//在线时长
        public int ZaiXianHour;//在线时长-小时数
        public int ZaiXianMinute;//在线时长-分钟数
        public string ZhuangTai;//状态
        public bool Running;//标志保姆是否运行
        /// <summary>
        /// 保姆实例
        /// </summary>
        public static QBearHelper[] Helper;
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="index"></param>
        /// <param name="p"></param>
        public QBearHelper(int index, Process p)
        {
            id = index;
            process = p;
            if (id > 0)
            {
                NiCheng = "尚未支持...";
                return;
            }
            if (SearchWindow())
            {
                if (SearchBaseAddress1() &&
                    SearchBaseAddress2())
                    ReadBearInfo();
            }
        }

        const string qbProcessName = "QQPets3D";
        public static bool SearchQBear()
        {
            Process[] qbProcess = Process.GetProcessesByName(qbProcessName);
            if (qbProcess.Length > 0)
            {
                Helper = new QBearHelper[qbProcess.Length];
                for (int i = 0; i < qbProcess.Length; i++)
                {
                    Helper[i] = new QBearHelper(i, qbProcess[i]);
                }
            }
            else
            {
                Helper = null;
            }
            return true;
        }

        public bool Start()
        {
            Running = true;
            return true;
        }

        public bool Stop()
        {
            Running = false;
            return true;
        }

        #region SearchWindow
        private Object thisLock = new Object();
        /// <summary>
        /// 搜索进程内的几个窗体
        /// 注意:由于使用了几个静态变量保存搜索结果,所以不能在多线程中同时调用。
        /// </summary>
        bool SearchWindow()
        {
            lock (thisLock)
            {
                hWndTemps.Clear();
                hWndTemp1 = IntPtr.Zero;
                hWndTemp2 = IntPtr.Zero;
                hWndTemp3 = IntPtr.Zero;
                hWndTemp5 = IntPtr.Zero;
                qqTemp = "";
                foreach (ProcessThread pt in process.Threads)
                {
                    WinAPI.EnumThreadWindows(pt.Id, callBackEnumThreadWindows, 0);
                }
                if (hWndTemps.Count <= 0 || hWndTemp1 == IntPtr.Zero ||
                    hWndTemp2 == IntPtr.Zero || hWndTemp5 == IntPtr.Zero || qqTemp == "")// || hWndTemp3 == IntPtr.Zero)
                {
                    return false;
                }
                hWnd = new IntPtr[hWndTemps.Count];
                for (int i = 0; i < hWndTemps.Count; i++)
                {
                    hWnd[i] = (IntPtr)hWndTemps[i];
                }
                hWnd1 = hWndTemp1;
                hWnd2 = hWndTemp2;
                hWnd3 = hWndTemp3;
                hWnd5 = hWndTemp5;
                qq = qqTemp;
                return true;
            }
        }
        /// <summary>
        /// 进程窗口回调函数代理
        /// </summary>
        WinAPI.CallBack callBackEnumThreadWindows = new WinAPI.CallBack(ThreadWindowProcess);
        const string className = "ClassName";
        const string text1 = "QQPets3D";
        const string className1 = "Pet_Win_144";
        const string text2 = "";
        const string className2 = "TWINCONTROL";
        const string text3 = "工作学习计划表";
        const string className3 = "TWINCONTROL";
        const string className4 = "QQPET_", className41 = "_[", className42 = "]";
        const string text5 = "";
        const string className5 = "TWINCONTROL";
        const int windowTextLength = 0x50;
        const int classNameLength = 0x50;

        static ArrayList hWndTemps=new ArrayList();
        static IntPtr hWndTemp1;
        static IntPtr hWndTemp2;
        static IntPtr hWndTemp3;
        static IntPtr hWndTemp5;
        static string qqTemp;
        static StringBuilder sbTemp = new StringBuilder(Math.Max(windowTextLength, classNameLength));
        static string strTemp, strTemp1, strTemp2;
        /// <summary>
        /// 进程窗口回调处理函数
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        static bool ThreadWindowProcess(IntPtr hwnd, int lParam)
        {
            if (hwnd == IntPtr.Zero)
                return false;
            WinAPI.GetClassName(hwnd, sbTemp, classNameLength);
            strTemp1 = sbTemp.ToString();

            if (strTemp1.Length > className.Length &&
                strTemp1.IndexOf(className) == 0)
            {
                uint n = 0;
                try
                {
                    n = uint.Parse(strTemp1.Substring(9));
                    if (n > 0)
                    {
                        WinAPI.RECT rect = new WinAPI.RECT();
                        WinAPI.GetWindowRect(hwnd, ref rect);
                        if (//rect.right - rect.left == 108 &&
                            rect.bottom - rect.top == 27)
                        {
                            hWndTemps.Add(hwnd);
                        }
                    }
                }
                catch
                {
                }
            }
            else if (strTemp1.IndexOf(className1) == 0)
            {
                WinAPI.GetWindowText(hwnd, sbTemp, windowTextLength);
                strTemp2 = sbTemp.ToString();
                if (strTemp2.IndexOf(text1) == 0)
                {
                    hWndTemp1 = hwnd;
                }
            }
            else if (strTemp1.IndexOf(className2) == 0)
            {
                WinAPI.GetWindowText(hwnd, sbTemp, windowTextLength);
                strTemp2 = sbTemp.ToString();
                if (strTemp2 =="")
                {
                    WinAPI.RECT rect = new WinAPI.RECT();
                    WinAPI.GetWindowRect(hwnd, ref rect);
                    if (rect.right - rect.left == 130)// &&
                    //rect.bottom - rect.top == 162)//高度会变化162,200
                    {
                        hWndTemp2 = hwnd;
                    }
                    else if (rect.right - rect.left == 123 &&
                    rect.bottom - rect.top == 25)
                    {
                        hWndTemp5 = hwnd;
                    }

                }
                else if (strTemp2.IndexOf(text3) == 0)
                {
                    hWndTemp3 = hwnd;
                }
            }
            else if (strTemp1.IndexOf(className4) == 0)
            {
                if (strTemp1.IndexOf(className41, className4.Length) > className4.Length &&
                    strTemp1.EndsWith(className42))
                {
                    WinAPI.GetWindowText(hwnd, sbTemp, windowTextLength);
                    strTemp2 = sbTemp.ToString();
                    if (strTemp2.IndexOf(strTemp1 + className41) == 0)
                    {
                        qqTemp = strTemp1.Substring(strTemp1.IndexOf(className41, className4.Length) + className41.Length);
                        qqTemp = qqTemp.Substring(0, qqTemp.Length - className42.Length);
                    }
                }
            }
            return true;
        }
        #endregion

        #region BaseAddress
        const string //flag1 = "sqlite_version", flag12 = "P3DLoginMod",//昵称,主人,生日,总成长,等级,饮食,清洁,体力,心情
                   flag2 = "Value_0",//昵称
                   flag3 = "Online_Txt",//今日在线
                   flag4 = "Estate_Txt";//状态
        const uint nStepLength = 0x2000;//内存搜索步长,经测试0x2000时速度最快
        /// <summary>
        /// 从内存中搜索几个基地址
        /// </summary>
        /// <returns></returns>
        bool SearchBaseAddress1()
        {
            nBaseAddress2 = 0;
            nBaseAddress3 = 0;
            nBaseAddress4 = 0;

            byte[] bFlag2 = Encoding.Default.GetBytes(flag2);
            byte[] bFlag3 = Encoding.Default.GetBytes(flag3);
            byte[] bFlag4 = Encoding.Default.GetBytes(flag4);

            bool bFound = true;
            int nFound = 0;//标志已经搜索到了几个地址
            byte[] bFlag;//暂存当前搜索的标识
            byte[] bBuffer;
            string strBuffer;

            IntPtr hProcess = WinAPI.OpenProcess(
                WinAPI.PROCESS_ALL_ACCESS, false, (uint)process.Id);//nProcessId);
            if (hProcess == IntPtr.Zero)
                return false;
            //DateTime dt = DateTime.Now;
                nFound = 1;
                bFlag = bFlag2;

            bBuffer = new byte[nStepLength + bFlag.Length - 1];
            for (uint nBaseAddress = 0x03000000;//0x00400000;//0x02010000;
                nBaseAddress < 0x7FFFFFFF; nBaseAddress += nStepLength)
            {
                if (WinAPI.ReadProcessMemory(hProcess,
                    (IntPtr)nBaseAddress, bBuffer, (uint)bBuffer.Length, IntPtr.Zero))
                {
                    uint nDeviation = 0;//偏移
                    for (; nDeviation < nStepLength; nDeviation++)
                    {
                        bFound = true;
                        for (int n = 0; n < bFlag.Length; n++)
                        {
                            if (bFlag[n] != bBuffer[nDeviation + n])
                            {
                                bFound = false;
                                break;
                            }
                        }
                        if (bFound)
                        {
                            break;
                        }
                    }
                    if (bFound)
                    {
                        switch (nFound)
                        {
                            case 1:
                                if (WinAPI.ReadProcessMemory(hProcess, nBaseAddress + nDeviation + 0x24,
                                    out strBuffer, (uint)flag2.Length))
                                {
                                    if (strBuffer.IndexOf(flag2) == 0)
                                    {
                                        nFound = 2;
                                        bFlag = bFlag3;
                                        bBuffer = new byte[nStepLength + bFlag.Length - 1];

                                        nBaseAddress2 = nBaseAddress + nDeviation;
                                    }
                                }
                                break;
                            case 2:
                                if (WinAPI.ReadProcessMemory(hProcess, nBaseAddress + nDeviation + 0x2c,
                                    out strBuffer, (uint)flag3.Length))
                                {
                                    if (strBuffer.IndexOf(flag3) == 0)
                                    {
                                        nFound = 3;
                                        bFlag = bFlag4;
                                        bBuffer = new byte[nStepLength + bFlag.Length - 1];

                                        nBaseAddress3 = nBaseAddress + nDeviation;
                                    }
                                }
                                break;
                            case 3:
                                if (WinAPI.ReadProcessMemory(hProcess, nBaseAddress + nDeviation + 0x2c,
                                    out strBuffer, (uint)flag4.Length))
                                {
                                    if (strBuffer.IndexOf(flag4) == 0)
                                    {
                                        nFound = 4;
                                        bFlag = null;
                                        bBuffer = null;

                                        nBaseAddress4 = nBaseAddress + nDeviation;
                                        goto end;
                                    }
                                }
                                break;
                            default:
                                goto end;
                            //break;
                        }
                        nBaseAddress = nBaseAddress + nDeviation +
                          (uint)bFlag.Length - nStepLength;// 从接下来的地址继续搜索
                    }
                }
            }
        end:
            WinAPI.CloseHandle(hProcess);
            //TimeSpan tp = DateTime.Now - dt;
            //System.Windows.Forms.MessageBox.Show(tp.Minutes + "分" + tp.Seconds + "秒" + tp.Milliseconds + "毫秒","搜索耗时");
            return nFound == 4;
        }
        /// <summary>
        /// 从内存中搜索[昵称,主人,生日,总成长,等级,饮食,清洁,体力,心情]数据段的基地址
        /// </summary>
        /// <returns></returns>
        bool SearchBaseAddress2()
        {
            nBaseAddress1 = 0;

            bool bFound = true;
            //int nFound = 0;//标志已经搜索到了几个地址
            byte[] bFlag;//暂存当前搜索的标识
            byte[] bBuffer;
            int[] nBuffer = new int[1];
            if (!ReadBearInfo())
            {
                return false;
            }
            bFlag = Encoding.Default.GetBytes(NiCheng);
            bBuffer = new byte[nStepLength + bFlag.Length - 1];
            IntPtr hProcess = WinAPI.OpenProcess(
                WinAPI.PROCESS_ALL_ACCESS, false, (uint)process.Id);//nProcessId);
            if (hProcess == IntPtr.Zero)
                return false;
            for (uint nBaseAddress = 0x01000000;
                nBaseAddress < nBaseAddress2; nBaseAddress += nStepLength)
            {
                if (WinAPI.ReadProcessMemory(hProcess,
                    (IntPtr)nBaseAddress, bBuffer, (uint)bBuffer.Length, IntPtr.Zero))
                {
                    uint nDeviation = 0;//偏移
                    for (; nDeviation < nStepLength; nDeviation++)
                    {
                        bFound = true;
                        for (int n = 0; n < bFlag.Length; n++)
                        {
                            if (bFlag[n] != bBuffer[nDeviation + n])
                            {
                                bFound = false;
                                break;
                            }
                        }
                        if (bFound)
                        {
                            break;
                        }
                    }
                    if (bFound)
                    {
                        if (WinAPI.ReadProcessMemory(hProcess, (IntPtr)(nBaseAddress + nDeviation + 0x21 + 0x23 + 0x2 + 0x2 + 0x4 + 0x4),
                            nBuffer, 4, IntPtr.Zero) && nBuffer[0] > 0 &&
                            WinAPI.ReadProcessMemory(hProcess, (IntPtr)(nBaseAddress + nDeviation + 0x21 + 0x23 + 0x2 + 0x2 + 0x4 + 0x4 + 0x4 + 0x4 + 0x4),
                            nBuffer, 4, IntPtr.Zero) && nBuffer[0] <= TiLiMax && nBuffer[0] >= 0 &&
                            WinAPI.ReadProcessMemory(hProcess, (IntPtr)(nBaseAddress + nDeviation + 0x21 + 0x23 + 0x2 + 0x2 + 0x4 + 0x4 + 0x4 + 0x4 + 0x4 + 0x4),
                            nBuffer, 4, IntPtr.Zero) && nBuffer[0] <= XinQingMax && nBuffer[0] >= 0)
                        {
                            nBaseAddress1 = nBaseAddress + nDeviation;
                            WinAPI.CloseHandle(hProcess);
                            return true;
                        }
                    }
                }
            }
            WinAPI.CloseHandle(hProcess);
            return false;
        }

        /// <summary>
        /// 验证当前基地址是否还有效
        /// </summary>
        /// <returns></returns>
        bool VerifyBaseAddress()
        {
            if (nBaseAddress1 < 0x01000000 ||
                nBaseAddress2 < 0x00300000 ||
                nBaseAddress3 < 0x00300000 ||
                nBaseAddress4 < 0x00300000)
            {
                return false;
            }
            IntPtr hProcess = WinAPI.OpenProcess(
                WinAPI.PROCESS_ALL_ACCESS, false, (uint)process.Id);
            if (hProcess == IntPtr.Zero)
                return false;

            string strBuffer1, strBuffer2;
            if (!WinAPI.ReadProcessMemory(hProcess, nBaseAddress1, out strBuffer1, 32) ||
                !WinAPI.ReadProcessMemory(hProcess, nBaseAddress2 - 0x64, out strBuffer2, 32))
            {
                WinAPI.CloseHandle(hProcess);
                return false;
            }
            if (strBuffer1 != strBuffer2)
            {
                if (hWnd != null)
                {
                    foreach (IntPtr handle in hWnd)//发送消息,使得熊熊档案窗口显示并刷新
                    {
                        WinAPI.SendMessage(handle, 0x0465, 0, 0x00000200);
                    }
                    WinAPI.SetWindowPos(hWnd2, IntPtr.Zero, -130, -162, 0, 0, WinAPI.SWP_NOSIZE | WinAPI.SWP_NOZORDER);
                    System.Threading.Thread.Sleep(100);

                    if (!WinAPI.ReadProcessMemory(hProcess, nBaseAddress2 - 0x64, out strBuffer2, 32))
                    {
                        WinAPI.CloseHandle(hProcess);
                        return false;
                    }
                }
            }
            WinAPI.CloseHandle(hProcess);
            return strBuffer1 == strBuffer2;
        }
        #endregion

        #region ReadBearInfo
        /// <summary>
        /// 读取熊熊的各种状态信息
        /// </summary>
        /// <returns></returns>
        public bool ReadBearInfo()
        {
            IntPtr hProcess = WinAPI.OpenProcess(
                WinAPI.PROCESS_ALL_ACCESS, false, (uint)process.Id);
            if (hProcess == IntPtr.Zero)
                return false;
            if (hWnd != null)
            {
                foreach (IntPtr handle in hWnd)//发送消息,使得熊熊档案窗口显示并刷新
                {
                    WinAPI.SendMessage(handle, 0x0465, 0, 0x00000200);
                }
                WinAPI.SetWindowPos(hWnd2, IntPtr.Zero, -130, -162, 0, 0, WinAPI.SWP_NOSIZE | WinAPI.SWP_NOZORDER);
                System.Threading.Thread.Sleep(100);
            }

            if (!WinAPI.ReadProcessMemory(hProcess, nBaseAddress2 - 0x64, out NiCheng, 32))
            {
                WinAPI.CloseHandle(hProcess);
                return false;
            }

            if (!WinAPI.ReadProcessMemory(hProcess, nBaseAddress3 - 0xE4, out ZaiXian, 32))
            {
                WinAPI.CloseHandle(hProcess);
                return false;
            }

            int index1 = ZaiXian.IndexOf("小时");
            int index2 = ZaiXian.IndexOf("分");
            string strHour = "0";
            string strMinute = "0";
            if (index1 > 0)
            {
                strHour = ZaiXian.Substring(0, index1);
                if (index2 > 0)
                {
                    strMinute = ZaiXian.Substring(index1 + 2, ZaiXian.Length - index1 - 3);
                }
            }
            else
            {
                if (index2 > 0)
                {
                    strMinute = ZaiXian.Substring(0, index2);
                }
            }
            try
            {
                ZaiXianHour = int.Parse(strHour);
                ZaiXianMinute = int.Parse(strMinute);
            }
            catch
            {
            }

            if (!WinAPI.ReadProcessMemory(hProcess, nBaseAddress4 - 0xE4, out ZhuangTai, 32))
            {
                WinAPI.CloseHandle(hProcess);
                return false;
            }
            //string strBuffer = "";
            if (nBaseAddress1 >= 0x01000000)// &&
                //WinAPI.ReadProcessMemory(hProcess, nBaseAddress1, out strBuffer, (uint)flag1.Length) &&
                //strBuffer.IndexOf(flag1) == 0)
            {
                if (!WinAPI.ReadProcessMemory(hProcess, nBaseAddress1, out NiCheng, 32))
                {
                    WinAPI.CloseHandle(hProcess);
                    return false;
                }
                //if (strBuffer == NiCheng)
                //{
                    if (!WinAPI.ReadProcessMemory(hProcess, nBaseAddress1 + 0x21, out ZhuRen, 32))
                    {
                        WinAPI.CloseHandle(hProcess);
                        return false;
                    }

                    int[] nBuffer = new int[1];
                    if (!WinAPI.ReadProcessMemory(hProcess, (IntPtr)(nBaseAddress1 + 0x21 + 0x23),
                        nBuffer, 2, IntPtr.Zero))
                    {
                        WinAPI.CloseHandle(hProcess);
                        return false;
                    }
                    ShengRiDay = nBuffer[0];

                    if (!WinAPI.ReadProcessMemory(hProcess, (IntPtr)(nBaseAddress1 + 0x21 + 0x23 + 0x2),
                        nBuffer, 2, IntPtr.Zero))
                    {
                        WinAPI.CloseHandle(hProcess);
                        return false;
                    }
                    ShengRiMonth = nBuffer[0];
                    ShengRi = ShengRiMonth + "月" + ShengRiDay + "日";

                    if (!WinAPI.ReadProcessMemory(hProcess, (IntPtr)(nBaseAddress1 + 0x21 + 0x23 + 0x2 + 0x2 + 0x4),
                        nBuffer, 4, IntPtr.Zero))
                    {
                        WinAPI.CloseHandle(hProcess);
                        return false;
                    }
                    ZongChengZhang = nBuffer[0];

                    if (!WinAPI.ReadProcessMemory(hProcess, (IntPtr)(nBaseAddress1 + 0x21 + 0x23 + 0x2 + 0x2 + 0x4 + 0x4),
                        nBuffer, 4, IntPtr.Zero))
                    {
                        WinAPI.CloseHandle(hProcess);
                        return false;
                    }
                    DengJi = nBuffer[0];
                    if (DengJi > 0)
                    {
                        YinShiMax = 600 + 50 * (DengJi - 1);
                        if (DengJi <= 16)
                        {
                            QingJieMax = 1200 + 50 * (DengJi - 1);
                        }
                        else
                        {
                            QingJieMax = 1950 + 100 * (DengJi - 16);
                        }
                    }

                    if (!WinAPI.ReadProcessMemory(hProcess, (IntPtr)(nBaseAddress1 + 0x21 + 0x23 + 0x2 + 0x2 + 0x4 + 0x4 + 0x4),
                        nBuffer, 4, IntPtr.Zero))
                    {
                        WinAPI.CloseHandle(hProcess);
                        return false;
                    }
                    YinShi = nBuffer[0];

                    if (!WinAPI.ReadProcessMemory(hProcess, (IntPtr)(nBaseAddress1 + 0x21 + 0x23 + 0x2 + 0x2 + 0x4 + 0x4 + 0x4 + 0x4),
                        nBuffer, 4, IntPtr.Zero))
                    {
                        WinAPI.CloseHandle(hProcess);
                        return false;
                    }
                    QingJie = nBuffer[0];

                    if (!WinAPI.ReadProcessMemory(hProcess, (IntPtr)(nBaseAddress1 + 0x21 + 0x23 + 0x2 + 0x2 + 0x4 + 0x4 + 0x4 + 0x4 + 0x4),
                        nBuffer, 4, IntPtr.Zero))
                    {
                        WinAPI.CloseHandle(hProcess);
                        return false;
                    }
                    TiLi = nBuffer[0];

                    if (!WinAPI.ReadProcessMemory(hProcess, (IntPtr)(nBaseAddress1 + 0x21 + 0x23 + 0x2 + 0x2 + 0x4 + 0x4 + 0x4 + 0x4 + 0x4 + 0x4),
                        nBuffer, 4, IntPtr.Zero))
                    {
                        WinAPI.CloseHandle(hProcess);
                        return false;
                    }
                    XinQing = nBuffer[0];
                //}
            }

            WinAPI.CloseHandle(hProcess);
            return true;
        }

        /// <summary>
        /// 重新获得熊熊的总成长,等级,饮食,清洁,体力,心情这几个属性
        /// </summary>
        /// <returns></returns>
        public bool ReGetBearInfo1()
        {
            //if (qq == "" || hWnd == null || hWnd1 == IntPtr.Zero || hWnd2 == IntPtr.Zero)
            //{
            //    if (!SearchWindow())
            //        return false;
            //}
            if (!VerifyBaseAddress())
            {
                if (!SearchBaseAddress1()||
                    !SearchBaseAddress2())
                {
                    return false;
                }
                return ReadBearInfo();
            }

            IntPtr hProcess = WinAPI.OpenProcess(
                WinAPI.PROCESS_ALL_ACCESS, false, (uint)process.Id);
            if (hProcess == IntPtr.Zero)
                return false;
            if (!WinAPI.ReadProcessMemory(hProcess, nBaseAddress1, out NiCheng, 32))
            {
                WinAPI.CloseHandle(hProcess);
                return false;
            }

            if (!WinAPI.ReadProcessMemory(hProcess, nBaseAddress1 + 0x21, out ZhuRen, 32))
            {
                WinAPI.CloseHandle(hProcess);
                return false;
            }

            int[] nBuffer = new int[1];
            if (!WinAPI.ReadProcessMemory(hProcess, (IntPtr)(nBaseAddress1 + 0x21 + 0x23),
                nBuffer, 2, IntPtr.Zero))
            {
                WinAPI.CloseHandle(hProcess);
                return false;
            }
            ShengRiDay = nBuffer[0];

            if (!WinAPI.ReadProcessMemory(hProcess, (IntPtr)(nBaseAddress1 + 0x21 + 0x23 + 0x2),
                nBuffer, 2, IntPtr.Zero))
            {
                WinAPI.CloseHandle(hProcess);
                return false;
            }
            ShengRiMonth = nBuffer[0];
            ShengRi = ShengRiMonth + "月" + ShengRiDay + "日";

            if (!WinAPI.ReadProcessMemory(hProcess, (IntPtr)(nBaseAddress1 + 0x21 + 0x23 + 0x2 + 0x2 + 0x4),
                nBuffer, 4, IntPtr.Zero))
            {
                WinAPI.CloseHandle(hProcess);
                return false;
            }
            ZongChengZhang = nBuffer[0];

            if (!WinAPI.ReadProcessMemory(hProcess, (IntPtr)(nBaseAddress1 + 0x21 + 0x23 + 0x2 + 0x2 + 0x4 + 0x4),
                nBuffer, 4, IntPtr.Zero))
            {
                WinAPI.CloseHandle(hProcess);
                return false;
            }
            DengJi = nBuffer[0];
            if (DengJi > 0)
            {
                YinShiMax = 600 + 50 * (DengJi - 1);
                if (DengJi <= 16)
                {
                    QingJieMax = 1200 + 50 * (DengJi - 1);
                }
                else
                {
                    QingJieMax = 1950 + 100 * (DengJi - 16);
                }
            }

            if (!WinAPI.ReadProcessMemory(hProcess, (IntPtr)(nBaseAddress1 + 0x21 + 0x23 + 0x2 + 0x2 + 0x4 + 0x4 + 0x4),
                nBuffer, 4, IntPtr.Zero))
            {
                WinAPI.CloseHandle(hProcess);
                return false;
            }
            YinShi = nBuffer[0];

            if (!WinAPI.ReadProcessMemory(hProcess, (IntPtr)(nBaseAddress1 + 0x21 + 0x23 + 0x2 + 0x2 + 0x4 + 0x4 + 0x4 + 0x4),
                nBuffer, 4, IntPtr.Zero))
            {
                WinAPI.CloseHandle(hProcess);
                return false;
            }
            QingJie = nBuffer[0];

            if (!WinAPI.ReadProcessMemory(hProcess, (IntPtr)(nBaseAddress1 + 0x21 + 0x23 + 0x2 + 0x2 + 0x4 + 0x4 + 0x4 + 0x4 + 0x4),
                nBuffer, 4, IntPtr.Zero))
            {
                WinAPI.CloseHandle(hProcess);
                return false;
            }
            TiLi = nBuffer[0];

            if (!WinAPI.ReadProcessMemory(hProcess, (IntPtr)(nBaseAddress1 + 0x21 + 0x23 + 0x2 + 0x2 + 0x4 + 0x4 + 0x4 + 0x4 + 0x4 + 0x4),
                nBuffer, 4, IntPtr.Zero))
            {
                WinAPI.CloseHandle(hProcess);
                return false;
            }
            XinQing = nBuffer[0];

            return true;
        }
        #endregion

        #region 保姆动作
        public bool Work()
        {
            //WinAPI.PostMessageSafe(new HandleRef(null, hWnd1), WinAPI.WM_LBUTTONDOWN, (IntPtr)1, WinAPI.MakeLParam(70, 100));
            //WinAPI.PostMessageSafe(new HandleRef(null, hWnd1), WinAPI.WM_LBUTTONUP, (IntPtr)0, WinAPI.MakeLParam(70, 100));

            ClickWnd(hWnd1, 70, 100);
            ClickWnd(hWnd1, 70, 40);
            ClickWnd(hWnd1, 100, 70);
            ClickWnd(hWnd1, 40, 70);

            ClickWnd(hWnd5, 50, 10);

            //hWnd3 = (IntPtr)0x000306B6;
            //WinAPI.SendMessage(hWnd3, 0x001C, 1, 0);
            //WinAPI.SetWindowPos(hWnd3, IntPtr.Zero, 0, 0, 0, 0, WinAPI.SWP_SHOWWINDOW|
            //    WinAPI.SWP_NOZORDER | WinAPI.SWP_NOSIZE | WinAPI.SWP_NOMOVE|WinAPI.SWP_NOACTIVATE);
            return true;
        }
        #endregion

        void ClickWnd(IntPtr hwnd, int dx, int dy)
        {
            WinAPI.RECT rect = new WinAPI.RECT();
            WinAPI.GetWindowRect(hwnd, ref rect);
            Point p = Cursor.Position;
            Cursor.Hide();
            Cursor.Position = new Point(rect.left + dx, rect.top + dy);
            WinAPI.mouse_event(WinAPI.MouseEventFlag.LeftDown | WinAPI.MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
            Cursor.Position = p;
            Cursor.Show();
        }
    }
}

 

FormMain.cs

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.Diagnostics;

namespace QBear4A
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }
        DataTable dt;// q熊列表数据
        int index = -1;//当前熊索引

        private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new AboutBox().ShowDialog();
        }

        private void 退出EToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        private void FormMain_Load(object sender, EventArgs e)
        {
            toolStripStatusLabel.Text = "正在探测Q熊...";
            QBearHelper.SearchQBear();
            if (QBearHelper.Helper == null ||
                QBearHelper.Helper.Length <= 0)
            {
                toolStripStatusLabel.Text = "没有探测到Q熊...";
            }
            else
            {
                toolStripStatusLabel.Text = "发现" + QBearHelper.Helper.Length + "头Q熊.";
                dt = new DataTable("QBearList");
                dt.Columns.Add("序号", typeof(int));
                //dt.Columns.Add("保姆", typeof(bool));
                dt.Columns.Add("Q熊昵称", typeof(string));
                dt.Columns.Add("所属QQ", typeof(string));
                dt.Columns.Add("状态", typeof(string));
                dt.Columns.Add("今日在线", typeof(string));
                for (int i = 0; i < QBearHelper.Helper.Length; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["序号"] = i+1;
                    //dr["Q熊昵称"] = QBearHelper.Helper[i].NiCheng;
                    dr["所属QQ"] = QBearHelper.Helper[i].qq;
                    //dr["状态"] = QBearHelper.Helper[i].ZhuangTai;
                    //dr["今日在线"] = QBearHelper.Helper[i].ZaiXian;
                    dt.Rows.Add(dr);
                }
                this.dataGridViewQBear.DataSource = dt;
                index = 0;
                this.dataGridViewQBear.Rows[index].Selected = true;
                RefreshShow();
            }
        }

        private void testToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (QBearHelper.Helper[index].ReGetBearInfo1())
                RefreshShow();

            QBearHelper.Helper[index].Work();
            //WinAPI.SetWindowPos(Helper.qbInfos[0].hwnd1, WinAPI.HWND_TOPMOST, 0, 0, 0, 0, WinAPI.SWP_NOSIZE);
//               pictureBoxSnap.Image = Helper.Snapshot(0);
                //Helper.ShowDangAn(0);
                //Helper.ShowCtrlBar(0);
               // Point p = Cursor.Position;
               // Cursor.Hide();
               //// Cursor.Position = new Point(70, 155);
               // Cursor.Position = new Point(60, 120);
               // WinAPI.mouse_event(WinAPI.MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
               // WinAPI.mouse_event(WinAPI.MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
               // Cursor.Position = p;
               // Cursor.Show();

//               WinAPI.PostMessageSafe(new HandleRef(null,Helper.qbInfos[0].hwnd1), WinAPI.WM_LBUTTONDOWN, (IntPtr)1, WinAPI.MakeLParam(70, 100));
//               WinAPI.PostMessageSafe(new HandleRef(null, Helper.qbInfos[0].hwnd1), WinAPI.WM_LBUTTONUP, (IntPtr)0, WinAPI.MakeLParam(70, 100));
        }

        void RefreshShow()
        {
            //dt.Rows[index]["保姆"] = QBearHelper.Helper[index].Running;
            dt.Rows[index]["Q熊昵称"] = QBearHelper.Helper[index].NiCheng;
            dt.Rows[index]["状态"] = QBearHelper.Helper[index].ZhuangTai;
            dt.Rows[index]["今日在线"] = QBearHelper.Helper[index].ZaiXian;
            label昵称.Text = QBearHelper.Helper[index].NiCheng;
            label主人.Text = QBearHelper.Helper[index].ZhuRen;
            label生日.Text = QBearHelper.Helper[index].ShengRi;
            label等级.Text = QBearHelper.Helper[index].DengJi.ToString();
            label总成长.Text = QBearHelper.Helper[index].ZongChengZhang.ToString();
            label状态.Text = QBearHelper.Helper[index].ZhuangTai;
            label今日在线.Text = QBearHelper.Helper[index].ZaiXian;
            progressBar体力.Maximum = QBearHelper.Helper[index].TiLiMax;
            progressBar体力.Value = QBearHelper.Helper[index].TiLi;
            progressBar饮食.Maximum = QBearHelper.Helper[index].YinShiMax;
            progressBar饮食.Value = QBearHelper.Helper[index].YinShi;
            progressBar清洁.Maximum = QBearHelper.Helper[index].QingJieMax;
            progressBar清洁.Value = QBearHelper.Helper[index].QingJie;
            progressBar心情.Maximum = QBearHelper.Helper[index].XinQingMax;
            progressBar心情.Value = QBearHelper.Helper[index].XinQing;
            checkBoxRunning.Checked = QBearHelper.Helper[index].Running;
            checkBoxRunning.Text = "Q熊 " + QBearHelper.Helper[index].NiCheng + "\n的";
            checkBoxRunning.Text += checkBoxRunning.Checked ? "保姆运行中..." : "保姆尚未运行";
        }

        private void dataGridViewQBear_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridViewQBear.SelectedRows.Count < 1)
            {
                return;
            }
            index = (int)dataGridViewQBear.SelectedRows[0].Cells[0].Value - 1;
            QBearHelper.Helper[index].ReadBearInfo();
            RefreshShow();
        }

        private void toolStripButtonStrat_Click(object sender, EventArgs e)
        {
            if (!QBearHelper.Helper[index].Running)
            {
                QBearHelper.Helper[index].Start();
                RefreshShow();
            }
        }

        private void toolStripButtonStop_Click(object sender, EventArgs e)
        {
            if (QBearHelper.Helper[index].Running)
            {
                QBearHelper.Helper[index].Stop();
                RefreshShow();
            }
        }

        private void toolStripButtonStartAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < QBearHelper.Helper.Length; i++)
            {
                if (!QBearHelper.Helper[i].Running)
                {
                    QBearHelper.Helper[i].Start();
                }
            }
            RefreshShow();
       }

        private void toolStripButtonStopAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < QBearHelper.Helper.Length; i++)
            {
                if (QBearHelper.Helper[i].Running)
                {
                    QBearHelper.Helper[i].Stop();
                }
            }
            RefreshShow();
        }

        private void toolStripButtonCfg_Click(object sender, EventArgs e)
        {

        }
    }
}

posted @ 2010-09-11 16:17  huxi  阅读(521)  评论(0编辑  收藏  举报