利用ie控件做些事情 哈~
2013年09月22日
⁄ 综合
⁄ 共 7684字 ⁄ 字号
小 中 大
- #include "stdafx.h"
- #include "UseHtml.h"
- #include "UseHtmlDlg.h"
- #include "mshtmlc.h"
- CUseHtmlDlg::CUseHtmlDlg(CWnd* pParent )
- : CDialog(CUseHtmlDlg::IDD, pParent)
- {
- char szPath[MAX_PATH] = {0};
- int nPos = 0;
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
-
- ::GetModuleFileName(NULL, szPath, MAX_PATH);
- this->m_strPath = szPath;
- nPos = this->m_strPath.ReverseFind(_T('//'));
- ASSERT(nPos != -1);
- if (-1 == nPos)
- {
- return;
- }
- this->m_strPath = this->m_strPath.Left(nPos);
- }
- void CUseHtmlDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- DDX_Control(pDX, IDC_EXPLORER, m_expIe);
- }
- BEGIN_MESSAGE_MAP(CUseHtmlDlg, CDialog)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
-
- ON_BN_CLICKED(IDC_BTN_CLEAR, OnBnClickedBtnClear)
- ON_BN_CLICKED(IDC_BTN_MUSIC, OnBnClickedBtnMusic)
- ON_BN_CLICKED(IDC_BTN_PHOTO, OnBnClickedBtnPhoto)
- ON_BN_CLICKED(IDC_BTN_WORD, OnBnClickedBtnWord)
- ON_BN_CLICKED(IDC_BTN_PDF, OnBnClickedBtnPdf)
- ON_BN_CLICKED(IDC_BTN_RECORD, OnBnClickedBtnRecord)
- ON_BN_CLICKED(IDC_BTN_PRINT, OnBnClickedBtnPrint)
- END_MESSAGE_MAP()
- BEGIN_EVENTSINK_MAP(CUseHtmlDlg, CDialog)
- ON_EVENT(CUseHtmlDlg, IDC_EXPLORER, 250, BeforeNavigate2Explorer, VTS_DISPATCH VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PBOOL)
- END_EVENTSINK_MAP()
- BOOL CUseHtmlDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
-
-
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
-
-
- SetIcon(m_hIcon, TRUE);
- SetIcon(m_hIcon, FALSE);
-
-
- return TRUE;
- }
- void CUseHtmlDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
-
-
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- void CUseHtmlDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this);
- SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
-
- 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;
-
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- HCURSOR CUseHtmlDlg::OnQueryDragIcon()
- {
- return static_cast<HCURSOR>(m_hIcon);
- }
- void CUseHtmlDlg::OnBnClickedBtnClear()
- {
- this->m_expIe.Navigate("about:blank", NULL, NULL, NULL, NULL);
- }
- void CUseHtmlDlg::OnBnClickedBtnMusic()
- {
- this->m_expIe.Navigate(this->m_strPath + "//music.htm", NULL, NULL, NULL, NULL);
- }
- void CUseHtmlDlg::OnBnClickedBtnPhoto()
- {
- this->m_expIe.Navigate(this->m_strPath + "//image.htm", NULL, NULL, NULL, NULL);
- }
- void CUseHtmlDlg::OnBnClickedBtnWord()
- {
- this->m_expIe.Navigate(this->m_strPath + "//word.doc", NULL, NULL, NULL, NULL);
- }
- void CUseHtmlDlg::OnBnClickedBtnPdf()
- {
- this->m_expIe.Navigate(this->m_strPath + "//reader.pdf", NULL, NULL, NULL, NULL);
- }
- void CUseHtmlDlg::OnBnClickedBtnRecord()
- {
- this->m_expIe.Navigate(this->m_strPath + "//form.htm", NULL, NULL, NULL, NULL);
- }
- void CUseHtmlDlg::OnBnClickedBtnPrint()
- {
- this->m_expIe.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, NULL, NULL);
- }
- void CUseHtmlDlg::BeforeNavigate2Explorer(LPDISPATCH pDisp, VARIANT* URL, VARIANT* Flags, VARIANT* TargetFrameName, VARIANT* PostData, VARIANT* Headers, BOOL* Cancel)
- {
- CString strUrl(URL->bstrVal);
- int nPos = 0;
- int i = 0;
-
- nPos = strUrl.ReverseFind('//');
- if (-1 == nPos)
- {
- strUrl.ReverseFind('/');
- }
- try
- {
- if (strUrl.Mid(nPos + 1).CompareNoCase("ToDialog"))
- {
- throw(0);
- }
- if (0 == ((PostData->vt) & (VT_VARIANT | VT_BYREF)) )
- {
- throw(0);
- }
- VARIANT* v = PostData->pvarVal;
- if (0 == ((v->vt) & (VT_UI1 | VT_ARRAY)) )
- {
- throw(1);
- }
- SAFEARRAY* pArr = v->parray;
- CString strData = (LPCSTR)pArr->pvData;
- CStringArray arrPart;
- while (TRUE)
- {
- nPos = strData.Find('&');
- if (-1 == nPos)
- {
- arrPart.Add(strData);
- break;
- }
- arrPart.Add(strData.Left(nPos));
- strData = strData.Mid(nPos + 1);
- }
- CString strRet;
- for (i = 0; i < arrPart.GetSize(); i++)
- {
- CString strPart;
- strPart = arrPart.GetAt(i);
- nPos = strPart.Find('=');
- ASSERT(nPos != -1);
- CString strName = strPart.Left(nPos);
- CString strValue = strPart.Mid(nPos + 1);
- strName = WebStr2Str(strName);
- strValue= WebStr2Str(strValue);
- strRet += strName + " = " + strValue + "<br>";
- }
-
- IHTMLDocument2* pDoc = (IHTMLDocument2*)this->m_expIe.get_Document();
- VARIANT* param;
- SAFEARRAY* sfArray;
- BSTR bstr = strRet.AllocSysString();
- sfArray = ::SafeArrayCreateVector(VT_VARIANT, 0, 1);
- if (sfArray && pDoc)
- {
- if (S_OK == ::SafeArrayAccessData(sfArray, (LPVOID*)¶m))
- {
- param->vt = VT_BSTR;
- param->bstrVal = bstr;
- ::SafeArrayUnaccessData(sfArray);
- pDoc->write(sfArray);
- }
- ::SysFreeString(bstr);
- if (sfArray)
- {
- ::SafeArrayDestroy(sfArray);
- sfArray = NULL;
- }
- }
- pDoc->Release();
- *Cancel = TRUE;
- }
- catch(...)
- {
- *Cancel = FALSE;
- }
- }
- CString CUseHtmlDlg::WebStr2Str(LPCTSTR lpBuf)
- {
- CString str;
- int nLen = 0;
- int i = 0;
- if (NULL == lpBuf)
- {
- nLen = 0;
- }
- else
- {
- nLen = ::_tcslen(lpBuf);
- }
- i = 0;
- while (i < nLen)
- {
- if ('%' == lpBuf[i])
- {
- BYTE b1 = lpBuf[i + 1];
- BYTE b2 = lpBuf[i + 2];
- i += 2;
- if (b1 >= '0' && b1 <= '9')
- {
- b1 = (b1 - '0') * 16;
- }
- else if (b1 >= 'A' && b1 <= 'Z')
- {
- b1 = (b1 - 'A' + 10) * 16;
- }
- else if (b1 >= 'a' && b1 <= 'z')
- {
- b1 = (b1 - 'a' + 10) * 16;
- }
- if (b2 >= '0' && b2 <= '9')
- {
- b2 = (b2 - '0');
- }
- else if (b2 >= 'A' && b2 <= 'Z')
- {
- b2 = (b2 - 'A' + 10);
- }
- else if (b2 >= 'a' && b2 <= 'z')
- {
- b2 = (b2 - 'a' + 10);
- }
- char szStr[2] = {0};
- szStr[0] = b1 + b2;
- szStr[1] = '/0';
- str += szStr;
- }
- else if ('+' == lpBuf[i])
- {
- str += " ";
- }
- else
- {
- str += CString(&lpBuf[i], 1);
- }
- i++;
- }
- return str;
- }