首页 > 编程 > .NET > 正文

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

2024-07-10 12:58:53
字体:
来源:转载
供稿:网友
 

直接切入正题:

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表