All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
delphi程序中动态创建、删除视图
SQL Server开发人员应聘常被问的问题_[SQL Server教程]
浅谈数据库设计技巧(上)_mssql学习_编程技术
SQL,Server,2005数据库产品线的扩展_[SQL,Server教程]
SQL,Server非正常删除日志文件(ldf)恢复方法_[SQL,Server教程]
asp.net2.0服务器控件之RadioButton控件
跟我学SQL:(二),SELECT语句选项_mssql学习_编程技术
SQL Server 索引结构及其使用(二)_[SQL Server教程]
delphi中间编译单元(.dcu)
用php把现有动态新闻页面改成静态页的方案_php资料_编程技术
Reading number is top 10 pictures
毕姥爷事件,告诉你6条真理
漂亮脸蛋魔鬼身材2
住院一星期,检测费两万
a pure sister
随便发几张图
Fan bingbing black wings for platform and DanLuoWang believes beauty2
男人巳快沦落成动物了
The money of more than 100 countries and regions9
Perfect small Laurie1
In the world the most mysterious 21 place landscape4
Download software ranking
小黑猫大战两米大花蛇
I for your crazy
Prostitutes diary
1400篇各类破解文章
Boxer's Top ten classic battle4
Unix video tutorial8
Ashlynn Video3
天龙八部十二宫服务端
功夫熊猫2(上集)
jdk1.6 for windows
delv published in(发表于) 2014/1/23 3:12:36 Edit(编辑)
C#.net,控制Windows,Messenger和Windows,Live,Messenger_[Asp.Net教程]

C#.net,控制Windows,Messenger和Windows,Live,Messenger_[Asp.Net教程]

C#.net 控制Windows Messenger和Windows Live Messenger_[Asp.Net教程]

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


namespace MessengerHelper
{
/**////


/// 对Messenger窗口进行操作
///

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);
}
}


/**////


/// 输入消息
///

///
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) ;
}
}


/**////


/// 发送消息
///

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
{
/**////


/// 调用Window API
///

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







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.