对准器(捕获窗口句柄)

对准器(捕获窗口句柄)


━━━━━━━━━━━━━━━━━━━━━━━━
示例代码下载:CFinder.zip (请不要直接使用迅雷下载)
测试环境:VC6.0+WinXP
图片预览:

━━━━━━━━━━━━━━━━━━━━━━━━

一、功能:
寻找窗口句柄。我们经常在一些软件中能看到它的身影,如常用工具spy++。

二、使用方法:
1.添加3个资源图标和鼠标 IDB_NOMAL IDB_BLANK IDC_CROSS0
2.在对话框上放一个PICTURE控件,ID号为IDC_CAPTURE,选择位图类型,再选择IDB_NOMAL
3.调用方法:
    鼠标按下时调用 CFinder::MouseDown(m_hWnd);
    鼠标弹起时调用 CFinder::MouseUp();
    鼠标移动时调用 
 if (CFinder::MouseUp())
 {
  //捕捉了窗口
 }

4.CFinder::FindWnd保存了找到窗口的句柄
 
三、CFinder代码:

使用前添加资源图标

class CFinder
{
public:
 static void MouseDown(HWND hWnd)
 {
  hStatic  = ::GetDlgItem(hWnd,IDC_CAPTURE);
  hBmpBlank = LoadBitmap (::GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BLANK));
  hCurCross = LoadCursor (::GetModuleHandle(NULL), MAKEINTRESOURCE(IDC_CROSS0));
  
  POINT point;
  ::GetCursorPos(&point);
  RECT rc;
  ::GetWindowRect(hStatic, &rc);
  if( ::PtInRect(&rc, point) )
  {
   bCapture = true;
   
   ::SetCursor( hCurCross );
   hBmpNomal=(HBITMAP)::SendMessage (hStatic,STM_SETIMAGE,IMAGE_BITMAP,(long)hBmpBlank);
   ::SetCapture( hWnd );
   
   //必须把上次窗口句柄置空
   FindWnd=NULL;
  }
 }
 static bool MouseUp()
 {
  if( bCapture )
  {
   bCapture = false;
   ::SetCursor( LoadCursor(NULL,IDC_ARROW) );
   ::SendMessage (hStatic,STM_SETIMAGE,IMAGE_BITMAP,(long)hBmpNomal);
   ::ReleaseCapture();
   
   if( FindWnd )
    HeightLightWnd(FindWnd);

   ::DeleteObject(hBmpBlank);
   ::DeleteObject(hCurCross);

   return true;
  }
  else
   return false;
 }
 static bool MouseMove()
 {
  if (bCapture)
  {
   POINT point;
   ::GetCursorPos(&point);
   HWND hWnd=SmallestWindowFromPoint(point);
//   HWND hWnd=::WindowFromPoint(point);
   
   //第一次进入窗口
   if (FindWnd != hWnd)
   {   
    HeightLightWnd(FindWnd);//刷新旧窗口
    FindWnd=hWnd;   //保存wnd
    HeightLightWnd(hWnd); //高亮度新窗口
   }
   
   return true;
  }
  else
   return false;
 }

 /****************************************************************************
 把窗体的边框画上线条
 参数1 hWnd : 指定窗口句柄
 ****************************************************************************/
 static void HeightLightWnd(HWND hWnd)
 {
  RECT DesRect;
  ::GetWindowRect(hWnd,&DesRect);
  DesRect.left+=2; DesRect.top+=2;  DesRect.right-=2; DesRect.bottom-=2;
  
  HDC hdc=::GetDC(NULL);    
  HPEN Pen=::CreatePen(PS_SOLID,4,RGB(0,0,0));
  HPEN OldPen=(HPEN)SelectObject(hdc,Pen) ;
  SelectObject (hdc, GetStockObject (NULL_BRUSH)) ; //设置为空刷
  
  SetROP2 (hdc, R2_NOT) ; //当前绘制的像素值设为屏幕像素值的反,这样可以覆盖掉上次的绘图(自动擦除上次图形)
  Rectangle (hdc, DesRect.left,DesRect.top,DesRect.right,DesRect.bottom) ; //擦除原来的图形
  
  ::SelectObject (hdc,OldPen) ;
  ::DeleteObject(Pen);
  ::ReleaseDC(NULL,hdc);
 }

 //-----------------------------------------------
 // SmallestWindowFromPoint
 // Notice: from PasswordSpy by Brian Friesen
 //
 // Find the smallest window still containing the point
 //
 // WindowFromPoint returns the first window in the Z-order ->
 // if the password control is sorounded by a Group Box or some other control,
 // WindowFromPoint returns the handle to the sorounding control instead
 // to the password control.
 //
 static HWND SmallestWindowFromPoint( const POINT point )
 { 
  RECT rect, rcTemp;
  HWND hParent, hWnd, hTemp;
  
  hWnd = ::WindowFromPoint( point );
  if( hWnd != NULL )
  {
   ::GetWindowRect( hWnd, &rect );
   hParent = ::GetParent( hWnd );
   
   // Has window a parent?
   if( hParent != NULL )
   {
    // Search down the Z-Order
    hTemp = hWnd;
    do{
     hTemp = ::GetWindow( hTemp, GW_HWNDNEXT );
     
     // Search window contains the point, hase the same parent, and is visible?
     ::GetWindowRect( hTemp, &rcTemp );
     if(::PtInRect(&rcTemp, point) && ::GetParent(hTemp) == hParent && ::IsWindowVisible(hTemp))
     {
      // Is it smaller?
      if(((rcTemp.right - rcTemp.left) * (rcTemp.bottom - rcTemp.top)) < ((rect.right - rect.left) * (rect.bottom - rect.top)))
      {
       // Found new smaller window!
       hWnd = hTemp;
       ::GetWindowRect(hWnd, &rect);
      }
     }
    }while( hTemp != NULL );
   }
  }
  
  return hWnd;
 }

public:
 static HWND  FindWnd;
private:
 static HWND  hStatic;
 static HCURSOR  hCurCross;
 static HBITMAP hBmpNomal;
 static HBITMAP hBmpBlank;
 static bool  bCapture ;
};

HWND CFinder::hStatic=NULL;
HCURSOR CFinder::hCurCross=NULL;
HBITMAP CFinder::hBmpNomal=NULL;
HBITMAP CFinder::hBmpBlank=NULL;
bool CFinder::bCapture=false;
HWND CFinder::FindWnd=NULL;


四、调用示例:


void CDemoDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
 CFinder::MouseDown(m_hWnd);
 CDialog::OnLButtonDown(nFlags, point);
}

void CDemoDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
 if (CFinder::MouseUp() )
 {
  trace(CFinder::FindWnd);
 }
 CDialog::OnLButtonUp(nFlags, point);
}

void CDemoDlg::OnMouseMove(UINT nFlags, CPoint point)
{
 CFinder::MouseMove();
 CDialog::OnMouseMove(nFlags, point);
}



━━━━━━━━━━━━━━━━━━━━━━━━

五、请看另外一篇相关文章:
捕获窗口信息的小工具
http://hi.baidu.com/qiujiejia/blog/item/cb8ab1eec95e20ebb2fb9507.html
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值