PRB: Metafiles on Clipboard Are Not Visible to All Applications

This article refers to the following Microsoft .NET Framework Class Library namespaces:
  • System.Drawing.Imaging
  • System.Runtime.InteropServices

Symptoms

Metafiles that .NET applications add to the Clipboard are not visible to other applications.

Cause

This problem occurs because the .NET Framework uses a new Clipboard format when it adds metafiles to the Clipboard. Other applications, such as Microsoft Word, or the operating system are not aware of this new format and, therefore, cannot paste or display the image.

Resolution

To add a metafile to the Clipboard so that it is visible to other applications, you must use the CF_ENHMETAFILE format. Support for this format is provided through the DataObject class with the DataFormats.EnhancedMetafile method. Unfortunately, limitations in the current implementation of the .NET Framework prevent this from being a viable solution. Therefore, you must gain interoperability with Win32 Clipboard application programming interfaces (APIs) to resolve this problem.

The code samples that follow demonstrate how to add a metafile to the Clipboard so that it is visible to other applications.

Microsoft Visual Basic .NET Sample

Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices

Public Class ClipboardMetafileHelper
<DllImport("user32.dll", EntryPoint:="OpenClipboard", _
SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function OpenClipboard(ByVal hWnd As IntPtr) As Boolean
End Function
<DllImport("user32.dll", EntryPoint:="EmptyClipboard", _
SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function EmptyClipboard() As Boolean
End Function
<DllImport("user32.dll", EntryPoint:="SetClipboardData", _
SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function SetClipboardData(ByVal uFormat As Integer, ByVal hWnd As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="CloseClipboard", _
SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function CloseClipboard() As Boolean
End Function
<DllImport("gdi32.dll", EntryPoint:="CopyEnhMetaFileA", _
SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function CopyEnhMetaFile(ByVal hemfSrc As IntPtr, ByVal hNULL As IntPtr) As IntPtr
End Function
<DllImport("gdi32.dll", EntryPoint:="DeleteEnhMetaFile", _
SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function DeleteEnhMetaFile(ByVal hemfSrc As IntPtr) As Boolean
End Function

' Metafile mf is set to a state that is not valid inside this function.
Public Shared Function PutEnhMetafileOnClipboard(ByVal hWnd As IntPtr, ByVal mf As Metafile) As Boolean
Dim bResult As New Boolean()
bResult = False
Dim hEMF, hEMF2 As IntPtr
hEMF = mf.GetHenhmetafile() ' invalidates mf
If Not hEMF.Equals(New IntPtr(0)) Then
hEMF2 = CopyEnhMetaFile(hEMF, New IntPtr(0))
If Not hEMF2.Equals(New IntPtr(0)) Then
If OpenClipboard(hWnd) Then
If EmptyClipboard() Then
Dim hRes As IntPtr
hRes = SetClipboardData(14, hEMF2) ' 14 == CF_ENHMETAFILE
bResult = hRes.Equals(hEMF2)
CloseClipboard()
End If
End If
End If
DeleteEnhMetaFile(hEMF)
End If
Return bResult
End Function

End Class


'You can call this function with code that is similar to the following code:
Dim mf As New Metafile("filename.emf")
ClipboardMetafileHelper.PutEnhMetafileOnClipboard(me.Handle,mf)

Microsoft Visual C# .NET Sample

using System.Drawing.Imaging;
using System.Runtime.InteropServices;

public class ClipboardMetafileHelper
{
[DllImport("user32.dll")]
static extern bool OpenClipboard(IntPtr hWndNewOwner);
[DllImport("user32.dll")]
static extern bool EmptyClipboard();
[DllImport("user32.dll")]
static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem);
[DllImport("user32.dll")]
static extern bool CloseClipboard();
[DllImport("gdi32.dll")]
static extern IntPtr CopyEnhMetaFile(IntPtr hemfSrc, IntPtr hNULL);
[DllImport("gdi32.dll")]
static extern bool DeleteEnhMetaFile(IntPtr hemf);

// Metafile mf is set to a state that is not valid inside this function.
static public bool PutEnhMetafileOnClipboard( IntPtr hWnd, Metafile mf )
{
bool bResult = false;
IntPtr hEMF, hEMF2;
hEMF = mf.GetHenhmetafile(); // invalidates mf
if( ! hEMF.Equals( new IntPtr(0) ) )
{
hEMF2 = CopyEnhMetaFile( hEMF, new IntPtr(0) );
if( ! hEMF2.Equals( new IntPtr(0) ) )
{
if( OpenClipboard( hWnd ) )
{
if( EmptyClipboard() )
{
IntPtr hRes = SetClipboardData( 14 /*CF_ENHMETAFILE*/, hEMF2 );
bResult = hRes.Equals( hEMF2 );
CloseClipboard();
}
}
}
DeleteEnhMetaFile( hEMF );
}
return bResult;
}
}

//You can call this function with code that is similar to the following code:
Metafile mf = new Metafile( "filename.emf" );
ClipboardMetafileHelper.PutEnhMetafileOnClipboard(this.Handle, mf );
Propriedades

ID do Artigo: 323530 - Última Revisão: 8 de mai de 2006 - Revisão: 1

Comentários