gpt4 book ai didi

c# - 将 html 表格拖放到另一个应用程序中

转载 作者:太空狗 更新时间:2023-10-30 00:47:00 24 4
gpt4 key购买 nike

我在将 html 表格从我的 C# winforms 应用程序拖到外部应用程序(Outlook 电子邮件消息)中并使其呈现为表格而不是该表格的纯文本版本时遇到问题。我知道当你在剪贴板中复制/粘贴时,你必须将表格放在 CF_HTML format 中。但这似乎对拖 table 没有帮助。有谁知道我错过了什么?

最佳答案

ObjectListView支持将行从 ListView 复制和拖动到其他应用程序,包括文本和 HTML 版本。为此,它会执行如下操作:

DataObject dataObject = new DataObject();
this.CreateTextFormats(dataObject);
Clipboard.SetDataObject(dataObject);

要进行拖放操作,代码几乎是一样的:

DataObject dataObject = new DataObject();
this.CreateTextFormats(dataObject);
DragDropEffects effect = this.DoDragDrop(dataObject, DragDropEffects.All);

CreateTextFormats() 并不复杂:

public void CreateTextFormats(DataObject do) {
string textVersion;
string htmlVersion;
// Do the work of making the tab-separated text version and the HTML code
do.SetData(textVersion);
do.SetText(ConvertToHtmlFragment(htmlVersion), TextDataFormat.Html);
}

获得正确的 HTML 格式需要更长的时间:

/// <summary>
/// Convert the fragment of HTML into the Clipboards HTML format.
/// </summary>
/// <remarks>The HTML format is found here http://msdn2.microsoft.com/en-us/library/aa767917.aspx
/// </remarks>
/// <param name="fragment">The HTML to put onto the clipboard. It must be valid HTML!</param>
/// <returns>A string that can be put onto the clipboard and will be recognized as HTML</returns>
private string ConvertToHtmlFragment(string fragment) {
// Minimal implementation of HTML clipboard format
string source = "http://www.codeproject.com/KB/list/ObjectListView.aspx";

const String MARKER_BLOCK =
"Version:1.0\r\n" +
"StartHTML:{0,8}\r\n" +
"EndHTML:{1,8}\r\n" +
"StartFragment:{2,8}\r\n" +
"EndFragment:{3,8}\r\n" +
"StartSelection:{2,8}\r\n" +
"EndSelection:{3,8}\r\n" +
"SourceURL:{4}\r\n" +
"{5}";

int prefixLength = String.Format(MARKER_BLOCK, 0, 0, 0, 0, source, "").Length;

const String DEFAULT_HTML_BODY =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">" +
"<HTML><HEAD></HEAD><BODY><!--StartFragment-->{0}<!--EndFragment--></BODY></HTML>";

string html = String.Format(DEFAULT_HTML_BODY, fragment);
int startFragment = prefixLength + html.IndexOf(fragment);
int endFragment = startFragment + fragment.Length;

return String.Format(MARKER_BLOCK, prefixLength, prefixLength + html.Length, startFragment, endFragment, source, html);
}

关于c# - 将 html 表格拖放到另一个应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1698794/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com