如果我将选定的网页从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桌面应用程序?
答案 0 :(得分:1)
这是微软特有的,不要指望在其他操作系统中看到相同的信息 请注意,从IE复制片段时,您将获得相同的格式。
我不确定“网页到网页拖放操作”是什么意思。要放在哪里? textarea?
我不知道C#,但在C / C ++应用程序中,您必须使用OpenClipboard和相关的Win32 API来获取此信息。特别是,GetClipboardData(CF_HTML)允许您获取此信息(例如,也可以以纯文本形式获取,或者使用Unicode数据等)。
答案 1 :(得分:1)
WinForms应用可以使用Clipboard.GetText来获取它。但是,我认为没有办法在网页中进行此操作。
答案 2 :(得分:1)
为完整起见,这是获取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;
}
}