QQ自动登录工具

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
namespace qq
{
    public partial class Form1 : Form
    {
        ///
        /// 查找窗口句柄
        ///
        ///窗口类名
        ///窗口标题
        ///
        [DllImport("user32.dll")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        ///
        /// 查找子窗口句柄
        ///
        ///要查找子窗口的父窗口句柄
        ///上一个子窗口句柄
        ///子窗口类名
        ///窗口标题
        ///
        [DllImport("User32.DLL")]
        private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        ///
        /// 设置指定窗口为当前活动窗口
        ///
        ///窗口句柄
        [DllImport("User32.DLL")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        ///
        /// 可将最小化窗口还原
        ///
        ///
        ///
        ///
        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        ///
        /// 向指定窗口发送字符串
        ///
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);

        ///
        /// 可向指定窗口发送按键
        ///
        [DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern int SendMessage(IntPtr hwnd, uint wMsg, uint wParam, uint lParam);

        ///
        /// 发送按键消息用PostMessage比较好,SendMessage有时会不起作用
        ///
        [DllImport("user32.dll", EntryPoint = "PostMessage", SetLastError = true)]
        private static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

        // 注意,运行时知道如何列集一个矩形
        [DllImport("user32.dll")]
        private static extern int GetWindowRect(IntPtr hwnd, out NativeRECT lpRect);

        [DllImport("user32.dll")]
        private static extern UInt32 SendInput(UInt32 nInputs, ref INPUT pInputs, int cbSize);

        //SendMessage参数
        const int WM_KEYDOWN = 0x0100;//普通按键按下
        const int WM_KEYUP = 0x0101;//普通按键放开
        const int WM_SYSKEYDOWN = 0x104;//系统按键按下
        const int WM_SYSKEYUP = 0x105;//系统按键按下放开
        const int WM_SYSCHAR = 0x0106;//发送单个字符
        const int WM_SETTEXT = 0x000C;//发送文本
        const int WM_CLICK = 0x00F5;//模拟鼠标左键点击
        const int WM_SETFOCUS = 0x0007;//设置焦点
        const int WM_COPY = 0x301; // 复制
        const int WM_CUT = 0x300; // 剪切
        const int WM_PASTE = 0x302; //粘帖
        const int SW_RESTORE = 9;//恢复最小化的窗口
        public const int VK_TAB = 0x9;
        public const int VK_RETURN = 0xD;



        public enum MOUSEEVENTF
        {
            MOVE = 0x0001, //mouse move
            LEFTDOWN = 0x0002, //left button down
            LEFTUP = 0x0004, //left button up
            RIGHTDOWN = 0x0008, //right button down
            RIGHTUP = 0x0010, //right button up
            MIDDLEDOWN = 0x0020, //middle button down
            MIDDLEUP = 0x0040, //middle button up
            XDOWN = 0x0080, //x button down
            XUP = 0x0100, //x button down
            WHEEL = 0x0800, //wheel button rolled
            VIRTUALDESK = 0x4000, //map to entire virtual desktop
            ABSOLUTE = 0x8000, //absolute move
        }

        struct NativeRECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }


        public enum InputType
        {
            INPUT_MOUSE = 0,
            INPUT_KEYBOARD = 1,
            INPUT_HARDWARE = 2,
        }


        [Flags()]
        public enum KEYEVENTF
        {
            EXTENDEDKEY = 0x0001,
            KEYUP = 0x0002,
            UNICODE = 0x0004,
            SCANCODE = 0x0008,
        }

        [StructLayout(LayoutKind.Explicit)]
        private struct INPUT
        {
            [FieldOffset(0)]
            public Int32 type;//0-MOUSEINPUT;1-KEYBDINPUT;2-HARDWAREINPUT
            [FieldOffset(4)]
            public KEYBDINPUT ki;
            [FieldOffset(4)]
            public MOUSEINPUT mi;
            [FieldOffset(4)]
            public HARDWAREINPUT hi;
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct MOUSEINPUT
        {
            public Int32 dx;
            public Int32 dy;
            public Int32 mouseData;
            public Int32 dwFlags;
            public Int32 time;
            public IntPtr dwExtraInfo;
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct KEYBDINPUT
        {
            public Int16 wVk;
            public Int16 wScan;
            public Int32 dwFlags;
            public Int32 time;
            public IntPtr dwExtraInfo;
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct HARDWAREINPUT
        {
            public Int32 uMsg;
            public Int16 wParamL;
            public Int16 wParamH;
        }

        public Form1()
        {
            InitializeComponent();
        }
        /// 
        /// 引用user32.dll动态链接库(windows api),
        /// 使用库中定义 API:SetCursorPos 
        /// 
        [DllImport("user32.dll")]
        private static extern int SetCursorPos(int x, int y);
        /// 
        /// 移动鼠标到指定的坐标点
        /// 
        public void MoveMouseToPoint(Point p)
        {
            SetCursorPos(p.X, p.Y);
        }

        // WindowsAPI.SendMouseLeftClick((int) WindowsAPI.MOUSEEVENTF.LEFTDOWN, (int) WindowsAPI.MOUSEEVENTF.LEFTUP, rect.X + 200, rect.Y + 275);
        [DllImport("user32.dll")]
        static extern void mouse_event(MOUSEEVENTF flags, int dx, int dy, uint data, UIntPtr extraInfo);
        public static void MouseLeftClickEvent(int dx, int dy, uint data)
        {
            SetCursorPos(dx, dy);
            //System.Threading.Thread.Sleep(2 * 1000);
            mouse_event(MOUSEEVENTF.LEFTDOWN, dx, dy, data, UIntPtr.Zero);
            mouse_event(MOUSEEVENTF.LEFTUP, dx, dy, data, UIntPtr.Zero);
        }

        ///
        /// 发送单一按键,如Tab,Shift,Home,End
        ///
        ///VK_TAB,VK_SHIFT
        public static void SendSingleKey(int wVk)
        {
            INPUT input_down = new INPUT();
            input_down.type = (int)InputType.INPUT_KEYBOARD;
            input_down.ki.dwFlags = 0;
            input_down.ki.wVk = (short)wVk;
            SendInput(1, ref input_down, Marshal.SizeOf(input_down));//keydown

            INPUT input_up = new INPUT();
            input_up.type = (int)InputType.INPUT_KEYBOARD;
            input_up.ki.wVk = (short)wVk;
            input_up.ki.dwFlags = (int)KEYEVENTF.KEYUP;
            SendInput(1, ref input_up, Marshal.SizeOf(input_up));//keyup 
        }

        //登陆
        private void loginqq_Click(object sender, EventArgs e)
        {
         System.Diagnostics.Process.Start(@"C:\Program Files (x86)\Tencent\TIM\Bin\QQScLauncher.exe");
            NativeRECT rect = new NativeRECT();
          IntPtr ParenthWnd = new IntPtr(0);
          Thread.Sleep(2000);
           ParenthWnd = FindWindow(null, "TIM");
            //判断这个窗体是否有效 
            if (ParenthWnd != IntPtr.Zero)
            {

                //MessageBox.Show("找到窗口");
                //MessageBox.Show(ParenthWnd.ToString());

                GetWindowRect(ParenthWnd, out rect);
                int x = rect.left;//X
                int y = rect.top;//Y
                int idX = x + 180;
                int idy = y + 275;
                //846848165               
              
                MouseLeftClickEvent(idX, idy, 0);
                //Clipboard.SetDataObject(123);
                //SendMessage(ParenthWnd, WM_PASTE, 0, 0);
                Thread.Sleep(500);
                //发送账号
                System.Windows.Forms.SendKeys.Send("12345678");
                
                SendSingleKey(VK_TAB);
                //发送密码
                System.Windows.Forms.SendKeys.Send("88888");

                SendSingleKey(VK_RETURN);
            }

            else
            {
                MessageBox.Show("没有找到窗口");
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }



}

 

你可能感兴趣的:(QQ自动登录工具)