.NET的Clipboard.GetData(DataFormats.Html)方法有BUG。
网上查询了一下,有个解决方法,不过那个网站被封了(至少我这没法访问)
用某软件上去看了一下,照做,OK了
using System;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
namespace JX
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// By JX
/// Version 1.0.200511041900
/// This static class contains a method to extract Dataformat.Html data from a IDataObject
/// obtained from the Clipboard or a Drag and Drop operation.
/// The method overcomes the UTF8 corruption problem encountered in .NET Framework version 2.0.50727.42
/// Version 1.0 of the Dataformat.Html data has a header like this:
///
/// Version:1.0
/// StartHTML:000000238
/// EndHTML:000000546
/// StartFragment:000000353
/// EndFragment:000000510
/// StartSelection:000000353
/// EndSelection:000000510
///
/// The values refer to the BYTES of the string not the actual chars
/// </summary>
static class HtmlFromIDataObject
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Extracts data of type Dataformat.Html from an IdataObject data container
/// This method shouldn't throw any exception but writes relevant exception informations in the debug window
/// </summary>
/// <param name="data">IdataObject data container</param>
/// <returns>A byte[] array with the decoded string or null if the method fails</returns>
public static byte[] GetHtml(System.Windows.Forms.IDataObject data)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Runtime.InteropServices.ComTypes.IDataObject interopData = data as System.Runtime.InteropServices.ComTypes.IDataObject;
![](/Images/OutliningIndicators/InBlock.gif)
FORMATETC format = new FORMATETC();
format.cfFormat = (short)DataFormats.GetFormat(DataFormats.Html).Id;
format.dwAspect = DVASPECT.DVASPECT_CONTENT;
format.lindex = -1;
format.tymed = TYMED.TYMED_HGLOBAL;
![](/Images/OutliningIndicators/InBlock.gif)
STGMEDIUM stgmedium = new STGMEDIUM();
stgmedium.tymed = TYMED.TYMED_HGLOBAL;
stgmedium.pUnkForRelease = null;
![](/Images/OutliningIndicators/InBlock.gif)
int queryResult = 0;
![](/Images/OutliningIndicators/InBlock.gif)
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
queryResult = interopData.QueryGetData(ref format);
}
catch (Exception exp)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Debug.WriteLine("HtmlFromIDataObject.GetHtml -> QueryGetData(ref format) threw an exception: "
+ Environment.NewLine + exp.ToString());
return null;
}
![](/Images/OutliningIndicators/InBlock.gif)
if (queryResult != 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Debug.WriteLine("HtmlFromIDataObject.GetHtml -> QueryGetData(ref format) returned a code != 0 code: "
+ queryResult.ToString());
return null;
}
![](/Images/OutliningIndicators/InBlock.gif)
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
interopData.GetData(ref format, out stgmedium);
}
catch (Exception exp)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Diagnostics.Debug.WriteLine("HtmlFromIDataObject.GetHtml -> GetData(ref format, out stgmedium) threw this exception: "
+ Environment.NewLine + exp.ToString());
return null;
}
![](/Images/OutliningIndicators/InBlock.gif)
if (stgmedium.unionmember == IntPtr.Zero)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Debug.WriteLine("HtmlFromIDataObject.GetHtml -> stgmedium.unionmember returned an IntPtr pointing to zero");
return null;
}
![](/Images/OutliningIndicators/InBlock.gif)
IntPtr pointer = stgmedium.unionmember;
HandleRef handleRef = new HandleRef(null, pointer);
![](/Images/OutliningIndicators/InBlock.gif)
byte[] rawArray = null;
![](/Images/OutliningIndicators/InBlock.gif)
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
IntPtr ptr1 = GlobalLock(handleRef);
![](/Images/OutliningIndicators/InBlock.gif)
int length = GlobalSize(handleRef);
![](/Images/OutliningIndicators/InBlock.gif)
rawArray = new byte[length];
![](/Images/OutliningIndicators/InBlock.gif)
Marshal.Copy(ptr1, rawArray, 0, length);
![](/Images/OutliningIndicators/InBlock.gif)
}
catch (Exception exp)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Debug.WriteLine("HtmlFromIDataObject.GetHtml -> Html Import threw an exception: " + Environment.NewLine + exp.ToString());
}
finally
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
GlobalUnlock(handleRef);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
return rawArray;
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
[DllImport("kernel32.dll", CharSet = CharSet.Auto, ExactSpelling = true, SetLastError = true)]
private static extern IntPtr GlobalLock(HandleRef handle);
![](/Images/OutliningIndicators/InBlock.gif)
[DllImport("kernel32.dll", CharSet = CharSet.Auto, ExactSpelling = true, SetLastError = true)]
private static extern bool GlobalUnlock(HandleRef handle);
![](/Images/OutliningIndicators/InBlock.gif)
[DllImport("kernel32.dll", CharSet = CharSet.Auto, ExactSpelling = true, SetLastError = true)]
private static extern int GlobalSize(HandleRef handle);
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
byte[] rawHtmlBytes = JX.HtmlFromIDataObject.GetHtml(dataObject)
string rawHtml = Encoding.UTF8.GetString(rawHtmlBytes);
原文地址:http://bakamachine.blogspot.com/2006/05/workarond-for-dataobject-html.html