Created
July 4, 2024 07:36
-
-
Save Benshi/47ec37045630c944b1d1a329c5888c00 to your computer and use it in GitHub Desktop.
[VB]ハイパーリンクをクリップボードにコピー
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
上記は CF_HTML 形式のクリップボードデータを作成するサンプルですが、その対象は
.NET Framework 4.5
以降 および.NET Core 3.0
以降に限られます。このコードを
.NET Framework 3.5
や.NET Framework 4
で実行した場合は、Microsoft Word 等に貼り付けた時に漢字等が文字化けします。