前端时间做了Messenger助手,后来发现只支持Windows Messenger,不支持Windows Live Messenger,最近改了一下,用到了Active Accessibility编程,代码如下: namespace MessengerHelper private MessengerWindowHelper(){} if (_inputBoxHandle == IntPtr.Zero && _submitButtonHandle == IntPtr.Zero) /**//// <summary> /**//// <summary> private IntPtr GetInputBoxHandle() private IntPtr GetSubmitButton() private object[] GetAccessibleChildren(IAccessible paccContainer) private void GetAccessibleObjects(System.IntPtr imWindowHwnd, out IAccessible inputBox, out IAccessible submitButtion) inputBox = null ; string accName ; if (accRole == 43 && accName == “发送按钮”) if (inputBox != null && submitButtion != null) using System; using Accessibility; namespace MessengerHelper [DllImport(“User32.dll”)] [DllImport(“user32.dll”, CharSet=CharSet.Auto)] [DllImport(“user32.dll”, SetLastError = true)] [DllImport(“user32.dll”, EntryPoint=”SendMessage”)] [DllImport(“user32.dll”, EntryPoint=”SendMessage”)] [DllImport(“Oleacc.dll”)] [DllImport(“Oleacc.dll”)] 由于微软出的Messenger产品及版本繁多,而且插件也不少,所以兼容性不是很强,实用性不大,但是,个人觉得代码还是有一些借鉴作用。
using System;
using Accessibility;
{
/**//// <summary>
/// 对Messenger窗口进行操作
/// </summary>
public class MessengerWindowHelper
{
IntPtr _messengerWindowHandle ;
IntPtr _inputBoxHandle ;
IntPtr _submitButtonHandle ;
IAccessible _inputBox ;
IAccessible _submitButton ;
public MessengerWindowHelper(IntPtr windowHandle)
{
_messengerWindowHandle = windowHandle ;
_inputBoxHandle = GetInputBoxHandle() ;
_submitButtonHandle = GetSubmitButton() ;
{
GetAccessibleObjects(_messengerWindowHandle, out _inputBox, out _submitButton);
}
}
/// 输入消息
/// </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>
public void SendMessage()
{
if (_submitButton == null)
{
Win32.SendMessageInt(_submitButtonHandle, Win32.WM_CLICK, IntPtr.Zero, 0);
}
else
{
_submitButton.accDoDefaultAction(Win32.CHILDID_SELF) ;
}
}
{
IntPtr topInputHandle = Win32.FindWindowEx(_messengerWindowHandle, System.IntPtr.Zero, “RichEdit20W”, null) ;
return Win32.FindWindowEx(_messengerWindowHandle, topInputHandle, “RichEdit20W”, null) ;
}
{
return Win32.FindWindowEx(_messengerWindowHandle, System.IntPtr.Zero, “Button”, “发送(&S)”) ;
}
{
object[] rgvarChildren = new object[paccContainer.accChildCount] ;
int pcObtained ;
Win32.AccessibleChildren(paccContainer,0,paccContainer.accChildCount, rgvarChildren, out pcObtained);
return rgvarChildren ;
}
{
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);
submitButtion = null ;
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 ;
}
{
submitButtion = childChild ;
}
{
break ;
}
}
break ;
}
}
}
}
}
using System.Runtime.InteropServices;
{
/**//// <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;
public static extern Int32 FindWindow(String lpClassName,String lpWindowName);
public static extern IntPtr FindWindowEx(
IntPtr parentHandle,
IntPtr childAfter,
string lpszClass,
int sWindowTitle /**//*HWND*/);
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
public static extern int SendMessageString (IntPtr hwnd, int wMsg, IntPtr wParam, string lParam);
public static extern int SendMessageInt (IntPtr hwnd, int wMsg, IntPtr wParam, int lParam);
public static extern int AccessibleObjectFromWindow(
IntPtr hwnd,
int dwObjectID,
ref Guid refID,
ref IAccessible ppvObject);
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);
}
}
c#控制windows messenger和windows live messenger窗口发送消息_c#应用
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » c#控制windows messenger和windows live messenger窗口发送消息_c#应用
相关推荐
-      利用c#远程存取access数据库_c#应用
-      c# 3.0新特性系列:隐含类型var_c#教程
-      c#动态生成树型结构的web程序设计_c#应用
-      论c#变得越来越臃肿是不可避免的_c#应用
-      用c#监控并显示cpu状态信息_c#应用
-      c#中实现vb中的createobject方法_c#应用
-      photoshop给花瓶打造彩绘效果_photoshop教程
-      使用c#创建sql server的存储过程_c#应用