📄 webctl.cpp
字号:
// WebCtl.cpp : Implementation of the CWebCtrl ActiveX Control class.
#include "stdafx.h"
#include "web.h"
#include "WebCtl.h"
#include "WebPpg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#import <msxml2.dll>
IMPLEMENT_DYNCREATE(CWebCtrl, COleControl)
/////////////////////////////////////////////////////////////////////////////
// Message map
BEGIN_MESSAGE_MAP(CWebCtrl, COleControl)
//{{AFX_MSG_MAP(CWebCtrl)
ON_WM_TIMER()
ON_WM_CREATE()
ON_WM_LBUTTONDOWN()
ON_WM_KEYDOWN()
//}}AFX_MSG_MAP
ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// Dispatch map
BEGIN_DISPATCH_MAP(CWebCtrl, COleControl)
//{{AFX_DISPATCH_MAP(CWebCtrl)
DISP_FUNCTION(CWebCtrl, "Read", Read, VT_EMPTY, VTS_NONE)
DISP_FUNCTION(CWebCtrl, "Write", Write, VT_EMPTY, VTS_NONE)
//}}AFX_DISPATCH_MAP
DISP_FUNCTION_ID(CWebCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
END_DISPATCH_MAP()
/////////////////////////////////////////////////////////////////////////////
// Event map
BEGIN_EVENT_MAP(CWebCtrl, COleControl)
//{{AFX_EVENT_MAP(CWebCtrl)
//}}AFX_EVENT_MAP
END_EVENT_MAP()
/////////////////////////////////////////////////////////////////////////////
// Property pages
// TODO: Add more property pages as needed. Remember to increase the count!
BEGIN_PROPPAGEIDS(CWebCtrl, 1)
PROPPAGEID(CWebPropPage::guid)
END_PROPPAGEIDS(CWebCtrl)
/////////////////////////////////////////////////////////////////////////////
// Initialize class factory and guid
IMPLEMENT_OLECREATE_EX(CWebCtrl, "WEB.WebCtrl.1",
0x89c476f8, 0xec9c, 0x4a6c, 0xa2, 0x5, 0x2c, 0xc0, 0x36, 0xc4, 0x37, 0x39)
/////////////////////////////////////////////////////////////////////////////
// Type library ID and version
IMPLEMENT_OLETYPELIB(CWebCtrl, _tlid, _wVerMajor, _wVerMinor)
/////////////////////////////////////////////////////////////////////////////
// Interface IDs
const IID BASED_CODE IID_DWeb =
{ 0x33e2dfb4, 0xf900, 0x4273, { 0xac, 0x54, 0x6f, 0xde, 0xf7, 0x2d, 0x2, 0xd3 } };
const IID BASED_CODE IID_DWebEvents =
{ 0xf58097e5, 0x19b0, 0x43d3, { 0x8f, 0xda, 0x66, 0x71, 0xbd, 0xe7, 0xa8, 0x4f } };
/////////////////////////////////////////////////////////////////////////////
// Control type information
static const DWORD BASED_CODE _dwWebOleMisc =
OLEMISC_ACTIVATEWHENVISIBLE |
OLEMISC_SETCLIENTSITEFIRST |
OLEMISC_INSIDEOUT |
OLEMISC_CANTLINKINSIDE |
OLEMISC_RECOMPOSEONRESIZE;
IMPLEMENT_OLECTLTYPE(CWebCtrl, IDS_WEB, _dwWebOleMisc)
/////////////////////////////////////////////////////////////////////////////
// CWebCtrl::CWebCtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CWebCtrl
BOOL CWebCtrl::CWebCtrlFactory::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_WEB,
IDB_WEB,
afxRegApartmentThreading,
_dwWebOleMisc,
_tlid,
_wVerMajor,
_wVerMinor);
else
return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}
/////////////////////////////////////////////////////////////////////////////
// CWebCtrl::CWebCtrl - Constructor
CWebCtrl::CWebCtrl()
{
InitializeIIDs(&IID_DWeb, &IID_DWebEvents);
CreateXMLFile("test.xml");
// TODO: Initialize your control's instance data here.
m_spSHWinds = NULL;
}
/////////////////////////////////////////////////////////////////////////////
// CWebCtrl::~CWebCtrl - Destructor
CWebCtrl::~CWebCtrl()
{
// TODO: Cleanup your control's instance data here.
if (m_spSHWinds){
m_spSHWinds.Release();
m_spSHWinds = 0;
}
CoUninitialize();
}
/////////////////////////////////////////////////////////////////////////////
// CWebCtrl::OnDraw - Drawing function
void CWebCtrl::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);
// CRect rect;
// GetWindowRect(&rect);
// m_btn.MoveWindow(&rect);
pdc->TextOut(0,0,"My Controller");
}
/////////////////////////////////////////////////////////////////////////////
// CWebCtrl::DoPropExchange - Persistence support
void CWebCtrl::DoPropExchange(CPropExchange* pPX)
{
ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
COleControl::DoPropExchange(pPX);
// TODO: Call PX_ functions for each persistent custom property.
}
/////////////////////////////////////////////////////////////////////////////
// CWebCtrl::OnResetState - Reset control to default state
void CWebCtrl::OnResetState()
{
COleControl::OnResetState(); // Resets defaults found in DoPropExchange
// TODO: Reset any other control state here.
}
/////////////////////////////////////////////////////////////////////////////
// CWebCtrl::AboutBox - Display an "About" box to the user
void CWebCtrl::AboutBox()
{
CDialog dlgAbout(IDD_ABOUTBOX_WEB);
dlgAbout.DoModal();
}
/////////////////////////////////////////////////////////////////////////////
// CWebCtrl message handlers
int CWebCtrl::CreateXMLFile(char *xml_file_name)
{
::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(MSXML2::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 CWebCtrl::Read()
{
// TODO: Add your dispatch handler code here
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
// int n = m_ctrlIE.GetItemCount();//GetCount();
//
// for (int i = 0; i < n; i ++){
// IWebBrowser2 *pBrowser = (IWebBrowser2 *)m_ctrlIE.GetItemData(i);
// if (pBrowser){
// pBrowser->Release();
// }
// }
//
// m_ctrlIE.DeleteAllItems();
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);
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");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -