网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > C#应用
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.用C#实现FTP搜索引擎.
.用Visual C#动态生成组件.
.使用C#创建SQL Server的存储过程.
.读取指定盘符的硬盘序列号.
.C# 中返回上一页问题代码.
.C#引用Excel找不到类型或命名空间.
.C#中实现DataGrid双向排序.
.对C# 2.0中匿名方法的怀疑分析.
.VS.Net C# 调用 Active 组件.
.RadioBox、CheckBox和Validating.
.c#关闭系统进程以及如何调用cmd并.
.列出C#进程以及详细信息.
.C#判断一个string是否可以为数字.
.实用技巧:.Net框架类库中定时器.
.用C#实现木马程序.
.C#中的委托与事件[翻译] .
.c# MD5加密算法的一个实例.
.如何利用C#创建和调用DLL .
.C#中实现随机时间的获取.
.C#反射实例讲解.

C#控制Windows Messenger和Windows Live Messenger窗口发送消息

发表日期:2006-11-30


前端时间做了Messenger助手,后来发现只支持Windows Messenger,不支持Windows Live Messenger,最近改了一下,用到了Active Accessibility编程,代码如下:
using System;
using Accessibility;

namespace MessengerHelper
{
    /**//// <summary>
    /// 对Messenger窗口进行操作
    /// </summary>
    public class MessengerWindowHelper
    {
        IntPtr _messengerWindowHandle ;
        IntPtr _inputBoxHandle ;
        IntPtr _submitButtonHandle ;
        IAccessible _inputBox ;
        IAccessible _submitButton ;

        private MessengerWindowHelper(){}
        public MessengerWindowHelper(IntPtr windowHandle)
        {
            _messengerWindowHandle = windowHandle ;
            _inputBoxHandle = GetInputBoxHandle() ;
            _submitButtonHandle = GetSubmitButton() ;

            if (_inputBoxHandle == IntPtr.Zero && _submitButtonHandle == IntPtr.Zero)
            {
                GetAccessibleObjects(_messengerWindowHandle, out _inputBox, out _submitButton);
            }
        }

        /**//// <summary>
        /// 输入消息
        /// </summary>
        /// <param name="message"></param>
        public void InputMessage(string message)
        {
            if (_inputBox == null)
            {
                Win32.SendMessageString(_inputBoxHandle, Win32.WM_SETTEXT, IntPtr.Zero, message);           
            }
            else
            {
                _inputBox.set_accValue(Win32.CHILDID_SELF, message) ;
            }
        }

        /**//// <summary>
        /// 发送消息
        /// </summary>
        public void SendMessage()
        {
            if (_submitButton == null)
            {
                Win32.SendMessageInt(_submitButtonHandle, Win32.WM_CLICK, IntPtr.Zero, 0);            
            }
            else
            {
                _submitButton.accDoDefaultAction(Win32.CHILDID_SELF) ;
            }
        }

        private IntPtr GetInputBoxHandle()
        {
            IntPtr topInputHandle = Win32.FindWindowEx(_messengerWindowHandle, System.IntPtr.Zero, "RichEdit20W", null) ;
            return Win32.FindWindowEx(_messengerWindowHandle, topInputHandle, "RichEdit20W", null) ;
        }

        private IntPtr GetSubmitButton()
        {
            return Win32.FindWindowEx(_messengerWindowHandle, System.IntPtr.Zero, "Button", "发送(&S)") ;
        }

        private object[] GetAccessibleChildren(IAccessible paccContainer)
        {
            object[] rgvarChildren = new object[paccContainer.accChildCount] ;
            int pcObtained ;
          Win32.AccessibleChildren(paccContainer,0,paccContainer.accChildCount, rgvarChildren, out pcObtained);
            return rgvarChildren ;
        }

        private void GetAccessibleObjects(System.IntPtr imWindowHwnd, out IAccessible inputBox, out IAccessible submitButtion)
        {
            System.IntPtr ptrUIHWND = Win32.FindWindowEx(imWindowHwnd, System.IntPtr.Zero, "DirectUIHWND", 0);
            Guid guidCOM = new Guid(0x618736E0,0x3C3D,0x11CF,0x81,0xC,0x0,0xAA,0x0,0x38,0x9B,0x71);
            Accessibility.IAccessible IACurrent = null;
           
            Win32.AccessibleObjectFromWindow(ptrUIHWND,(int)Win32.OBJID_CLIENT,ref guidCOM,ref IACurrent);
            IACurrent = (IAccessible)IACurrent.accParent;
            int childCount = IACurrent.accChildCount;
            object[] windowChildren = new object[childCount];
            int pcObtained;
            Win32.AccessibleChildren(IACurrent, 0, childCount, windowChildren, out pcObtained);

            inputBox = null ;
            submitButtion = null ;

            string accName ;
            int accRole ;
            foreach(IAccessible child in windowChildren)
            {
                accRole = (int)child.get_accRole(Win32.CHILDID_SELF) ;
                accName = child.get_accName(Win32.CHILDID_SELF) ;
                if (accRole == 10)
                {
                    object[] clientChilren = GetAccessibleChildren(child) ;                           
                    IAccessible client = (IAccessible)clientChilren[0] ;
                    clientChilren = GetAccessibleChildren(client) ;
                   
                    foreach (IAccessible childChild in clientChilren)
                    {
                        accRole = (int)childChild.get_accRole(Win32.CHILDID_SELF) ;
                        accName = childChild.get_accName(Win32.CHILDID_SELF) ;
                       
                        if (accRole == 42 && accName == "输入")
                        {
                            inputBox = childChild ;
                        }

                        if (accRole == 43 && accName == "发送按钮")
                        {
                            submitButtion = childChild ;
                        }

                        if (inputBox != null && submitButtion != null)
                        {
                            break ;
                        }
                    }                   
                    break ;
                }                       
            }           
        }
    }
}

using System;
using System.Runtime.InteropServices;

using Accessibility;

namespace MessengerHelper
{
    /**//// <summary>
    /// 调用Window API
    /// </summary>
    public class Win32
    {
        public const int WM_SETTEXT = 0x000C;
        public const int WM_CLICK = 0x00F5;
 
        public const int CHILDID_SELF  = 0;
        public const int CHILDID_1   = 1;
        public const int OBJID_CLIENT  = -4;

        [DllImport("User32.dll")]
        public static extern Int32 FindWindow(String lpClassName,String lpWindowName);

        [DllImport("user32.dll", CharSet=CharSet.Auto)]
        public static extern IntPtr FindWindowEx(
            IntPtr parentHandle,
            IntPtr childAfter,
            string lpszClass,
            int sWindowTitle  /**//*HWND*/);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className,  string  windowTitle);       

        [DllImport("user32.dll", EntryPoint="SendMessage")]
        public static extern int SendMessageString (IntPtr hwnd, int wMsg, IntPtr wParam, string lParam);

        [DllImport("user32.dll", EntryPoint="SendMessage")]
        public static extern int SendMessageInt (IntPtr hwnd, int wMsg, IntPtr wParam, int lParam);

        [DllImport("Oleacc.dll")]
      public static extern int AccessibleObjectFromWindow(
      IntPtr hwnd,
      int dwObjectID,
      ref Guid refID,
      ref IAccessible ppvObject);

        [DllImport("Oleacc.dll")]
        public static extern int WindowFromAccessibleObject(
            IAccessible pacc,
            out IntPtr phwnd);
      
      [DllImport("Oleacc.dll")]
      public static extern int AccessibleChildren(
      Accessibility.IAccessible paccContainer,
      int iChildStart,
      int cChildren,
      [Out] object[] rgvarChildren,
      out int pcObtained);   
    }
}

由于微软出的Messenger产品及版本繁多,而且插件也不少,所以兼容性不是很强,实用性不大,但是,个人觉得代码还是有一些借鉴作用。

上一篇:Visual c#的Excle编程 人气:5787
下一篇:对C#下函数,委托,事件的一点理解! 人气:5710
浏览全部C#的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐