MABDlg.cpp : 实现文件 (QueryInterface,IMarkupServices,IMarkupPointer,IMarkupPointer2)

来源:互联网 发布:sql随机生成递增 编辑:程序博客网 时间:2024/04/28 12:09
#include "stdafx.h"
#include "MAB.h"
#include "MABDlg.h"


 #include <comdef.h>
#include <mshtml.h>
#pragma warning(disable : 4146) //see Q231931 for explaintation
#import <mshtml.tlb> no_auto_exclude


#ifdef _DEBUG
#define new DEBUG_NEW
#endif




// 用于应用程序“关于”菜单项的 CAboutDlg 对话框


class CAboutDlg : public CDialog
{
public:
CAboutDlg();


// 对话框数据
enum { IDD = IDD_ABOUTBOX };


protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持


// 实现
protected:
DECLARE_MESSAGE_MAP()
};


CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}


void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()




// CMABDlg 对话框








CMABDlg::CMABDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMABDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}


void CMABDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_EXPLORER1, webbrowser);
}


BEGIN_MESSAGE_MAP(CMABDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDOK, &CMABDlg::OnBnClickedOk)
ON_BN_CLICKED(IDC_BUTTON1, &CMABDlg::OnBnClickedButton1)
END_MESSAGE_MAP()




// CMABDlg 消息处理程序


BOOL CMABDlg::OnInitDialog()
{
CDialog::OnInitDialog();


// 将“关于...”菜单项添加到系统菜单中。


// IDM_ABOUTBOX 必须在系统命令范围内。
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);// 设置小图标


// TODO: 在此添加额外的初始化代码


return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}


void CMABDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}


// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。


void CMABDlg::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 CMABDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}




void CMABDlg::OnBnClickedOk()
{
LPCTSTR t=_T("http://www.baidu.com");
webbrowser.Navigate(t,0,0,0,0);
}
 
BOOL SetSelRange(CComQIPtr<IHTMLDocument2> pHTMLDocument2,long lStart,long lStop)
{
 BOOL bResult=FALSE;
 CComQIPtr<IHTMLElement>  pHTMLElement;
 CComQIPtr<IHTMLBodyElement> pHTMLBodyElement;
 CComQIPtr<IHTMLTxtRange> pHTMLTxtRange;
 CComQIPtr<IMarkupServices> pMarkupServices;
 CComQIPtr<IMarkupPointer> pMarkupPointer1;
 CComQIPtr<IMarkupPointer> pMarkupPointer2;
 CComQIPtr<IMarkupPointer2> pMarkupPointer21;
 CComQIPtr<IMarkupPointer2> pMarkupPointer22;
 if (pHTMLDocument2)
 {
  // 创建一个区域接口
  pHTMLDocument2->get_body(&pHTMLElement);
  if (pHTMLElement)
  {
   pHTMLElement->QueryInterface(IID_IHTMLBodyElement,(void**)&pHTMLBodyElement);
   if (pHTMLBodyElement)
   { 
    pHTMLBodyElement->createTextRange(&pHTMLTxtRange);
   }
  }
  // 获得起点和结束点的接口
  pHTMLDocument2->QueryInterface(IID_IMarkupServices , (LPVOID*)&pMarkupServices);
  if (pHTMLTxtRange&&pMarkupServices)
  {
   pMarkupServices->CreateMarkupPointer(&pMarkupPointer1);
   pMarkupServices->CreateMarkupPointer(&pMarkupPointer2);
   if (pMarkupPointer1&&pMarkupPointer2)
   {
    pMarkupServices->MovePointersToRange(pHTMLTxtRange,
    pMarkupPointer1, pMarkupPointer2);
    pMarkupPointer1->QueryInterface(IID_IMarkupPointer2,(void**)&pMarkupPointer21);
    pMarkupPointer2->QueryInterface(IID_IMarkupPointer2,(void**)&pMarkupPointer22);
   }
  }
  if (pMarkupPointer21&&pMarkupPointer22)
  {
   pMarkupPointer21->MoveToMarkupPosition(NULL,lStart);
   pMarkupPointer22->MoveToMarkupPosition(NULL,lStop);
   pMarkupServices->MoveRangeToPointers(pMarkupPointer21,
    pMarkupPointer22,pHTMLTxtRange);
   pHTMLTxtRange->select();
  }
 }
 return bResult;
}
BEGIN_EVENTSINK_MAP(CMABDlg, CDialog)
ON_EVENT(CMABDlg, IDC_EXPLORER1, 259, CMABDlg::DocumentCompleteExplorer1, VTS_DISPATCH VTS_PVARIANT)
END_EVENTSINK_MAP()


void CMABDlg::DocumentCompleteExplorer1(LPDISPATCH pDisp, VARIANT* URL)
{
// TODO: 在此处添加消息处理程序代码
IHTMLDocument2 *pHTMLDocument=NULL; 
if (!(pHTMLDocument = (IHTMLDocument2*)webbrowser.get_Document())) 
   return;
    SetSelRange(pHTMLDocument,180,30);
}


void CMABDlg::OnBnClickedButton1()
{
HRESULT hresult;
IDispatch * pdisp = (IDispatch *)NULL;
DISPID dispid;
OLECHAR * r=_T("color");
OLECHAR * szMember = r;


// Code that sets a pointer to the dispatch (pdisp) is omitted.


hresult = pdisp->GetIDsOfNames(
   IID_NULL,
   &szMember,
   1, LOCALE_SYSTEM_DEFAULT,
   &dispid);

}




查詢文檔的IDisplayServices接口,用IDisplayServices::CreateDisplayPointer來創建IDisplayPointer,用IDisplayPointer::MoveToPoint移動IDisplayPointer。
查詢文檔的IMarkupServices接口,用IMarkupServices::CreateMarkupPointer來創建IMarkupPointer,用IDisplayPointer::PositionMarkupPointer來移動IMarkupPointer。
查詢文檔的body的IHTMLBodyElement接口,用IHTMLBodyElement::createTextRange創建一個IHTMLTxtRange,用IMarkupServices::MoveRangeToPointers将IHTMLTxtRange移動到IMarkupPointer指定的位置,用IHTMLTxtRange::select選中IHTMLTxtRange的内容
用IHTMLDocument2::selection獲得選擇的内容的IHTMLSelectionObject接口,用IHTMLSelectionObject::createRange創建一個range,查詢其IHTMLControlRange接口,如果支持,那麽用IHTMLControlRange::item和IHTMLControlRange::length遍曆它裏面的元素。