Microsoft KB Archive/323530

= PRB: Metafiles on Clipboard Are Not Visible to All Applications =

Article ID: 323530

Article Last Modified on 5/8/2006

-

APPLIES TO


 * Microsoft .NET Framework Software Development Kit 1.0 Service Pack 2
 * Microsoft .NET Framework Software Development Kit 1.0 Service Pack 2
 * Microsoft Windows XP Professional
 * Microsoft Windows XP Professional for Itanium-based systems

-



This article was previously published under Q323530



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  _ Public Shared Function OpenClipboard(ByVal hWnd As IntPtr) As Boolean End Function  _ Public Shared Function EmptyClipboard As Boolean End Function  _ Public Shared Function SetClipboardData(ByVal uFormat As Integer, ByVal hWnd As IntPtr) As IntPtr End Function  _ Public Shared Function CloseClipboard As Boolean End Function  _ Public Shared Function CopyEnhMetaFile(ByVal hemfSrc As IntPtr, ByVal hNULL As IntPtr) As IntPtr End Function  _ 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(&quot;filename.emf&quot;) ClipboardMetafileHelper.PutEnhMetafileOnClipboard(me.Handle,mf)

Microsoft Visual C# .NET Sample
using System.Drawing.Imaging; using System.Runtime.InteropServices;

public class ClipboardMetafileHelper {   [DllImport(&quot;user32.dll&quot;)] static extern bool OpenClipboard(IntPtr hWndNewOwner); [DllImport(&quot;user32.dll&quot;)] static extern bool EmptyClipboard; [DllImport(&quot;user32.dll&quot;)] static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem); [DllImport(&quot;user32.dll&quot;)] static extern bool CloseClipboard; [DllImport(&quot;gdi32.dll&quot;)] static extern IntPtr CopyEnhMetaFile(IntPtr hemfSrc, IntPtr hNULL); [DllImport(&quot;gdi32.dll&quot;)] 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( &quot;filename.emf&quot; ); ClipboardMetafileHelper.PutEnhMetafileOnClipboard(this.Handle, mf );

Additional query words: unavailable cannot access see not available enhanced emf wmf

Keywords: kbprb kbdswgdi2003swept KB323530

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.