惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - karoc

《重构-改善既有代码的设计》读书笔记 扩展GridView,增加单选按钮列 任务开始时间和完成时间 最大长度验证控件:MaxLengthValidator 又是关于AjaxControlToolkit的ModalPopup的问题 有个总结 使用svn的小插曲 开发小经验总结(不断更新) 今天发现一个VS2008中文版和英文版的差别 解决DocType引起AjaxTookit的ModulPopup显示异常问题的方法 Reporting Services 2005 常见问题解决方法(不断更新) - karoc 用VSTO开发Project插件心得 自定义MemberShipProvider和PersonalizationProvider使用WebParts实现个性化页面 - karoc 在线修改KeyValue配置节 瞎搞八搞,另类PageBase 改造Duwamish中的Configuration 直接用Response输出可以加批注的Excel 一个读取扩展名为xml的资源文件的方法 用C#+WMI实现获取w3wp进程对应的应用程序池
C#控制Windows Messenger和Windows Live Messenger窗口发送消息
karoc · 2006-11-29 · via 博客园 - karoc

前端时间做了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产品及版本繁多,而且插件也不少,所以兼容性不是很强,实用性不大,但是,个人觉得代码还是有一些借鉴作用。