Skip to content

Instantly share code, notes, and snippets.

@Benshi
Created July 4, 2024 07:36
Show Gist options
  • Save Benshi/47ec37045630c944b1d1a329c5888c00 to your computer and use it in GitHub Desktop.
Save Benshi/47ec37045630c944b1d1a329c5888c00 to your computer and use it in GitHub Desktop.
[VB]ハイパーリンクをクリップボードにコピー
Public Class Form1
  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ClipboardSetHyperLink(New Uri("http://bbs.wankuma.com/"), "C# と VB.NET の質問掲示板")
  End Sub
  Private Sub ClipboardSetHyperLink(url As Uri, text As String, Optional style As String = Nothing)
    Dim link = <a href=<%= url.ToString() %> style=<%= style %>><%= text %></a>
    Dim htmlLink = link.ToString()
    Dim contentsLength = New System.Text.UTF8Encoding(False).GetByteCount(htmlLink)
    Dim sb As New System.Text.StringBuilder()
    sb.AppendLine("Version:0.9")
    sb.AppendLine("StartHTML:0000000101")
    sb.AppendLine("EndHTML:" & (175 + contentsLength).ToString("D8"))
    sb.AppendLine("StartFragment:0000000117")
    sb.AppendLine("EndFragment:" & (141 + contentsLength).ToString("D8"))
    sb.AppendLine("<html>")
    sb.AppendLine("<body>")
    sb.AppendLine("<!--StartFragment-->")
    sb.AppendLine(htmlLink)
    sb.AppendLine("<!--EndFragment-->")
    sb.Append("</body></html>")
    Clipboard.Clear()
    Clipboard.SetData(DataFormats.Html, sb.ToString() & vbNullChar)
  End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment