.net下基于API封装的DirectUIHWND窗体访问

直接切入正题:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值