现在的位置: 首页 > 综合 > 正文

利用ie控件做些事情 哈~

2013年09月22日 ⁄ 综合 ⁄ 共 7684字 ⁄ 字号 评论关闭
 
  1. // UseHtmlDlg.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "UseHtml.h"
  5. #include "UseHtmlDlg.h"
  6. #include "mshtmlc.h"
  7. CUseHtmlDlg::CUseHtmlDlg(CWnd* pParent /*=NULL*/)
  8.     : CDialog(CUseHtmlDlg::IDD, pParent)
  9. {
  10.     char szPath[MAX_PATH] = {0};
  11.     int nPos = 0;
  12.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  13.     
  14.     ::GetModuleFileName(NULL, szPath, MAX_PATH);
  15.     this->m_strPath = szPath;
  16.     nPos = this->m_strPath.ReverseFind(_T('//'));
  17.     ASSERT(nPos != -1);
  18.     if (-1 == nPos)
  19.     {
  20.         return;
  21.     }
  22.     this->m_strPath = this->m_strPath.Left(nPos);
  23. }
  24. void CUseHtmlDlg::DoDataExchange(CDataExchange* pDX)
  25. {
  26.     CDialog::DoDataExchange(pDX);
  27.     DDX_Control(pDX, IDC_EXPLORER, m_expIe);
  28. }
  29. BEGIN_MESSAGE_MAP(CUseHtmlDlg, CDialog)
  30.     ON_WM_SYSCOMMAND()
  31.     ON_WM_PAINT()
  32.     ON_WM_QUERYDRAGICON()
  33.     //}}AFX_MSG_MAP
  34.     ON_BN_CLICKED(IDC_BTN_CLEAR, OnBnClickedBtnClear)
  35.     ON_BN_CLICKED(IDC_BTN_MUSIC, OnBnClickedBtnMusic)
  36.     ON_BN_CLICKED(IDC_BTN_PHOTO, OnBnClickedBtnPhoto)
  37.     ON_BN_CLICKED(IDC_BTN_WORD, OnBnClickedBtnWord)
  38.     ON_BN_CLICKED(IDC_BTN_PDF, OnBnClickedBtnPdf)
  39.     ON_BN_CLICKED(IDC_BTN_RECORD, OnBnClickedBtnRecord)
  40.     ON_BN_CLICKED(IDC_BTN_PRINT, OnBnClickedBtnPrint)
  41. END_MESSAGE_MAP()
  42. BEGIN_EVENTSINK_MAP(CUseHtmlDlg, CDialog)
  43.     ON_EVENT(CUseHtmlDlg, IDC_EXPLORER, 250, BeforeNavigate2Explorer, VTS_DISPATCH VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PBOOL)
  44. END_EVENTSINK_MAP()
  45. // CUseHtmlDlg 消息处理程序
  46. BOOL CUseHtmlDlg::OnInitDialog()
  47. {
  48.     CDialog::OnInitDialog();
  49.     // 将/“关于.../”菜单项添加到系统菜单中。
  50.     // IDM_ABOUTBOX 必须在系统命令范围内。
  51.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  52.     ASSERT(IDM_ABOUTBOX < 0xF000);
  53.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  54.     if (pSysMenu != NULL)
  55.     {
  56.         CString strAboutMenu;
  57.         strAboutMenu.LoadString(IDS_ABOUTBOX);
  58.         if (!strAboutMenu.IsEmpty())
  59.         {
  60.             pSysMenu->AppendMenu(MF_SEPARATOR);
  61.             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  62.         }
  63.     }
  64.     // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
  65.     //  执行此操作
  66.     SetIcon(m_hIcon, TRUE);         // 设置大图标
  67.     SetIcon(m_hIcon, FALSE);        // 设置小图标
  68.     // TODO: 在此添加额外的初始化代码
  69.     
  70.     return TRUE;  // 除非设置了控件的焦点,否则返回 TRUE
  71. }
  72. void CUseHtmlDlg::OnSysCommand(UINT nID, LPARAM lParam)
  73. {
  74.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  75.     {
  76.         //CAboutDlg dlgAbout;
  77.         //dlgAbout.DoModal();
  78.     }
  79.     else
  80.     {
  81.         CDialog::OnSysCommand(nID, lParam);
  82.     }
  83. }
  84. // 如果向对话框添加最小化按钮,则需要下面的代码
  85. //  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
  86. //  这将由框架自动完成。
  87. void CUseHtmlDlg::OnPaint() 
  88. {
  89.     if (IsIconic())
  90.     {
  91.         CPaintDC dc(this); // 用于绘制的设备上下文
  92.         SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  93.         // 使图标在工作矩形中居中
  94.         int cxIcon = GetSystemMetrics(SM_CXICON);
  95.         int cyIcon = GetSystemMetrics(SM_CYICON);
  96.         CRect rect;
  97.         GetClientRect(&rect);
  98.         int x = (rect.Width() - cxIcon + 1) / 2;
  99.         int y = (rect.Height() - cyIcon + 1) / 2;
  100.         // 绘制图标
  101.         dc.DrawIcon(x, y, m_hIcon);
  102.     }
  103.     else
  104.     {
  105.         CDialog::OnPaint();
  106.     }
  107. }
  108. //当用户拖动最小化窗口时系统调用此函数取得光标显示。
  109. HCURSOR CUseHtmlDlg::OnQueryDragIcon()
  110. {
  111.     return static_cast<HCURSOR>(m_hIcon);
  112. }
  113. void CUseHtmlDlg::OnBnClickedBtnClear()
  114. {
  115.     this->m_expIe.Navigate("about:blank", NULL, NULL, NULL, NULL);
  116. }
  117. void CUseHtmlDlg::OnBnClickedBtnMusic()
  118. {
  119.     this->m_expIe.Navigate(this->m_strPath + "//music.htm", NULL, NULL, NULL, NULL);
  120. }
  121. void CUseHtmlDlg::OnBnClickedBtnPhoto()
  122. {
  123.     this->m_expIe.Navigate(this->m_strPath + "//image.htm", NULL, NULL, NULL, NULL);
  124. }
  125. void CUseHtmlDlg::OnBnClickedBtnWord()
  126. {
  127.     this->m_expIe.Navigate(this->m_strPath + "//word.doc", NULL, NULL, NULL, NULL);
  128. }
  129. void CUseHtmlDlg::OnBnClickedBtnPdf()
  130. {
  131.     this->m_expIe.Navigate(this->m_strPath + "//reader.pdf", NULL, NULL, NULL, NULL);
  132. }
  133. void CUseHtmlDlg::OnBnClickedBtnRecord()
  134. {
  135.     this->m_expIe.Navigate(this->m_strPath + "//form.htm", NULL, NULL, NULL, NULL);
  136. }
  137. void CUseHtmlDlg::OnBnClickedBtnPrint()
  138. {
  139.     this->m_expIe.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, NULL, NULL);
  140. }
  141. void CUseHtmlDlg::BeforeNavigate2Explorer(LPDISPATCH pDisp, VARIANT* URL, VARIANT* Flags, VARIANT* TargetFrameName, VARIANT* PostData, VARIANT* Headers, BOOL* Cancel)
  142. {
  143.     CString strUrl(URL->bstrVal);
  144.     int nPos = 0;
  145.     int i = 0;
  146.     
  147.     nPos = strUrl.ReverseFind('//');
  148.     if (-1 == nPos)
  149.     {
  150.         strUrl.ReverseFind('/');
  151.     }
  152.     try
  153.     {
  154.         if (strUrl.Mid(nPos + 1).CompareNoCase("ToDialog"))
  155.         {
  156.             throw(0); //不是预定义的URL
  157.         }
  158.         if (0 == ((PostData->vt) & (VT_VARIANT | VT_BYREF)) )
  159.         {
  160.             throw(0);
  161.         }
  162.         VARIANT* v = PostData->pvarVal;
  163.         if (0 == ((v->vt) & (VT_UI1 | VT_ARRAY)) )
  164.         {
  165.             throw(1);
  166.         }
  167.         SAFEARRAY* pArr = v->parray;
  168.         CString strData = (LPCSTR)pArr->pvData;//至此,得到传送的数据流
  169.         CStringArray arrPart;
  170.         while (TRUE)
  171.         {
  172.             nPos = strData.Find('&');
  173.             if (-1 == nPos)
  174.             {
  175.                 arrPart.Add(strData);
  176.                 break;
  177.             }
  178.             arrPart.Add(strData.Left(nPos));
  179.             strData = strData.Mid(nPos + 1);
  180.         }
  181.         CString strRet;
  182.         for (i = 0; i < arrPart.GetSize(); i++)
  183.         {
  184.             CString strPart;
  185.             strPart = arrPart.GetAt(i);
  186.             nPos = strPart.Find('=');
  187.             ASSERT(nPos != -1);
  188.             CString strName = strPart.Left(nPos);
  189.             CString strValue = strPart.Mid(nPos + 1);
  190.             strName = WebStr2Str(strName);      //转换Web字符串到标准字符串
  191.             strValue= WebStr2Str(strValue);
  192.             strRet += strName + " = " + strValue + "<br>";
  193.         }
  194.         //////////// 以下是演示使用DHTML //////////////
  195.         IHTMLDocument2* pDoc = (IHTMLDocument2*)this->m_expIe.get_Document();
  196.         VARIANT* param;
  197.         SAFEARRAY* sfArray;
  198.         BSTR bstr = strRet.AllocSysString();
  199.         sfArray = ::SafeArrayCreateVector(VT_VARIANT, 0, 1);
  200.         if (sfArray && pDoc)
  201.         {
  202.             if (S_OK == ::SafeArrayAccessData(sfArray, (LPVOID*)&param))
  203.             {
  204.                 param->vt = VT_BSTR;
  205.                 param->bstrVal = bstr;
  206.                 ::SafeArrayUnaccessData(sfArray);
  207.                 pDoc->write(sfArray);
  208.             }
  209.             ::SysFreeString(bstr);
  210.             if (sfArray)
  211.             {
  212.                 ::SafeArrayDestroy(sfArray);
  213.                 sfArray = NULL;
  214.             }
  215.         }
  216.         pDoc->Release();
  217.         *Cancel = TRUE;
  218.     }
  219.     catch(...)
  220.     {
  221.         *Cancel = FALSE;
  222.     }
  223. }
  224. CString CUseHtmlDlg::WebStr2Str(LPCTSTR lpBuf)
  225. {
  226.     CString str;
  227.     int nLen    = 0;
  228.     int i       = 0;
  229.     if (NULL == lpBuf)
  230.     {
  231.         nLen = 0;
  232.     }
  233.     else
  234.     {
  235.         nLen = ::_tcslen(lpBuf);
  236.     }
  237.     i = 0;
  238.     while (i < nLen)
  239.     {
  240.         if ('%' == lpBuf[i])
  241.         {
  242.             BYTE b1 = lpBuf[i + 1];
  243.             BYTE b2 = lpBuf[i + 2];
  244.             i += 2;
  245.             if (b1 >= '0' && b1 <= '9')
  246.             {
  247.                 b1 = (b1 - '0') * 16;
  248.             }
  249.             else if (b1 >= 'A' && b1 <= 'Z')
  250.             {
  251.                 b1 = (b1 - 'A' + 10) * 16;
  252.             }
  253.             else if (b1 >= 'a' && b1 <= 'z')
  254.             {
  255.                 b1 = (b1 - 'a' + 10) * 16;
  256.             }
  257.             if (b2 >= '0' && b2 <= '9')
  258.             {
  259.                 b2 = (b2 - '0');
  260.             }
  261.             else if (b2 >= 'A' && b2 <= 'Z')
  262.             {
  263.                 b2 = (b2 - 'A' + 10);
  264.             }
  265.             else if (b2 >= 'a' && b2 <= 'z')
  266.             {
  267.                 b2 = (b2 - 'a' + 10);
  268.             }
  269.             char szStr[2] = {0};
  270.             szStr[0] = b1 + b2;
  271.             szStr[1] = '/0';
  272.             str += szStr;
  273.         }
  274.         else if ('+' == lpBuf[i])
  275.         {
  276.             str += " ";
  277.         }
  278.         else
  279.         {
  280.             str += CString(&lpBuf[i], 1);
  281.         }
  282.         i++;
  283.     }
  284.     return str;
  285. }

抱歉!评论已关闭.