直接切入正题:
1.添加引用. 项目->添加引用->C:\Progmme~1\MSN Messenger\msnmsgr.exe
2. using MessengerAPI;
声明:
private MessengerAPI.MessengerClass msn;
private MessengerAPI.IMessengerConversationWnd msnMsgWnd;
3.Init里添加:
msn = new MessengerClass();
msn.OnIMWindowCreated +=new DMessengerEvents_OnIMWindowCreatedEventHandler(msn_OnIMWindowCreated);
msn.OnIMWindowDestroyed +=new DMessengerEvents_OnIMWindowDestroyedEventHandler(msn_OnIMWindowDestroyed);
4.
private void msn_OnIMWindowCreated(object pIMWindow)
{
msnMsgWnd = (IMessengerConversationWnd) pIMWindow; //取得IM窗口句柄
#region Get IM Message
System.IntPtr ptrUIHWND = NativeWIN32.FindWindowEx(msnMsgWnd.HWND, 0, "DirectUIHWND", 0); //取得窗口内DirectUIHWND句柄
Guid guidCOM= new Guid(0x618736E0,0x3C3D,0x11CF,0x81,0xC,0x0,0xAA,0x0,0x38,0x9B,0x71); //COM的GUID
IAccessible IACurrent=null; //IAccessible集
try
{
NativeWIN32.AccessibleObjectFromWindow(ptrUIHWND,(int)NativeMsg.OBJID_CLIENT,ref guidCOM,ref IACurrent); //取得ptrUIHWND中IAccessible集
IACurrent = (IAccessible)IACurrent.accParent; //其父,才是IAccessible集真正的容器
int _ChildCount = IACurrent.accChildCount;
object[] _Children = new object[_ChildCount];
int _out;
NativeWIN32.AccessibleChildren(IACurrent,0,_ChildCount-1,_Children,out _out); //从IACurrent中,将所有子IAccessible加入_Children数组中
foreach(IAccessible _child in _Children)
{
string _accName = _child.get_accName((int)NativeMsg.CHILDID_SELF);
}
}
catch(Exception ex)
{
throw ex;
}
#endregion
}
private void msn_OnIMWindowDestroyed(object pIMWindow)
{
msnMsgWnd = null;
}
5.直接查找消息窗体的方法
Private void FindIMwindow()
{System.IntPtr hWndStart;
int _next = 0;
do
{
hWndStart = NativeWIN32.FindWindowEx(0, _next, "IMWindowClass", 0);
_next = hWndStart.ToInt32();
}while(_next != 0);
}
6.最后是API声明
#region API wrapper
public class NativeWIN32
{
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr FindWindowEx(
int parent /*HWND*/,
int next /*HWND*/,
string lpszClass,
string sWindowTitle);
[DllImport("Oleacc.dll")]
public static extern int AccessibleObjectFromWindow(
IntPtr hwnd,
int dwObjectID,
Guid refID,
ref IAccessible ppvObject);
[DllImport("Oleacc.dll")]
public static extern int AccessibleChildren(
Accessibility.IAccessible paccContainer,
int iChildStart,
int cChildren,
[Out] object[] rgvarChildren,
out int pcObtained);
}
public enum NativeMsg:long {
CHILDID_SELF = 0,
CHILDID_1 = 1,
OBJID_CLIENT = 0xFFFFFFC
}
#endregion
查看本文来源