直接切入正题:
添加引用 项目>添加引用>C:\Progmme~\MSN Messenger\msnmsgrexe
using MessengerAPI;
声明:
private MessengerAPIMessengerClass msn;
private MessengerAPIIMessengerConversationWnd msnMsgWnd;
Init里添加:
msn = new MessengerClass();
msnOnIMWindowCreated +=new DMessengerEvents_OnIMWindowCreatedEventHandler(msn_OnIMWindowCreated);
msnOnIMWindowDestroyed +=new DMessengerEvents_OnIMWindowDestroyedEventHandler(msn_OnIMWindowDestroyed);
private void msn_OnIMWindowCreated(object pIMWindow)
{
msnMsgWnd = (IMessengerConversationWnd) pIMWindow; //取得IM窗口句柄
#region Get IM Message
SystemIntPtr ptrUIHWND = NativeWINFindWindowEx(msnMsgWndHWND DirectUIHWND );//取得窗口内DirectUIHWND句柄
Guid guidCOM= new Guid(xExCDxCFxxCxxAAxxxBx);//COM的GUID
IAccessible IACurrent=null;//IAccessible集
try
{
NativeWINAccessibleObjectFromWindow(ptrUIHWND(int)NativeMsgOBJID_CLIENTref guidCOMref IACurrent); //取得ptrUIHWND中IAccessible集
IACurrent = (IAccessible)IACurrentaccParent;//其父才是IAccessible集真正的容器
int _ChildCount = IACurrentaccChildCount;
object[] _Children = new object[_ChildCount];
int _out;
NativeWINAccessibleChildren(IACurrent_ChildCount_Childrenout _out); //从IACurrent中将所有子IAccessible加入_Children数组中
foreach(IAccessible _child in _Children)
{
string _accName = _childget_accName((int)NativeMsgCHILDID_SELF);
}
}
catch(Exception ex)
{
throw ex;
}
#endregion
}
private void msn_OnIMWindowDestroyed(object pIMWindow)
{
msnMsgWnd = null;
}
直接查找消息窗体的方法
Private void FindIMwindow()
{SystemIntPtr hWndStart;
int _next = ;
do
{
hWndStart = NativeWINFindWindowEx( _next IMWindowClass );
_next =hWndStartToInt();
}while(_next != );
}
最后是API声明
#region API wrapper
public class NativeWIN
{
[DllImport(userdll CharSet=CharSetAuto)]
public static extern IntPtr FindWindowEx(
int parent /*HWND*/
int next /*HWND*/
string lpszClass
string sWindowTitle);
[DllImport(Oleaccdll)]
public static extern int AccessibleObjectFromWindow(
IntPtr hwnd
int dwObjectID
Guid refID
ref IAccessible ppvObject);
[DllImport(Oleaccdll)]
public static extern int AccessibleChildren(
AccessibilityIAccessible paccContainer
int iChildStart
int cChildren
[Out] object[] rgvarChildren
out int pcObtained);
}
public enum NativeMsg:long {
CHILDID_SELF=
CHILDID_ =
OBJID_CLIENT= xFFFFFFC
}
#endregion