drag-and-drop - 如何在网页或 C# 中读取原始 (CF_HTML) 剪贴板数据?

标签 drag-and-drop clipboard copy-paste

如果我将所选网页从 Firefox 拖放到 HTML-Kit,HTML-Kit 会询问我是要粘贴为文本还是 HTML。如果我选择“文本”,我会得到这个:

Version:0.9
StartHTML:00000147
EndHTML:00000516
StartFragment:00000181
EndFragment:00000480
SourceURL:http://en.wikipedia.org/wiki/Herodotus
<html><body>
<!--StartFragment-->Additional details have been garnered from the <i><a href="http://en.wikipedia.org/wiki/Suda" title="Suda">Suda</a></i>, an 11th-century encyclopaedia of the <a href="http://en.wikipedia.org/wiki/Byzantium" title="Byzantium">Byzantium</a> which likely took its information from traditional accounts.<!--EndFragment-->
</body>
</html>

According to MSDN ,这是“CF_HTML”格式的剪贴板数据。在 OS X 和 Linux 系统上是否一样?

有没有什么方法可以在网页到网页的拖放操作中访问这种详细信息(而不只是简单的剪辑片段)? C# WinForms 桌面应用程序怎么样?

最佳答案

为了完整起见,这里是获取 RAW 剪贴板数据的 P/Invoke Win32 API 代码

using System;
using System.Runtime.InteropServices;
using System.Text;

//--------------------------------------------------------------------------------
http://metadataconsulting.blogspot.com/2019/06/How-to-get-HTML-from-the-Windows-system-clipboard-directly-using-PInvoke-Win32-Native-methods-avoiding-bad-funny-characters.html
//--------------------------------------------------------------------------------

public class ClipboardHelper
{
    #region Win32 Native PInvoke

    [DllImport("User32.dll", SetLastError = true)]
    private static extern uint RegisterClipboardFormat(string lpszFormat);
    //or specifically - private static extern uint RegisterClipboardFormatA(string lpszFormat);

    [DllImport("User32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool IsClipboardFormatAvailable(uint format);

    [DllImport("User32.dll", SetLastError = true)]
    private static extern IntPtr GetClipboardData(uint uFormat);

    [DllImport("User32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool OpenClipboard(IntPtr hWndNewOwner);

    [DllImport("User32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool CloseClipboard();

    [DllImport("Kernel32.dll", SetLastError = true)]
    private static extern IntPtr GlobalLock(IntPtr hMem);

    [DllImport("Kernel32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool GlobalUnlock(IntPtr hMem);

    [DllImport("Kernel32.dll", SetLastError = true)]
    private static extern int GlobalSize(IntPtr hMem);

    #endregion

    public static string GetHTMLWin32Native()
    {

        string strHTMLUTF8 = string.Empty; 
        uint CF_HTML = RegisterClipboardFormatA("HTML Format");
        if (CF_HTML != null || CF_HTML == 0)
                return null;

        if (!IsClipboardFormatAvailable(CF_HTML))
            return null;

        try
        {
            if (!OpenClipboard(IntPtr.Zero))
                return null;

            IntPtr handle = GetClipboardData(CF_HTML);
            if (handle == IntPtr.Zero)
                return null;

            IntPtr pointer = IntPtr.Zero;

            try
            {
                pointer = GlobalLock(handle);
                if (pointer == IntPtr.Zero)
                    return null;

                uint size = GlobalSize(handle);
                byte[] buff = new byte[size];

                Marshal.Copy(pointer, buff, 0, (int)size);

            strHTMLUTF8 = System.Text.Encoding.UTF8.GetString(buff);
            }
            finally
            {
                if (pointer != IntPtr.Zero)
                    GlobalUnlock(handle);
            }
        }
        finally
        {
            CloseClipboard();
        }

        return strHTMLUTF8; 
    }
}

关于drag-and-drop - 如何在网页或 C# 中读取原始 (CF_HTML) 剪贴板数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/141630/

相关文章:

javascript - 利用dom to image将图像复制到剪贴板

javascript - YUI 自动完成 : search after pasting?

excel - 在 Excel 工作表中搜索文本并将单元格范围提取到表格中

javascript - 类似 iPhoneX 通过拖动显示/隐藏菜单/容器

wpf - 需要相当于 SystemInformation.DragSize 的 Wpf

c# - 图像拖放(WPF 窗口)

emacs - 如何在emacs helm-mode中复制文件的路径

drag-and-drop - 拖放到文件输入在 IE11 中不起作用

emacs 在 nowindow 模式下将 kill-ring 复制到系统剪贴板

java/Swing : clipboard paste