C# 使用 Windows API 实例(QQ自动登陆的部分代码)

作者:易庆华先生

*地址:湖南省岳阳县荣家湾镇城南村

*E-Mail:  yqh2648@163.com            jiayistudio@163.com

*/

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.Reflection;
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        string lpClassName, lpWindowName;
        StringBuilder lpString;
        #region Import Dll
        //窗体闪烁
        [DllImport("user32.dll")]
        static extern int FlashWindow(IntPtr hwnd,int bInvert);
        //查找指定窗体
        [DllImport("user32.dll", SetLastError = true,EntryPoint="FindWindowA")]
        static extern int FindWindow(String lpClassName,String lpWindowName);
        //查找指定窗体标题
        [DllImport("user32.dll", SetLastError = true, EntryPoint = "GetWindowTextA")]
        static extern int GetWindowText(int hwnd,StringBuilder lpString,int cch);
        [DllImport("user32.dll")]
        private static extern int GetWindowThreadProcessId(
             int hwnd,
             IntPtr lpdwProcessId);
        //获取鼠标坐标
        [DllImport("user32.dll")]
        private static extern int GetCursorPos(
             ref POINTAPI lpPoint);
        [StructLayout(LayoutKind.Sequential)]
        private struct POINTAPI
        {
            internal int x;
            internal int y;
        }
//设置鼠标坐标
        [DllImport("user32.dll")]
        private static extern int SetCursorPos(
             int x,
             int y);
        [DllImport("User32.dll")]
        public static extern Boolean EnumChildWindows(int hWndParent, Delegate lpEnumFunc, int lParam);
        public delegate int Callback(int hWnd, int lParam);
      
#endregion
        public int EnumChildGetValue(int hWnd, int lParam)
        {
            StringBuilder formDetails = new StringBuilder(256);
            int txtValue;
            string editText = "";
            txtValue = GetWindowText(hWnd, formDetails, 256);
            editText = formDetails.ToString().Trim();
            listBox1.Items.Add("子窗体:"+editText);

            return 1;
        }

        public Form1()
        {
            InitializeComponent();
            listBox1.Items.Clear();
            int sRc = FlashWindow(this.Handle, 5);
        }
        public void Insert_listbox()
        {
            StringBuilder lpString = new StringBuilder(50);
            lpClassName = "#32770";
            lpWindowName = "QQ用户登录";
            int hwnd=FindWindow(lpClassName,lpWindowName);
           
            listBox1.Items.Add("QQ登录窗体句柄:0x" +Convert.ToString(hwnd,16));
            int lp=GetWindowText(hwnd, lpString, 40);
            listBox1.Items.Add("窗体标题是:"+lpString.ToString());
          int GetWindowThreadProcessId(hwnd,IntPtr.Zero);
        
          listBox1.Items.Add("进程ID:0x"+Convert.ToString(id,16));
          Callback myCallBack = new Callback(EnumChildGetValue);
          EnumChildWindows(hwnd, myCallBack, 0);
          
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                listBox1.Items.Clear();
                Insert_listbox();
                int sRc = FlashWindow(this.Handle, 5);
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try {
                //显示鼠标坐标
                POINTAPI pt = new POINTAPI();
                GetCursorPos(ref pt);
                this.Mouselabel.Text = "X:" + pt.x + "Y:" + pt.y;
            }catch(Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

        private void Auto_Mouse_Btn_Click(object sender, EventArgs e)
        {
            this.timer2.Enabled = true;
            listBox1.Items.Add("鼠标自动移到中......");
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            Random random = new Random();
            int x = random.Next(500);
            int y = random.Next(458);
            int sRC=SetCursorPos(x, y);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值