日期:2014-05-17  浏览次数:20950 次

【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