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

前端时间做了Messenger助手,后来发现只支持Windows Messenger,不支持Windows Live Messenger,最近改了一下,用到了Active Accessibility编程,代码如下:
None.gif using  System;
None.gif
using  Accessibility;
None.gif
None.gif
namespace  MessengerHelper
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 对Messenger窗口进行操作
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class MessengerWindowHelper
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        IntPtr _messengerWindowHandle ;
InBlock.gif        IntPtr _inputBoxHandle ;
InBlock.gif        IntPtr _submitButtonHandle ;
InBlock.gif        IAccessible _inputBox ;
InBlock.gif        IAccessible _submitButton ;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
private MessengerWindowHelper()dot.gif{}
InBlock.gif        
public MessengerWindowHelper(IntPtr windowHandle)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _messengerWindowHandle 
= windowHandle ;
InBlock.gif            _inputBoxHandle 
= GetInputBoxHandle() ;
InBlock.gif            _submitButtonHandle 
= GetSubmitButton() ;
InBlock.gif
InBlock.gif            
if (_inputBoxHandle == IntPtr.Zero && _submitButtonHandle == IntPtr.Zero)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                GetAccessibleObjects(_messengerWindowHandle, 
out _inputBox, out _submitButton);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 输入消息
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="message"></param>

InBlock.gif        public void InputMessage(string message)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (_inputBox == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Win32.SendMessageString(_inputBoxHandle, Win32.WM_SETTEXT, IntPtr.Zero, message);            
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _inputBox.set_accValue(Win32.CHILDID_SELF, message) ;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 发送消息
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public void SendMessage()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (_submitButton == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Win32.SendMessageInt(_submitButtonHandle, Win32.WM_CLICK, IntPtr.Zero, 
0);             
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _submitButton.accDoDefaultAction(Win32.CHILDID_SELF) ;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private IntPtr GetInputBoxHandle()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IntPtr topInputHandle 
= Win32.FindWindowEx(_messengerWindowHandle, System.IntPtr.Zero, "RichEdit20W"null) ;
InBlock.gif            
return Win32.FindWindowEx(_messengerWindowHandle, topInputHandle, "RichEdit20W"null) ;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private IntPtr GetSubmitButton()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return Win32.FindWindowEx(_messengerWindowHandle, System.IntPtr.Zero, "Button""发送(&S)") ;
ExpandedSubBlockEnd.gif        }

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

InBlock.gif
InBlock.gif        
private void GetAccessibleObjects(System.IntPtr imWindowHwnd, out IAccessible inputBox, out IAccessible submitButtion)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.IntPtr ptrUIHWND 
= Win32.FindWindowEx(imWindowHwnd, System.IntPtr.Zero, "DirectUIHWND"0);
InBlock.gif            Guid guidCOM 
= new Guid(0x618736E0,0x3C3D,0x11CF,0x81,0xC,0x0,0xAA,0x0,0x38,0x9B,0x71);
InBlock.gif            Accessibility.IAccessible IACurrent 
= null;
InBlock.gif            
InBlock.gif            Win32.AccessibleObjectFromWindow(ptrUIHWND,(
int)Win32.OBJID_CLIENT,ref guidCOM,ref IACurrent); 
InBlock.gif            IACurrent 
= (IAccessible)IACurrent.accParent;
InBlock.gif            
int childCount = IACurrent.accChildCount;
InBlock.gif            
object[] windowChildren = new object[childCount];
InBlock.gif            
int pcObtained;
InBlock.gif            Win32.AccessibleChildren(IACurrent, 
0, childCount, windowChildren, out pcObtained);
InBlock.gif
InBlock.gif            inputBox 
= null ;
InBlock.gif            submitButtion 
= null ;
InBlock.gif
InBlock.gif            
string accName ;
InBlock.gif            
int accRole ;
InBlock.gif            
foreach(IAccessible child in windowChildren)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                accRole 
= (int)child.get_accRole(Win32.CHILDID_SELF) ;
InBlock.gif                accName 
= child.get_accName(Win32.CHILDID_SELF) ;
InBlock.gif                
if (accRole == 10)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
object[] clientChilren = GetAccessibleChildren(child) ;                            
InBlock.gif                    IAccessible client 
= (IAccessible)clientChilren[0] ;
InBlock.gif                    clientChilren 
= GetAccessibleChildren(client) ;
InBlock.gif                    
InBlock.gif                    
foreach (IAccessible childChild in clientChilren)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        accRole 
= (int)childChild.get_accRole(Win32.CHILDID_SELF) ;
InBlock.gif                        accName 
= childChild.get_accName(Win32.CHILDID_SELF) ;
InBlock.gif                        
InBlock.gif                        
if (accRole == 42 && accName == "输入")
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            inputBox 
= childChild ;
ExpandedSubBlockEnd.gif                        }

InBlock.gif
InBlock.gif                        
if (accRole == 43 && accName == "发送按钮")
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            submitButtion 
= childChild ;
ExpandedSubBlockEnd.gif                        }

InBlock.gif
InBlock.gif                        
if (inputBox != null && submitButtion != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
break ;
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }
                    
InBlock.gif                    
break ;
ExpandedSubBlockEnd.gif                }
                        
ExpandedSubBlockEnd.gif            }
            
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

None.gif using  System;
None.gif
using  System.Runtime.InteropServices;
None.gif
None.gif
using  Accessibility;
None.gif
None.gif
namespace  MessengerHelper
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 调用Window API
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Win32
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public const int WM_SETTEXT = 0x000C
InBlock.gif        
public const int WM_CLICK = 0x00F5;
InBlock.gif 
InBlock.gif        
public const int CHILDID_SELF  = 0;
InBlock.gif        
public const int CHILDID_1   = 1;
InBlock.gif        
public const int OBJID_CLIENT  = -4;
InBlock.gif
InBlock.gif        [DllImport(
"User32.dll")]
InBlock.gif        
public static extern Int32 FindWindow(String lpClassName,String lpWindowName);
InBlock.gif
InBlock.gif        [DllImport(
"user32.dll", CharSet=CharSet.Auto)]
InBlock.gif        
public static extern IntPtr FindWindowEx(
InBlock.gif            IntPtr parentHandle,
InBlock.gif            IntPtr childAfter, 
InBlock.gif            
string lpszClass,
ExpandedSubBlockStart.gifContractedSubBlock.gif            
int sWindowTitle  /**//*HWND*/);
InBlock.gif
InBlock.gif        [DllImport(
"user32.dll", SetLastError = true)]
InBlock.gif        
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className,  string  windowTitle);        
InBlock.gif
InBlock.gif        [DllImport(
"user32.dll", EntryPoint="SendMessage")] 
InBlock.gif        
public static extern int SendMessageString (IntPtr hwnd, int wMsg, IntPtr wParam, string lParam); 
InBlock.gif
InBlock.gif        [DllImport(
"user32.dll", EntryPoint="SendMessage")] 
InBlock.gif        
public static extern int SendMessageInt (IntPtr hwnd, int wMsg, IntPtr wParam, int lParam); 
InBlock.gif
InBlock.gif        [DllImport(
"Oleacc.dll")]
InBlock.gif      
public static extern int AccessibleObjectFromWindow(
InBlock.gif      IntPtr hwnd,
InBlock.gif      
int dwObjectID,
InBlock.gif      
ref Guid refID,
InBlock.gif      
ref IAccessible ppvObject);
InBlock.gif
InBlock.gif        [DllImport(
"Oleacc.dll")]
InBlock.gif        
public static extern int WindowFromAccessibleObject(
InBlock.gif            IAccessible pacc,
InBlock.gif            
out IntPtr phwnd);
InBlock.gif      
InBlock.gif      [DllImport(
"Oleacc.dll")]
InBlock.gif      
public static extern int AccessibleChildren(
InBlock.gif      Accessibility.IAccessible paccContainer,
InBlock.gif      
int iChildStart,
InBlock.gif      
int cChildren,
InBlock.gif      [Out] 
object[] rgvarChildren,
InBlock.gif      
out int pcObtained);    
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

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

转载于:https://www.cnblogs.com/karoc/archive/2006/11/29/576253.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值