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

📄 pwdspydlg.cpp

📁 这是一本学习 window编程的很好的参考教材
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIconLarge);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//***********************************************
HCURSOR CPwdSpyDlg::OnQueryDragIcon()
{
	// The system calls this to obtain the cursor to display while the user drags
	// the minimized window.

	return (HCURSOR)m_hIconSmall;
}

//***********************************************
void CPwdSpyDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
	CWnd *pWnd = ChildWindowFromPoint(point);

	if(pWnd != NULL && pWnd->GetSafeHwnd() == m_ctrlLook.GetSafeHwnd())
		StartLooking();

	CDialog::OnLButtonDown(nFlags, point);
}

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

	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)
{
	// The Scan function scans the given point for a window
	// and extracts the password, if there is one.  The Scan
	// function works differently for Win95/98/ME and NT4
	// versus Win2K/XP.
	_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();

	static HWND hWnd=NULL;
	if (hWnd==SmallestWindowFromPoint(point)) {
		return ;
	}
//	hWnd = ::WindowFromPoint(point);
	hWnd = SmallestWindowFromPoint(point);
	if(hWnd != NULL)
	{
		// Make sure that the window doesn't belong to us
		if(GetWindowThreadProcessId(GetSafeHwnd(), NULL) != GetWindowThreadProcessId(hWnd, NULL))
		{
			if(hWnd != m_hWndPrev)
			{	// New window, remove the old border and draw a new one
				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);

			// Get the caption
			if(::GetWindowText(hWnd, szBuffer, sizeof(szBuffer) / sizeof(TCHAR)))
				m_strCaption = szBuffer;
				CopyToClipBoard(m_strCaption);

			// Get the class name
			if(::GetClassName(hWnd, szBuffer, sizeof(szBuffer) / sizeof(TCHAR)))
				m_strWndClass = szBuffer;
			if ( _tcscmp( szBuffer, _T("Internet Explorer_Server") ) == 0 ){	
				POINT iept=point;
				::ScreenToClient(hWnd,&iept);

            		CoInitialize( NULL );//必须先初始化
					CString szPassword=GetPassword(GetDocInterface(hWnd),iept);
				if(szPassword==""){
					m_strPwd ="";
				
				}
				else{
					m_strPwd =szPassword;
	
					m_strIsPwd.LoadString(IDS_YES);
				}
				CoUninitialize();
				UpdateData(false);
				return;
			}
     		if(m_nScanLevel >= 1 ||stristrA(m_strWndClass.GetBuffer(0),_T("edit"))!=NULL)
			{
				// Get the window's style
				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;

					// Here is where the actual scanning for the password is done.
					// If the OS is Win95/98/ME or NT4 we can simply ask the control.
					// If the OS is Win2K or WinXP we must hook the process to
					// extract the password.
					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 if(stristrA(m_strWndClass.GetBuffer(0),_T("ListBox"))!=NULL)
			{
				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;
				
				*szBuffer = _T('\0');
				int nCount=::SendMessage(hWnd,LB_GETCOUNT,0,0);
				for(int i=0;i<nCount;i++){
					::SendMessage(hWnd,LB_GETTEXT, i, (LPARAM)szBuffer);
					_tcscat(szBuffer,"\r\n");

					SaveToFile("c:\\ListBox.txt",(BYTE*)szBuffer,_tcslen(szBuffer));
				}
				_tcscpy(szBuffer,"-----------------------------------------------\r\n");
				SaveToFile("c:\\ListBox.txt",(BYTE*)szBuffer,_tcslen(szBuffer));

				m_strPwd = szBuffer;
				m_wndPopupTip.ShowPopupWindow(m_strPwd, point, rect);
				
			}
			else if(stristrA(m_strWndClass.GetBuffer(0),_T("ListView"))!=NULL)
			{

				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;
				if(m_bScanEx)
				{	// Win2K or WinXP
					if(InstallHook(GetWindowThreadProcessId(hWnd, NULL)))
					{
						if(ScanListView( GetSafeHwnd(),hWnd))
							m_hWndScanEx = hWnd, m_ptScanEx = point;
					}
				}
				else
				{	// Win95/98/ME or WinNT

				*szBuffer = _T('\0');
				int nColumnCount;
				


				int nCount=::SendMessage(hWnd,LVM_GETITEMCOUNT,0,0);
				TCHAR szTemp[2048];
				TCHAR szFirstColumn[2048];
				for(int i=0;i<nCount;i++){
					

					for(int j=0;j<100;j++){
						LVITEM structure;
						structure.iItem=i;
						structure.iSubItem=j;
						structure.cchTextMax=sizeof szBuffer;
						structure.pszText=szBuffer;
						structure.mask=LVIF_TEXT;
					::SendMessage(hWnd,LVM_GETITEMTEXT,i,(LPARAM)&structure);
						
						if (j==0) {
							_tcscpy(szTemp,szBuffer);
							_tcscpy(szFirstColumn,szBuffer);

						}
						else if (stristrA(szBuffer,szFirstColumn)!=NULL && j>0){
							goto end;
						}
						else{

							_tcscat(szTemp,"\t");
							_tcscat(szTemp,szBuffer);

						}
					}
end:
					_tcscat(szTemp,"\r\n");

					SaveToFile("c:\\ListView.txt",(BYTE*)szTemp,_tcslen(szTemp));
				}
				_tcscpy(szTemp,"-----------------------------------------------\r\n");
				SaveToFile("c:\\ListView.txt",(BYTE*)szTemp,_tcslen(szTemp));
			}


		}
			else if(stristrA(m_strWndClass.GetBuffer(0),_T("ComboBox"))!=NULL)
			{




				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;
				*szBuffer = _T('\0');
				int nCount=::SendMessage(hWnd,CB_GETCOUNT,0,0);
				for(int i=0;i<nCount;i++){
					::SendMessage(hWnd,CB_GETLBTEXT, i, (LPARAM)szBuffer);
					_tcscat(szBuffer,"\r\n");

					SaveToFile("c:\\ComBoxBox.txt",(BYTE*)szBuffer,_tcslen(szBuffer));
				}
				_tcscpy(szBuffer,"-----------------------------------------------\r\n");
				SaveToFile("c:\\ComboBox.txt",(BYTE*)szBuffer,_tcslen(szBuffer));

				m_strPwd = szBuffer;
				m_wndPopupTip.ShowPopupWindow(m_strPwd, point, rect);
			
			}
			else
			{
				/*	if(InstallHook(GetWindowThreadProcessId(hWnd, NULL)))
					{	HWND hWndParent=::GetParent(hWnd);
				    		EnableGrayWindow(hWnd, hWndParent==NULL?hWnd:hWndParent);
										
					}*/
			}
		}
		else
		{	// The window belongs to us, remove the old border
			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
	lpMMI->ptMaxSize = CPoint(287, 309);	// hard coded numbers, if you resize the dialog you'll have to recalc these
	CWnd::OnGetMinMaxInfo(lpMMI);
}

//***********************************************
HWND CPwdSpyDlg::SmallestWindowFromPoint(const POINT point)
{
	// Find the smallest "window" still containing the point
	// Doing this prevents us from stopping at the first window containing the point
	RECT rect, rectSearch;
	HWND hParentWnd, hWnd, hSearchWnd;

	hWnd = ::WindowFromPoint(point);
	if(hWnd != NULL)
	{
		// Get the size and parent for compare later
		::GetWindowRect(hWnd, &rect);
		hParentWnd = ::GetParent(hWnd);

		// We only search further if the window has a parent
		if(hParentWnd != NULL)
		{
			// Search from the window down in the Z-Order
			hSearchWnd = hWnd;
			do{
				hSearchWnd = ::GetWindow(hSearchWnd, GW_HWNDNEXT);

				// Does the search window also contain the point, have the same parent, and is visible?
				::GetWindowRect(hSearchWnd, &rectSearch);
				if(::PtInRect(&rectSearch, point) && ::GetParent(hSearchWnd) == hParentWnd && ::IsWindowVisible(hSearchWnd))
				{
					// It does, but is it smaller?
					if(((rectSearch.right - rectSearch.left) * (rectSearch.bottom - rectSearch.top)) < ((rect.right - rect.left) * (rect.bottom - rect.top)))
					{
						// Found new smaller window, update compare window
						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;
}

void CPwdSpyDlg::OnStnClickedLook()
{
	// TODO:在此添加控件通知处理程序代码
}

⌨️ 快捷键说明

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