// 我想用自己的插入图片对话框代替CEDITHTMLVIEW中的插入图片对话框,
// 代码如下 CPoint point;
point = GetCaretPos(); IHTMLDocument2 * pDoc;
GetDHtmlDocument(&pDoc);
IHTMLElement * pElement = NULL;
IHTMLSelectionObject * pSelection = NULL;
IHTMLImgElement * pAnchor = NULL;
IHTMLTxtRange * pTextAreaElement = NULL; if(SUCCEEDED(pDoc->elementFromPoint(point.x,point.y,&pElement)))
{
CInsertPicDlg dlg;
VARIANT p;
VariantInit(&p);
p.vt = VT_I4;
BSTR bStr;
pElement->get_outerHTML(&bStr);
// 此处获得的bStr从HTML语法来说,没错,但是此处总判断为不是IHTMLImgElement
// 用类似的地址,也就是加入后htmlview自动修正后所得<img src="file:///c:/123.jpg">居然不行
if(SUCCEEDED(pElement->QueryInterface(IID_IHTMLImgElement,(void **)&pAnchor)))
{
BSTR bURL,bBorder,bAlign,bAlt;
long vspace,hspace;
pAnchor->get_src(&bURL);
pAnchor->get_align(&bAlign);
dlg.m_pic_path = (CW2A)bURL;
dlg.m_stralign = (CW2A)bAlign;
}
else
{
BSTR value;
pDoc->get_selection(&pSelection);
IDispatch * pDisp;
if(SUCCEEDED(pSelection->createRange(&pDisp)))
{
pDisp->QueryInterface(IID_IHTMLTxtRange,(void**)&pTextAreaElement);
pDisp->Release();
}
}
if(dlg.DoModal()==IDOK&&dlg.m_pic_path!="")
{
if(pAnchor)
{
pAnchor->put_src(dlg.m_pic_path.AllocSysString());
pAnchor->put_align(dlg.m_stralign.AllocSysString());
pAnchor->Release();
}
else if(pTextAreaElement)
{
CString picpath;
picpath.Format("<img src=\"%s\" align=\"%s\">",dlg.m_pic_path,dlg.m_stralign);
pTextAreaElement->pasteHTML(picpath.AllocSysString());
pTextAreaElement->Release();
pSelection->Release();
}
}
pElement->Release();
}
pDoc->Release();
// 代码如下 CPoint point;
point = GetCaretPos(); IHTMLDocument2 * pDoc;
GetDHtmlDocument(&pDoc);
IHTMLElement * pElement = NULL;
IHTMLSelectionObject * pSelection = NULL;
IHTMLImgElement * pAnchor = NULL;
IHTMLTxtRange * pTextAreaElement = NULL; if(SUCCEEDED(pDoc->elementFromPoint(point.x,point.y,&pElement)))
{
CInsertPicDlg dlg;
VARIANT p;
VariantInit(&p);
p.vt = VT_I4;
BSTR bStr;
pElement->get_outerHTML(&bStr);
// 此处获得的bStr从HTML语法来说,没错,但是此处总判断为不是IHTMLImgElement
// 用类似的地址,也就是加入后htmlview自动修正后所得<img src="file:///c:/123.jpg">居然不行
if(SUCCEEDED(pElement->QueryInterface(IID_IHTMLImgElement,(void **)&pAnchor)))
{
BSTR bURL,bBorder,bAlign,bAlt;
long vspace,hspace;
pAnchor->get_src(&bURL);
pAnchor->get_align(&bAlign);
dlg.m_pic_path = (CW2A)bURL;
dlg.m_stralign = (CW2A)bAlign;
}
else
{
BSTR value;
pDoc->get_selection(&pSelection);
IDispatch * pDisp;
if(SUCCEEDED(pSelection->createRange(&pDisp)))
{
pDisp->QueryInterface(IID_IHTMLTxtRange,(void**)&pTextAreaElement);
pDisp->Release();
}
}
if(dlg.DoModal()==IDOK&&dlg.m_pic_path!="")
{
if(pAnchor)
{
pAnchor->put_src(dlg.m_pic_path.AllocSysString());
pAnchor->put_align(dlg.m_stralign.AllocSysString());
pAnchor->Release();
}
else if(pTextAreaElement)
{
CString picpath;
picpath.Format("<img src=\"%s\" align=\"%s\">",dlg.m_pic_path,dlg.m_stralign);
pTextAreaElement->pasteHTML(picpath.AllocSysString());
pTextAreaElement->Release();
pSelection->Release();
}
}
pElement->Release();
}
pDoc->Release();
// 获取当前所在位置
else
{
BSTR value;
pDoc->get_selection(&pSelection);
IDispatch * pDisp;
if(SUCCEEDED(pSelection->createRange(&pDisp)))
{
pDisp->QueryInterface(IID_IHTMLTxtRange,(void**)&pTextAreaElement);
pDisp->Release();
}
}// 当前不是img就插入图片
else if(pTextAreaElement)
{
CString picpath;
picpath.Format("<img src=\"%s\" align=\"%s\">",dlg.m_pic_path,dlg.m_stralign);
pTextAreaElement->pasteHTML(picpath.AllocSysString());
pTextAreaElement->Release();
pSelection->Release();
}
Conquer what is not
http://www.codeguru.com/ieprogram/HtmlMemory.html