科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件.NET下基于API封装的DirectUIHWND窗体访问

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

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

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

作者: 来源:中国IT实验室 2008年6月7日

关键字: 封装 API VB vb.net Windows

  • 评论
  • 分享微博
  • 分享邮件
直接切入正题:
  
  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
    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章