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

来源:互联网 发布:安卓 adb mac 编辑:程序博客网 时间:2024/05/12 00:05

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

原创粉丝点击