【问题标题】:TWebrowser Copy from Word DocumentTWebbrowser 从 Word 文档复制
【发布时间】:2016-06-28 02:52:07
【问题描述】:

我有一个 TWebBrowser 处于编辑模式,我正在尝试允许用户从 Word 文档(或任何地方)复制和粘贴文本和图像并粘贴到网络浏览器中

我已经能够使用以下代码获取要粘贴的文本:

pvaIn := EmptyParam;
HtmlEditor.ExecWB(OLECMDID_PASTE, OLECMDEXECOPT_DODEFAULT, pvaIn);

HtmlEditor 是我的 TWebBrowser 组件

我的问题是当尝试粘贴图像时,网络浏览器似乎知道我粘贴了图像,但它只显示一个可编辑的文本框。

有没有办法将图像粘贴到 TWebBrowser 中?

【问题讨论】:

  • Web (html) 页面不包含图像。它们包含指向单独存储在磁盘上的图像的链接。你会把它粘贴到什么地方?
  • @KenWhite 这是否意味着我不能使用默认的 OLECMDID_PASTE 命令?我想让用户能够将部分 Word 文档复制到浏览器中,然后使用 Indy 将其作为电子邮件发送
  • 您可以复制文本。不过,我不相信 OLECMDID_PASTE 会自动为您处理图像。它会将粘贴的图像放在哪里,因为它们必须在硬盘上的某个地方? (我可能是错的,这就是我不写答案的原因。)
  • @Mattlaza 使用 TWebBrowser 作为发送电子邮件的中间存储?这让你自己很难受。 Can't you use something simpler like TRichEdit?
  • @JanDoggen 使用遗留程序,尽量不完全重写它

标签: delphi mshtml delphi-10-seattle twebbrowser


【解决方案1】:

这里的解决方案是将位图保存到磁盘,然后创建一个图像html图像并将其附加到光标位置的HTML。

if clipboard.hasformat(cf_bitmap) then //only if the clipboard currently has a image
begin
    bmp := TBitMap.Create();
    CreateGuid(uid);
    try
        filename := 'cb(' + System.Copy(guidToString(uid), 2, 8) + ').bmp'; //generate a unique filename
        path := ExtractFilePath(paramstr(0)) + filename;//the location where we will save it
        bmp.LoadFromClipboardFormat(cf_bitmap, clipboard.GetAsHandle(cf_bitmap), 0);
        bmp.SaveToFile(path); //save the clipboard image to disk

        Doc2 := nil;
        Doc2 := self.HtmlEditor.Document as IHTMLDocument2;

        if Doc2 = nil then
            exit;

        if Assigned(Doc2.Body) then
        begin
            Image := Doc2.createElement('img') as IHtmlDOMNode; //create the img element
            (Image as IHTMLImgElement).src := path; //set this to the path of the image we just saved

            if GetcaretPos(cursor) then //get the element at the cursor position
            begin
                ElementAtCursor := Doc2.elementFromPoint(cursor.X, cursor.Y);
                Html := '<img src="' + path + '"></img>'; //insert the image after this element
                ElementAtCursor.insertAdjacentHTML('AfterBegin', Html);
            end
            else
                (Doc2.Body as IHtmlDOMNode).appendChild(Image); //else just append to the body
        end;
        finally
            bmp.free();
        end;
end;

如您所见,第一步是检查剪贴板是否有 CF_BITMAP,如果有,我们将其保存到磁盘。然后我们创建一个 img HTML 元素,将该文件名附加到 img 的 src。最后,我们将 img 添加到光标所在的 HTML 中,如果无法获取光标,则将其附加到 HTML 正文中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 1970-01-01
    相关资源
    最近更新 更多