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

📄 pwdspydlg.cpp

📁 包括了一些VC编程实例
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	CDialog::OnLButtonUp(nFlags, point);
}

//***********************************************
void CPwdSpyDlg::StartLooking(void)
{
	m_nScanLevel = ((CComboBox*)GetDlgItem(IDC_COMBO_LEVEL))->GetCurSel();
	if(m_nScanLevel == CB_ERR) m_nScanLevel = 0;

	SetCapture();
	m_bIsLooking = true;

	m_hCursorPrev = SetCursor(m_hCursorScan);
	m_ctrlLook.SetIcon(m_hIconBlank);
}

//***********************************************
void CPwdSpyDlg::StopLooking(void)
{
	ReleaseCapture();
	m_bIsLooking = false;

	// If we've hooked another process, remove the hook
	if(m_bScanEx)
		RemoveHook();

	m_wndPopupTip.HidePopupWindow();
	if(m_hWndPrev != NULL)
	{
		InvertBorder(m_hWndPrev);
		m_hWndPrev = NULL;
	}

//	SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
	SetCursor(m_hCursorPrev);
	m_ctrlLook.SetIcon(m_hIconScan);

	// Redraw the whole screen
	::InvalidateRect(NULL, NULL, FALSE);
}

//***********************************************
void CPwdSpyDlg::OnMouseMove(UINT nFlags, CPoint point)
{
	if(m_bIsLooking)
		Scan(point);

	CDialog::OnMouseMove(nFlags, point);
}

//***********************************************
// 该函数扫描给定位置的窗口,得到密码
void CPwdSpyDlg::Scan(CPoint point)
{
	
	_ASSERTE(m_bIsLooking);

	bool bFound = false;

	ClientToScreen(&point);//转换到屏幕坐标

	m_strMousePos.Format(_T("X=%ld, Y=%ld"), point.x, point.y);
	m_strHwnd.Empty();
	m_strCaption.Empty();
	m_strWndClass.Empty();
	m_strIsPwd.Empty();
	m_strPwd.Empty();

	HWND hWnd;
//	hWnd = ::WindowFromPoint(point);
	hWnd = SmallestWindowFromPoint(point);//找到包含该点的最小窗口
	if(hWnd != NULL)
	{
		// 确保找到的窗口不是自己的窗口
		if(GetWindowThreadProcessId(GetSafeHwnd(), NULL) != GetWindowThreadProcessId(hWnd, NULL))
		{
			if(hWnd != m_hWndPrev)
			{	
				// 如果是新的窗口,则把原来老的边界去掉,画新窗口的边界
				m_wndPopupTip.HidePopupWindow();
				InvertBorder(m_hWndPrev);
				m_hWndPrev = hWnd;
				InvertBorder(m_hWndPrev);
			}

			TCHAR szBuffer[256];
			m_strHwnd.Format(_T("0x%08X"), hWnd);
			m_strIsPwd.LoadString((m_nScanLevel == 2) ? IDS_NOT_AVAILABLE : IDS_NO);

			// 得到窗口标题
			if(::GetWindowText(hWnd, szBuffer, sizeof(szBuffer) / sizeof(TCHAR)))
				m_strCaption = szBuffer;

			// 得到窗口类名字
			if(::GetClassName(hWnd, szBuffer, sizeof(szBuffer) / sizeof(TCHAR)))
				m_strWndClass = szBuffer;

			if(m_nScanLevel >= 1 || m_strWndClass.CompareNoCase(_T("edit")) == 0)
			{
				// 得到窗口风格
				long nStyle = ::GetWindowLong(hWnd, GWL_STYLE);
				if(m_nScanLevel == 2 || nStyle & ES_PASSWORD)
				{
					CRect rect; ::GetWindowRect(hWnd, &rect);
					if(m_nScanLevel == 0) m_strIsPwd.LoadString(IDS_YES);
					if(m_nScanLevel == 1) m_strIsPwd.LoadString(IDS_MAYBE);
					bFound = true;
               
					//这里用来得到密码.
					// 如果操作系统是 Win95/98/ME 或者 NT4 ,可以直接从控件得到
					// 如果操作系统是 Win2K or WinXP ,必须用钩子以得到密码
					if(m_bScanEx)
					{	// Win2K or WinXP
						if(InstallHook(GetWindowThreadProcessId(hWnd, NULL)))//安装钩子
						{
							if(ScanPassword(hWnd, GetSafeHwnd()))
								m_hWndScanEx = hWnd, m_ptScanEx = point;
						}
					}
					else
					{	// Win95/98/ME or WinNT
						*szBuffer = _T('\0');
						::SendMessage(hWnd, WM_GETTEXT, sizeof(szBuffer) / sizeof(TCHAR), (LPARAM)szBuffer);
						m_strPwd = szBuffer;
						m_wndPopupTip.ShowPopupWindow(m_strPwd, point, rect);
					}
				}
			}
		}
		else
		{	// 窗口属于自己,移除边界
			m_wndPopupTip.HidePopupWindow();
			InvertBorder(m_hWndPrev);
			m_hWndPrev = NULL;
		}
	}

	if(!bFound)
		m_wndPopupTip.HidePopupWindow();

	UpdateData(FALSE);//更新数据显示
}

//***********************************************
void CPwdSpyDlg::OnAlwaysOnTop()
{
	::SetWindowPos(GetSafeHwnd(), (m_bAlwaysOnTop) ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);

	// Check/uncheck the menu item
	CMenu *pSysMenu = GetSystemMenu(FALSE);
	if(pSysMenu != NULL)
	{
		UINT nFlags = MF_BYCOMMAND | (m_bAlwaysOnTop) ? MF_CHECKED : MF_UNCHECKED;
		pSysMenu->CheckMenuItem(IDM_ALWAYS_ON_TOP, nFlags);
	}
}

//***********************************************
BOOL CPwdSpyDlg::PreTranslateMessage(MSG *pMsg)
{
	if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE && m_bIsLooking)
	{
		StopLooking();
		return TRUE;
	}
	else
	{
		return CDialog::PreTranslateMessage(pMsg);
	}
}

//***********************************************
void CPwdSpyDlg::OnGetMinMaxInfo(MINMAXINFO FAR *lpMMI)
{
	// This function prevents the dialog from coming up full screen
	// if the user starts it "maximized"
	lpMMI->ptMaxSize = CPoint(286, 308);	// hard coded numbers, if you resize the dialog you'll have to recalc these
	CWnd::OnGetMinMaxInfo(lpMMI);
}

//***********************************************
//找到包含鼠标点的最小窗口
HWND CPwdSpyDlg::SmallestWindowFromPoint(const POINT point)
{

	RECT rect, rectSearch;
	HWND pWnd, hWnd, hSearchWnd;

	hWnd = ::WindowFromPoint(point);
	if(hWnd != NULL)
	{
		// 得到本窗口大小和父窗口句柄,以便比较
		::GetWindowRect(hWnd, &rect);
		pWnd = ::GetParent(hWnd);

		// 只有该窗口有父亲才继续比较
		if(pWnd != NULL)
		{
			// 按z方向搜索
			hSearchWnd = hWnd;
			do{
				hSearchWnd = ::GetWindow(hSearchWnd, GW_HWNDNEXT);

				// 是否新找到的窗口也包含该点,并且跟本窗口有同一个父亲,并且是可见的
				::GetWindowRect(hSearchWnd, &rectSearch);
				if(::PtInRect(&rectSearch, point) && ::GetParent(hSearchWnd) == pWnd && ::IsWindowVisible(hSearchWnd))
				{
					// 哪个更小
					if(((rectSearch.right - rectSearch.left) * (rectSearch.bottom - rectSearch.top)) < ((rect.right - rect.left) * (rect.bottom - rect.top)))
					{
						// 替换,继续查找
						hWnd = hSearchWnd;
						::GetWindowRect(hWnd, &rect);
					}
				}
			}while(hSearchWnd != NULL);
		}
	}

	return hWnd;
}

//***********************************************
void CPwdSpyDlg::InvertBorder(const HWND hWnd)
{
	if(!IsWindow(hWnd))
		return;

	RECT rect;

	// Get the coordinates of the window on the screen
	::GetWindowRect(hWnd, &rect);

	// Get a handle to the window's device context
	HDC hDC = ::GetWindowDC(hWnd);

	// Create an inverse pen that is the size of the window border
	SetROP2(hDC, R2_NOT);

	HPEN hPen = CreatePen(PS_INSIDEFRAME, 3 * GetSystemMetrics(SM_CXBORDER), RGB(0,0,0));

	// Draw the rectangle around the window
	HPEN hOldPen = (HPEN)SelectObject(hDC, hPen);
	HBRUSH hOldBrush = (HBRUSH)SelectObject(hDC, GetStockObject(NULL_BRUSH));

	Rectangle(hDC, 0, 0, rect.right - rect.left, rect.bottom - rect.top);

	SelectObject(hDC, hOldBrush);
	SelectObject(hDC, hOldPen);

	// Give the window its device context back, and destroy our pen
	::ReleaseDC(hWnd, hDC);

	DeleteObject(hPen);
}

//***********************************************
LRESULT CPwdSpyDlg::OnActivateApp(WPARAM wParam, LPARAM lParam)
{
	if(!IsWindowVisible()) ShowWindow(SW_SHOW);
	if(IsIconic()) ShowWindow(SW_RESTORE);
	SetForegroundWindow();

	return TRUE;
}

//***********************************************
//消息响应,一旦有数据到来,就调用该函数
BOOL CPwdSpyDlg::OnCopyData(CWnd *pWnd, COPYDATASTRUCT *pCopyDataStruct)
{
	try
	{
		if((HWND)pCopyDataStruct->dwData == m_hWndScanEx)//数据发送的窗口是否为正在查询的窗口
		{
			TCHAR szBuffer[256] = {_T('\0')};
			DWORD dwSize = sizeof(szBuffer) * sizeof(TCHAR);
			if(pCopyDataStruct->cbData < dwSize)
				dwSize = pCopyDataStruct->cbData;

			CopyMemory(szBuffer, pCopyDataStruct->lpData, dwSize);//密码拷贝

			m_strPwd = szBuffer;
			CRect rect; ::GetWindowRect(m_hWndScanEx, &rect);
			m_wndPopupTip.ShowPopupWindow(m_strPwd, m_ptScanEx, rect);

			UpdateData(FALSE);//显示
		}
	}
	catch(...) {}

	return TRUE;
}

⌨️ 快捷键说明

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