对准器(捕获窗口句柄)

对准器(捕获窗口句柄)


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

示例代码下载:CFinder.zip (请不要直接使用迅雷下载)

测试环境:VC6.0+WinXP
图片预览:

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

1、功能:

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

2、使用方法:

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保存了找到窗口的句柄
 
3、CFinder代码:

使用前添加资源图标php

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);html

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

   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 ;
};spa

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


4、调用示例:
htm


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

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);
}



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

5、请看另一篇相关文章:

捕获窗口信息的小工具
http://hi.baidu.com/qiujiejia/blog/item/cb8ab1eec95e20ebb2fb9507.html

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

推荐:

 

谈老师的BLOG (佛教大德)
狮姐的博客(狮子窝,学佛与灵魂之探究)
大方广(学习传统文化)
慈善点击(轻松一点,行善积德,何乐不为)
电影《地球公民》(揭示鲜为人知的一面)  
心向光明 远离邪淫(现世警钟,不可不看)
戒淫(上篇)(正淫节欲,戒除邪淫)
戒淫(中篇)(纵欲之乐,忧患随之)
公民教育——命由我造