【C#】QQ消息自动发送代码

1、准备Windows API,是用C#开发的,所以要准备C#封装的Windows API。可以到以下地址下载:

C#版封装的Windows API,简体版+增加版,源码
http://bmpj.net/forum-viewthread-tid-461-fromuid-13.html

2、定义保存QQ聊天窗体的对象类

   

internal class QQChatWindows
    {
        private IntPtr _WindowHwnd = IntPtr.Zero;

        public IntPtr WindowHwnd
        {
            get { return _WindowHwnd; }
            set { _WindowHwnd = value; }
        }
        private string _Caption = String.Empty;

        public string Caption
        {
            get { return _Caption; }
            set { _Caption = value; }
        }
        public QQChatWindows(IntPtr windowhwnd, string caption)
        {
            _WindowHwnd = windowhwnd;
            _Caption = caption;
        }
    }

3、遍历QQ聊天窗体

private void EnumQQChatWindows()
        {
            this.listQQWindows.Items.Clear();
            this._QQListWindows.Clear();
            NativeMethods.EnumDesktopWindows(IntPtr.Zero, new NativeMethods.EnumDesktopWindowsDelegate(EnumWindowsProc), IntPtr.Zero);
        }

        private bool EnumWindowsProc(IntPtr hWnd, uint lParam)
        {
            string qqproname = this.GetProcessName(hWnd);
            StringBuilder className = new StringBuilder(255 + 1); //ClassName 最长
            NativeMethods.GetClassName(hWnd, className, className.Capacity);

            if (!qqproname.Equals(String.Empty) && qqproname.Equals("QQ") && className.ToString().Equals("TXGuiFoundation"))
            {
                StringBuilder caption = new StringBuilder(NativeMethods.GetWindowTextLength(hWnd) + 1);
                NativeMethods.GetWindowText(hWnd, caption, caption.Capacity);
                if (!caption.ToString().Equals(String.Empty) && !caption.ToString().Equals("TXFloatingWnd") && !caption.ToString().Equals("TXMenuWindow") && !caption.ToString().Equals("QQ2011"))
                {
                   
                    QQChatWindows qqchat = new QQChatWindows(hWnd, caption.ToString());
                    this._QQListWindows.Add(qqchat);

                    this.listQQWindows.Items.Add(caption);
                }

            }
            return true;

        }

        public string GetProcessName(IntPtr hWnd)
        {
            try
            {
                string processname = String.Empty;
                int proid = 0;
                uint threadid = NativeMethods.GetWindowThreadProcessId(hWnd, out proid);
                if (threadid > 0 && proid > 0)
                {
                    Process pro = Process.GetProcessById(proid);
                    processname = pro.ProcessName;

                }

                return processname;
            }
            catch
            {
                return String.Empty;
            }
        }

4、循环自动发送QQ消息

    

private bool SendQQMsg(IntPtr hWnd, string qqcaption, string sendtext)
        {
            try
            {
                NativeMethods.ShowWindow(hWnd, NativeMethods.ShowWindowCommands.Normal);

                NativeMethods.BringWindowToTop(hWnd);

                SendKeys.SendWait(sendtext);

                SendKeys.SendWait("^{ENTER}");   //CTRL+ENTER
                return true;
            }
            catch 
            {
                return false;
            }

        }

源码下载:

百木QQ信息自动发送器 源代码,大家可以完善!!
http://bmpj.net/forum-viewthread-tid-498-fromuid-13.html

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
作者: 三角猫 出处: http://www.zu14.cn/ 版权归 三角猫 和 真有意思网 所有,转载请注明出处 using System; using System.Collections.Generic; using System.Text; namespace QQAutoMsg { /// /// 消息发送 /// internal static class QQMsgSender { /// /// 发送消息 /// /// 所以已打开的QQ窗体的列表 /// 消息内容 internal static void Go(List qqChatWindows, string msg) { foreach (EnumQQChatWindows.QQChatWindow win in qqChatWindows) { SendMsg(win.WindowHwnd, msg); } } /// /// 根据窗体句柄,找到输入框和发送按钮,发送消息出去 /// /// 聊天窗口句柄 /// 消息内容 private static void SendMsg(IntPtr hWnd, string msg) { if (NativeMethods.IsWindow(hWnd)) //确认该聊天窗口仍然有效 { ////找到 发送 按钮 IntPtr hwndButton = NativeMethods.FindWindowEx(hWnd, IntPtr.Zero, "Button", "发送(S)"); if (IntPtr.Zero != hwndButton) { ////找到窗体顺序上的第一个RichEdit20A控件,其实就是消息显示框 IntPtr hwndRichEdit = NativeMethods.FindWindowEx(hWnd, IntPtr.Zero, "RichEdit20A", null); ////利用spy++,可以看到消息输入框的父窗体是类名为 AfxWnd42 的控件 ////在顺序上是显示框的下一个窗体 if (IntPtr.Zero != hwndRichEdit) { ////找到 AfxWnd42 这个窗体 hwndRichEdit = NativeMethods.GetWindow(hwndRichEdit, NativeMethods.GW_HWNDNEXT); if (IntPtr.Zero != hwndRichEdit) { ////这才是真正的消息输入框 hwndRichEdit = NativeMethods.FindWindowEx(hwndRichEdit, IntPtr.Zero, "RichEdit20A", null); if (hwndRichEdit != IntPtr.Zero) { ////发送消息,因为QQ屏蔽了 WM_SETTEXT, WM_PASTE 命令,所有采用 EM_REPLACESEL 来实现 NativeMethods.SendMessage(hwndRichEdit, NativeMethods.EM_REPLACESEL, IntPtr.Zero, msg); ////给发送按钮发 鼠标单击消息 NativeMethods.SendMessage(hwndButton, NativeMethods.BM_CLICK, IntPtr.Zero, IntPtr.Zero); } } } } } } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值