项目中要用到向CHtmlEditView中插入元素的功能,我搜索了一下,只提到用createElement和appentChild函数,但具体怎么搞,没有给出例子。我研究了一下,这里给出例子代码:
IHTMLDocument2 *pDoc=NULL;
GetDHtmlDocument(&pDoc); //这是CHtmlEditView的成员函数,得到IHtmlDocument2指针
VARIANT vt ;
vt.vt=VT_BSTR;
IHTMLElement *pElement=NULL;
CString str="<img src=http://www.baidu.com/img/logo.gif width=174 height=59 alt=/"点此进入 百度空间/" >";
VARIANT var;
var.vt = VT_BSTR;
var.bstrVal = str.AllocSysString();
pDoc->createElement(var.bstrVal,&pElement);//创建元素
IHTMLDOMNode *ppn=NULL;
IHTMLDOMNode *ppn2=NULL;
pElement->QueryInterface(IID_IHTMLDOMNode,(void **)&ppn);//获取该元素的DOMNode指针
IHTMLElement *pBodyElement=NULL;
pDoc->get_body(&pBodyElement);//获取BODY的IHTMLElement指针
IHTMLDOMNode *p=NULL;
pBodyElement->QueryInterface(IID_IHTMLDOMNode,(void **)&p);//获取BODY的DOMNode指针
p->appendChild(ppn,&ppn2);//将该元素插入到BODY中