熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

.NET下基於API封裝的DirectUIHWND窗體訪問

2013-11-13 10:21:39  來源: .NET編程 

  直接切入正題:
  
  添加引用 項目>添加引用>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
From:http://tw.wingwit.com/Article/program/net/201311/13375.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.