📄 getihtmldlg.cpp
字号:
// GetIhtmlDlg.cpp : implementation file
//
#include "stdafx.h"
#include "GetIhtml.h"
#include "GetIhtmlDlg.h"
#include "aboutdlg.h"
#include <atlbase.h>
#include <oleacc.h>
#include <winuser.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CGetIhtmlDlg dialog
//You can store the interface pointer in a member variable
//for easier access
//Modified by Chu Fang
/*函数名:GetDocInterface
参数:hWnd,WebBrowser控件的窗口句柄
功能:通过WM_HTML_GETOBJECT取得控件的IHTMLDocument2接口
*/
IHTMLDocument2* GetDocInterface(HWND hWnd)
{
// 我们需要显示地装载OLEACC.DLL,这样我们才知道有没有安装MSAA
HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );
IHTMLDocument2* pDoc2=NULL;
if ( hInst != NULL ){
if ( hWnd != NULL ){
CComPtr<IHTMLDocument> spDoc=NULL;
LRESULT lRes;
UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
::SendMessageTimeout( hWnd, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );
LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T("ObjectFromLresult") );
if ( pfObjectFromLresult != NULL ){
HRESULT hr;
hr=pfObjectFromLresult(lRes,IID_IHTMLDocument,0,(void**)&spDoc);
if ( SUCCEEDED(hr) ){
CComPtr<IDispatch> spDisp;
CComQIPtr<IHTMLWindow2> spWin;
spDoc->get_Script( &spDisp );
spWin = spDisp;
spWin->get_document( &pDoc2 );
}
}
}
::FreeLibrary(hInst);
}
else{//如果没有安装MSAA
AfxMessageBox(_T("请您安装Microsoft Active Accessibility"));
}
return pDoc2;
}
/*函数名:GetDocInterfaceByMSAA
参数:hwnd,WebBrowser控件的窗口句柄
功能:取得hwnd对应的Webbrowser控件的IHTMLDocument2*接口.
*/
IHTMLDocument2* GetDocInterfaceByMSAA(HWND hwnd)
{
HRESULT hr;
HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );
IHTMLDocument2* pDoc2=NULL;
if ( hInst != NULL ){
if ( hwnd != NULL ){
//取得AccessibleObjectFromWindow函数
LPFNACCESSIBLEOBJECTFROMWINDOW pfAccessibleObjectFromWindow =
(LPFNACCESSIBLEOBJECTFROMWINDOW)::GetProcAddress(hInst,_T("AccessibleObjectFromWindow"));
if(pfAccessibleObjectFromWindow != NULL){
CComPtr<IAccessible> spAccess;
hr=pfAccessibleObjectFromWindow(hwnd,0,
IID_IAccessible,(void**) &spAccess);//取得Webbrowser控件的IAccessible接口
if ( SUCCEEDED(hr) ){
CComPtr<IServiceProvider> spServiceProv;
hr=spAccess->QueryInterface(IID_IServiceProvider,(void**)&spServiceProv);
if(hr==S_OK){
CComPtr<IHTMLWindow2> spWin;
hr=spServiceProv->QueryService(IID_IHTMLWindow2,IID_IHTMLWindow2,
(void**)&spWin);
/*
注意:并不是每次都能取得IHTMLWindow2接口,如果调用失败,可以尝试取得IHTMLElement接口:
CComPtr<IHTMLElement> spElement;
hr=spServiceProv->QueryService(IID_IHTMLElement,IID_IHTMLElement,(void**)&spElement);
*/
if(hr==S_OK)
spWin->get_document(&pDoc2);
}
}
}
}
::FreeLibrary(hInst);
}
else{
AfxMessageBox(_T("请您安装Microsoft Active Accessibility"));
}
return pDoc2;
}
CGetIhtmlDlg::CGetIhtmlDlg(CWnd* pParent /*=NULL*/)
: CDialog(CGetIhtmlDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CGetIhtmlDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_bCapture=FALSE;
}
void CGetIhtmlDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CGetIhtmlDlg)
DDX_Control(pDX, IDC_GETHTML, m_3);
DDX_Control(pDX, IDC_GETPWD, m_2);
DDX_Control(pDX, IDC_ABOUT, m_1);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CGetIhtmlDlg, CDialog)
//{{AFX_MSG_MAP(CGetIhtmlDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_LBUTTONUP()
ON_BN_CLICKED(IDC_ABOUT, OnAbout)
ON_BN_CLICKED(IDC_GETHTML, OnGethtml)
ON_BN_CLICKED(IDC_GETPWD, OnGetpwd)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//参数:lpLine,命令行
//功能:运行可执行程序 lpLine为命令行
void RunExec(LPCTSTR lpLine)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
// Initialize the STARTUPINFO structure.
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
CreateProcess(
NULL, // pointer to name of executable module
(LPTSTR) lpLine, // pointer to command line string
NULL, // pointer to process security attributes
NULL, // pointer to thread security attributes
FALSE, // handle inheritance flag
0, // creation flags
NULL, // pointer to new environment block
NULL, // pointer to current directory name
&si, // pointer to STARTUPINFO
&pi // pointer to PROCESS_INFORMATION
);
}
//将pContent中的内容保存在一个临时文件,运行notepad.exe打开它.
void CGetIhtmlDlg::SaveAndShow(BSTR pContent)
{
static TCHAR lpFileName[MAX_PATH];
static TCHAR lpTempPath[MAX_PATH];
DWORD len;
if(GetTempPath(50,lpTempPath)!=0){
if(GetTempFileName(lpTempPath,_T("Get"),0,lpFileName)!=0){
HANDLE hFile=::CreateFile(lpFileName,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
0,///FILE_FLAG_DELETE_ON_CLOSE,
NULL
);
if(hFile!=NULL){
CString strContent(pContent);
if(!WriteFile(hFile,strContent,strContent.GetLength(),&len,NULL)){
AfxMessageBox("Can't not write to file");
}
CloseHandle(hFile);
CString str=_T("notepad ");
str+=lpFileName;
RunExec(str);
}
else
AfxMessageBox("Error in open file");
}
}
}
/////////////////////////////////////////////////////////////////////////////
// CGetIhtmlDlg message handlers
BOOL CGetIhtmlDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
SetWindowPos(&wndTopMost,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
pSysMenu->RemoveMenu(0,MF_BYPOSITION);
pSysMenu->RemoveMenu(1,MF_BYPOSITION);
pSysMenu->RemoveMenu(2,MF_BYPOSITION);
}
m_1.SetIcon(IDI_ICONABOUT);
m_2.SetIcon(IDI_ICONGETPWD);
m_3.SetIcon(IDI_ICONSOURCE);
EnableToolTips(TRUE);
m_tooltip.Create(this,TTS_ALWAYSTIP);
m_tooltip.Activate(TRUE);
m_tooltip.AddTool(&m_1,_T("关于本软件..."));
m_tooltip.AddTool(&m_2,_T("取得网页密码框密码"));
m_tooltip.AddTool(&m_3,_T("取得源代码"));
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CGetIhtmlDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
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_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CGetIhtmlDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CGetIhtmlDlg::OnGethtml()
{
m_bCapture=TRUE;
m_uAction=_GETHTML_;
SetCapture();
}
void CGetIhtmlDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
if(m_bCapture){
m_bCapture=FALSE;
ReleaseCapture();
static TCHAR buf[100];
POINT pt;
GetCursorPos(&pt);
HWND hwnd=::WindowFromPoint(pt);
if(hwnd!=NULL){
::GetClassName( hwnd, (LPTSTR)&buf, 100 );
if ( _tcscmp( buf, _T("Internet Explorer_Server") ) == 0 ){
if(m_uAction==_GETHTML_)
GetHtmlSource(GetDocInterface(hwnd));
// GetHtmlSource(GetDocInterfaceByMSAA(hwnd));
else{
POINT iept=pt;
::ScreenToClient(hwnd,&iept);
GetPassword(GetDocInterface(hwnd),iept);
// GetPassword(GetDocInterfaceByMSAA(hwnd),iept);
}
}
}
}
CDialog::OnLButtonUp(nFlags, point);
}
void CGetIhtmlDlg::OnAbout()
{
CAboutDlg dlg;
dlg.DoModal();
}
void CGetIhtmlDlg::OnGetpwd()
{
m_uAction=_GETPWD_;
m_bCapture=TRUE;
SetCapture();
}
BOOL CGetIhtmlDlg::PreTranslateMessage(MSG* pMsg)
{
if(m_tooltip.GetSafeHwnd()!=NULL)
m_tooltip.RelayEvent(pMsg);
return CDialog::PreTranslateMessage(pMsg);
}
void CGetIhtmlDlg::GetHtmlSource(IHTMLDocument2* pHDoc2)
{
if(pHDoc2==NULL)return;
CComPtr<IHTMLElementCollection> pAllColl;
HRESULT hr;
hr=pHDoc2->get_all(&pAllColl);
if(hr==S_OK){
LONG length=0;
hr=pAllColl->get_length(&length);
if(hr==S_OK){
for(int i=0;i<length;i++){
VARIANT vIndex,vName;
vName.vt=vIndex.vt=VT_I4;
vName.lVal=vIndex.lVal=i;
CComPtr<IDispatch> pDisp;
hr=pAllColl->item(vName,vIndex,&pDisp);
if( hr==S_OK ){
CComPtr<IHTMLElement> pElement;
hr=pDisp->QueryInterface(IID_IHTMLElement,(void**)&pElement);
if( hr==S_OK ){
CComBSTR tagName;
hr=pElement->get_tagName(&tagName);
if(hr==S_OK){
CString str(tagName);
if(str=="HTML"){
CComBSTR pContent;
hr=pElement->get_outerHTML(&pContent);
if(hr==S_OK){
SaveAndShow(pContent);
i=length;//以便退出循环
}
else{//if get_outerHTML failed
AfxMessageBox("can't get html code");
}
}//else if tagName isnot 'HTML'
}//else if get_tagName failed
}//else if don't get IHMTLElement interface
}//if no items
}
}//if get_length failed
}//if get_all failed
pHDoc2->Release();
}
void CGetIhtmlDlg::GetPassword(IHTMLDocument2 *pDoc2,POINT pt)
{
if(pDoc2==NULL)return;
CComPtr<IHTMLElement> pElement;
HRESULT hr=pDoc2->elementFromPoint(pt.x,pt.y,&pElement);
if(SUCCEEDED(hr)){
CComPtr<IHTMLInputTextElement> pPwdElement;
hr=pElement->QueryInterface(IID_IHTMLInputTextElement,
(void**)&pPwdElement);
if(SUCCEEDED(hr)){
CComBSTR type;
hr=pPwdElement->get_type(&type);
if(SUCCEEDED(hr)){
if(type==_T("password")){
CComBSTR pwd;
hr=pPwdElement->get_value(&pwd);
if(SUCCEEDED(hr)){
if(pwd.Length()!=0){
CComBSTR msg=_T("密码是:");
msg+=pwd;
CString str(msg);
AfxMessageBox(str);
}
else{
AfxMessageBox(_T("密码为空!"));
}
}
}
}
}
}
pDoc2->Release();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -