// 我想用自己的插入图片对话框代替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();

解决方案 »

  1.   

    从你的代码来看,当前位置的元素已经是Img了你才执行你的代码?一般来说,插入图片的位置的元素不一定是Img的
      

  2.   

    if(SUCCEEDED(pDoc->elementFromPoint(point.x,point.y,&pElement))) // 获取当前的element
    // 获取当前所在位置
    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();
    }
      

  3.   

    用IMarkupService来insert element吧,
      

  4.   

    Implementing Edit Designers 2: The Annotator Sample--------------------------------------------------------------------------------This topic and its accompanying sample explain how to implement an edit designer that enables users to annotate a Web page. This topic also shows how to create a Web page with an editable region and attach the annotation component to it.Command what is yours
    Conquer what is not
      

  5.   

    查了半天MSDN,不知道IMarkupService怎么用?
      

  6.   

    www.csdn.net/develop/article/13/13462.shtm
    http://www.codeguru.com/ieprogram/HtmlMemory.html