Clipboard.SetText(HTML)不再起作用 [英] Clipboard.SetText(HTML) no longer work

查看:71
本文介绍了Clipboard.SetText(HTML)不再起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码,该代码在HTML标签之间添加了某些生成的字符串,并使用HTML数据设置了剪贴板.如果我还没疯,那么我可以肯定这段代码对我有用,那么就不会对其进行任何更改,但是它将不再起作用!

I had a code which was adding certain generated string between HTML Tags and setting up the clipboard with HTML data. If I am not insane yet I am certain this code worked for me before, no changes have been made to it, but it doesn't work anymore!

我找到了 Clipboard HTML 的文档,并以其为例,在下面进行了测试,这对我也不起作用.

I found the documentation of Clipboard HTML and took the example, made the test below and it does't work for me either.

 Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

    Dim meh As String = _
            "Version:0.9" & vbCrLf & _
            "StartHTML:71" & vbCrLf & _
            "EndHTML:160" & vbCrLf & _
            "StartFragment:130" & vbCrLf & _
            "EndFragment:150" & vbCrLf & _
            "StartSelection:130" & vbCrLf & _
            "EndSelection:150" & vbCrLf & _
            "<!DOCTYPE html>" & vbCrLf & _
            "<HTML> " & vbCrLf & _
            "<BODY> " & vbCrLf & _
            "<!--StartFragment-->" & vbCrLf & _
            "<B>bold.</B> <I><B>This is bold italic.</B></I> <I>This</I> " & vbCrLf & _
            "<!--EndFragment--> " & vbCrLf & _
            "</BODY> " & vbCrLf & _
            "</HTML>"
    Clipboard.SetText(meh, TextDataFormat.Html)

End Sub

我也只是做了这样的事情:-

I also did simply something like this:-

Clipboard.SetText(TextBox1.text, TextDataFormat.Html)

然后,我将带有仔细计数的字节的其他字符串粘贴到StartHTML等上.

Then I was pasting different strings with carefully counted bytes for StartHTML, etc...

Version:0.9
StartHTML:71
EndHTML:178
StartFragment:71
EndFragment:178
<!DOCTYPE html>
<HTML> 
<BODY> 
<B>bold.</B> <I><B>This is bold italic.</B></I> <I>This</I> 
</BODY> 
</HTML>

我能够在Windows 10和Windows 7的VB2010,VB2017上重现此意外行为

I was able to reproduce this unexpected behavior on VB2010, VB2017 on Windows 10 and Windows 7

值得补充的是,我正在使用 Clipdiary 应用程序,该应用程序在运行上述代码后会返回诸如无法进行描述和标题"之类的内容

Worth to add that I am using Clipdiary application which after running the code above returns something like "Can't make description and title"

我想知道有人可以运行这段代码,看看我做错了什么,因为我在这里被要求了;我也在某处找到了此代码:-

I wonder could someone run this code and see what I am doing wrong because I get demanted here; I also found this code somewhere:-

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    PasteLink("http://www.experts-exchange.com/M_1539809.html", "Idle_Mind's Profile")
End Sub

Public Sub PasteLink(ByVal link As String, ByVal description As String)
    Const sContextStart As String = "<HTML><BODY><!--StartFragment -->"
    Const sContextEnd As String = "<!--EndFragment --></BODY></HTML>"
    Const m_sDescription As String = _
        "Version:1.0" & vbCrLf & _
        "StartHTML:aaaaaaaaaa" & vbCrLf & _
        "EndHTML:bbbbbbbbbb" & vbCrLf & _
        "StartFragment:cccccccccc" & vbCrLf & _
        "EndFragment:dddddddddd" & vbCrLf

    Dim sHtmlFragment As String = _
        "<A HREF=" & Chr(34) & link & Chr(34) & ">" _
        & description & "</A>"

    Dim sData As String = m_sDescription & sContextStart & sHtmlFragment & sContextEnd
    sData = sData.Replace("aaaaaaaaaa", m_sDescription.Length.ToString.PadLeft(10, "0"))
    sData = sData.Replace("bbbbbbbbbb", sData.Length.ToString.PadLeft(10, "0"))
    sData = sData.Replace("cccccccccc", (m_sDescription & sContextStart).Length.ToString.PadLeft(10, "0"))
    sData = sData.Replace("dddddddddd", (m_sDescription & sContextStart & sHtmlFragment).Length.ToString.PadLeft(10, "0"))

    Clipboard.SetDataObject(New DataObject(DataFormats.Html, sData))
End Sub

这也不起作用,基本上,这里的剪贴板设置为 DataObject ,但它不起作用.

Which didn't work either, basically here Clipboard is set as DataObject, yet it does not work.

说明

在伙计们阐明了不同的看法之后,我意识到对这个问题有不同的解释.

I realized after the the guys shed different light on it that there is a different explanation of this issue.

我的应用程序正在生成一个签名,然后将其放置到剪贴板中以将其粘贴到GMAIL.

My app was generating a Signature which was then placed into clipboard for pasting it to GMAIL.

如果我使用原始代码,将会发生以下情况:

If I use my original code here is what happens:

  1. 我尝试在Gmail的签名"部分粘贴-没有粘贴

  1. I try to paste in Signature section on Gmail - nothing pastes

我将其粘贴到内部或MSWORD中-粘贴确定

I paste this inside or MSWORD - pastes OK

然后我可以将其粘贴到Gmail中.

Then I can paste this into Gmail.

因此,必须在Gmail上的此窗口中进行某些更改,以使其不再起作用,并且由于整个剪贴板.settext模块在某种程度上无法正常工作,因此引起了整个问题的误解,使我发疯.

So something must have changed to this window on Gmail so it doesn't work anymore and since this whole clipboard.settext module is somewhat not working properly it generated the whole misunderstanding of the issue driving me crazy.

而且看起来Derek提供的代码即使在第一次使用时也可以在设置内的Gmail签名窗口上运行,然后我将需要重新设计功能.

Also it looks that the code provided by Derek works on that Gmail Signature windows inside settings even in first time then I will need to redesign my function.

推荐答案

您可以查看它是否适合您.我经常使用它.

You can see if this works for you. I use this pretty regularly.

它看起来与您的非常相似,它是c#,但是您可以使用在线代码转换器.

It looks very similar to yours though, and it is is c# but you can use an online code translator.

   public void SetHyperlinkOnClipboard( string link, string description )
   {
      try
      {
         const string sContextStart = "<HTML><BODY><!--StartFragment -->";
         const string sContextEnd = "<!--EndFragment --></BODY></HTML>";
         const string m_sDescription = "Version:0.9" + Constants.vbCrLf + "StartHTML:aaaaaaaaaa" + Constants.vbCrLf + "EndHTML:bbbbbbbbbb" + Constants.vbCrLf + "StartFragment:cccccccccc" + Constants.vbCrLf + "EndFragment:dddddddddd" + Constants.vbCrLf;

         string sHtmlFragment = "<A HREF=" + Strings.Chr( 34 ) + link + Strings.Chr( 34 ) + ">" + description + "</A>";

         string sData = m_sDescription + sContextStart + sHtmlFragment + sContextEnd;
         sData = sData.Replace( "aaaaaaaaaa", m_sDescription.Length.ToString().PadLeft( 10, '0' ) );
         sData = sData.Replace( "bbbbbbbbbb", sData.Length.ToString().PadLeft( 10, '0' ) );
         sData = sData.Replace( "cccccccccc", ( m_sDescription + sContextStart ).Length.ToString().PadLeft( 10, '0' ) );
         sData = sData.Replace( "dddddddddd", ( m_sDescription + sContextStart + sHtmlFragment ).Length.ToString().PadLeft( 10, '0' ) );
         Clipboard.SetDataObject( new DataObject( DataFormats.Html, sData ), true );    
      }
      catch( Exception ex )
      {

      }
   }

这篇关于Clipboard.SetText(HTML)不再起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆