⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 findwndtool.cpp

📁 模仿spy++ 截获取任意窗口句柄
💻 CPP
字号:
// FindWndTool.cpp : 实现文件
//

#include "stdafx.h"
#include "IRule.h"
#include "FindWndTool.h"


// CFindWndTool

IMPLEMENT_DYNAMIC(CFindWndTool, CStatic)

CFindWndTool::CFindWndTool()
{
	m_hDragCursor = (HCURSOR)LoadIcon(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDI_ICON_FIND));
	ASSERT(m_hDragCursor);
	m_bFind = FALSE;
	m_hwndUnderCursor = NULL;
	GdiplusStartup(&m_gdiplusToken, &m_gdiplusStartupInput, NULL);
}

CFindWndTool::~CFindWndTool()
{
}


BEGIN_MESSAGE_MAP(CFindWndTool, CStatic)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()



// CFindWndTool 消息处理程序



void CFindWndTool::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	::SetCursor(m_hDragCursor);
	SetCapture();
	m_bFind = TRUE;
	CStatic::OnLButtonDown(nFlags, point);
}

void CFindWndTool::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	m_bFind = FALSE;
	::ReleaseCapture();
	if(m_hwndUnderCursor)
	{
		DrawBorderInverted(m_hwndUnderCursor);
		m_hwndUnderCursor = NULL;
	}
	CStatic::OnLButtonUp(nFlags, point);
}

void CFindWndTool::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	static HWND hwndTemp = NULL, hwndTarget = NULL;
	static HWND hMainWnd = GetParent()->GetSafeHwnd();
	
	if(m_bFind)
	{
		POINT pt;
		if(!::GetCursorPos(&pt))
		{
			CStatic::OnMouseMove(nFlags, point);
			return;
		}
		
		hwndTemp = ::WindowFromPoint(static_cast<POINT>(pt));
		if(!hwndTemp)
		{
			CStatic::OnMouseMove(nFlags, point);
			return;
		}

		::ScreenToClient(hwndTemp, &pt);
		hwndTarget = ::ChildWindowFromPointEx(hwndTemp, pt, CWP_SKIPINVISIBLE | CWP_SKIPTRANSPARENT);
		{
			if(!hwndTarget)
				hwndTarget = hwndTemp;
		}

		if((m_hwndUnderCursor == hwndTarget) || (hwndTarget == hMainWnd) || (::GetParent(hwndTarget) == hMainWnd))

		{
			CStatic::OnMouseMove(nFlags, point);
			return;
		}
		

		if(m_hwndUnderCursor)
		{
			// draw again to remove the bounding rect
			DrawBorderInverted(m_hwndUnderCursor);
		}

		m_hwndUnderCursor = hwndTarget;
		GetParent()->PostMessage(msgFind, (WPARAM)hwndTarget, NULL);
		
		DrawBorderInverted(hwndTarget);
		// draw a rect around the target
		//Graphics graphics(::GetWindowDC(hwnd));
		//Pen pen(Color(0, 0, 0), 3);
		//graphics.DrawRectangle(&pen, 0, 0, rect.right - rect.left, rect.bottom - rect.top);
		//m_pGraphics->DrawRectangle(&pen, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
	}
	CStatic::OnMouseMove(nFlags, point);
}

/* 
the original author is 
Andrei Isac@codeproject.com
*/
void CFindWndTool::DrawBorderInverted(HWND hwnd)
{
	//HDC hdc;
	//hdc = ::GetWindowDC(hwnd);

	/*
	::GetWindowRect(hwnd, &rt);
	::OffsetRect(&rt,-rt.left,-rt.top);   
	::DrawFocusRect(hdc, &rt);
	::InflateRect(&rt,-1,-1); 
	::DrawFocusRect(hdc, &rt);
	::DrawFocusRect(hdc, &rt);
	::InflateRect(&rt,-1,-1); 
	::DrawFocusRect(hdc, &rt);

	::ReleaseDC(hwnd, hdc);
	*/
	CWnd* pWnd = CWnd::FromHandle(hwnd);
	CWindowDC dc(pWnd);
    // the bounding rectangle of the target window
    CRect rc;
    // get window bounding rectangle (absolute screen coordinates)
    pWnd->GetWindowRect(&rc);
    // save current device context state
    int dcSave = dc.SaveDC();
    // set drawing mode R2_NOT (Pixel is the inverse of the screen color)
    dc.SetROP2(R2_NOT);
    // create a pen that draws inside of a shape (PS_INSIDEFRAME)
    // the pen is three times thicker then the default Windows border size
    CPen pen;
    pen.CreatePen(PS_INSIDEFRAME, 
       3 * GetSystemMetrics(SM_CXBORDER), RGB(0, 0, 0));

    dc.SelectObject(&pen);
    dc.SelectObject(GetStockObject(NULL_BRUSH));
    
    dc.Rectangle(0, 0, rc.Width(), rc.Height());

    // done drawing, restore the CDC state to the saved one
    dc.RestoreDC(dcSave);
    // ReleaseDC called automatically for dc,
    // as well as DeleteObject for GDI objects
    // since we used automatic variables and the dc state was restored
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -