用Visual C++设计窗体探测器

本文介绍了一种用于检测屏幕中嵌套最深窗口的算法。通过宽度和深度搜索结合,能够准确地定位到指定坐标下的顶层或子级窗口,并且考虑了可见性和层级关系。

HWND BrotherWindowFromPoint(HWND hWndPoint,const POINT Point)
{
 //检测兄弟窗口
 RECT rcPoint;
 RECT rcNow;
 HWND hWndBrother=hWndPoint;//GetWindow(hWndPoint,GW_HWNDFIRST);
 hWndPoint=NULL;
 do
 {
  if(GetWindowstyle(hWndBrother)&WS_VISIBLE)//可见
  {
   GetWindowRect(hWndBrother,&rcNow);
   if(PtInRect(&rcNow,Point))
   {
    //检验矩形嵌套情况
    if(hWndPoint==NULL)
    {
     hWndPoint=hWndBrother;
     rcPoint=rcNow;
    }
    else if(
     ((rcNow.bottom<rcPoint.bottom)&&(rcNow.bottom>rcPoint.top)&&
     (rcNow.left>rcPoint.left)&&(rcNow.left<rcPoint.right))//左下角
     ||((rcNow.bottom<rcPoint.bottom)&&(rcNow.bottom>rcPoint.top)&&
     (rcNow.right>rcPoint.left)&&(rcNow.right<rcPoint.right))//右下角
     ||((rcNow.top>rcPoint.top)&&(rcNow.top<rcPoint.bottom)&&
     (rcNow.left> ;rcPoint.left)&&(rcNow.left<rcPoint.right))//左上角
     ||((rcNow.top>rcPoint.top)&&(rcNow.top<rcPoint.bottom)
     &&(rcNow.right>rcPoint.left)&&(rcNow.right<rcPoint.right))//右上角
    )
   {
    hWndPoint=hWndBrother;
    rcPoint=rcNow;
   }
  }
 }
 }while(hWndBrother=GetWindow(hWndBrother,GW_HWNDNEXT));
  return hWndPoint;
}


  该函数检测同层窗口,获得指定点内,嵌套最深的窗口,由此我们便可以生成我们自己的WindowFromPoint

HWND MyWindowFromPoint(const POINT Point)
{
 HWND hWndPoint=WindowFromPoint(Point);
 if(hWndPoint)
 {
  //宽度搜索兄弟窗口
  HWND hWndChild;
  if(!(GetWindowLong(hWndPoint,GWL_STYLE)&WS_CHILD))//顶层窗口
  return hWndPoint;
  //非顶层窗口,要进行兄弟查找.
  hWndPoint=MyBrotherWindowFromPoint(hWndPoint,Point);
  assert(hWndPoint);
  //深度搜索子窗口
  while(hWndChild=GetTopWindow(hWndPoint))
  {
   //宽度搜索兄弟子窗口
   if(NULL==(hWndChild=MyBrotherWindowFromPoint(hWndChild,Point)))
   break;
   hWndPoint=hWndChild;
  } //*/
 }
 return hWndPoint;
}

  该函数首先判断是否是顶层窗体,如果不是,首先进行宽度搜索,虽然麻烦了些,然而却不得不如此。顺便说一下,VC资源编辑器中正在设计的对话框拥有disable属性,spyxx你的窗体探测器也不能得到其内的所有控件句柄,而该函数所向无敌,如果去掉BrotherWindowFromPoint函数内的可见性判断,隐藏窗体也无处藏身。有兴趣的朋友可以亲自设计一下,如果你是懒惰者,可到华军软件园下在该程序红色间谍.
文章出处:飞诺网(www.firnow.com):http://dev.firnow.com/course/3_program/vc/vc_js/20100630/266066.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值