Getting CF_ENHMETAFILE from clipboard

A

Andrey Dzizenko

Hi all!

Did anyone try the subj?

I use extern IsClipboardFormatAvailable and GetClipboardData from
user32.dll. IsClipboardFormatAvailable(14) returns true, but
GetClipboardData(14) returns null-pointer after that.

I found working source codes that can get CF_ENHMETAFILE from clipboard,
but they are C++ WinAPI applications. Maybe there's some ruse, but I can't
see the difference in method using.

Thank you in advance for any advices.

Best regards,
A. Dzizenko
 
N

Nicholas Paldino [.NET/C# MVP]

Andrey,

Why not just use the GetData method on the IDataObject implementation
passed to you? You can pass DataFormats.EnhancedMetafile to get what you
want.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hi all!

Did anyone try the subj?

I use extern IsClipboardFormatAvailable and GetClipboardData from
user32.dll. IsClipboardFormatAvailable(14) returns true, but
GetClipboardData(14) returns null-pointer after that.

I found working source codes that can get CF_ENHMETAFILE from clipboard,
but they are C++ WinAPI applications. Maybe there's some ruse, but I can't
see the difference in method using.

Thank you in advance for any advices.

Best regards,
A. Dzizenko
 
M

Michael Phillips, Jr.

In c# you may use P-Invoke, with the following calls:
1) OpenClipboard
2) IsClipboardFormatAvailable(CF_ENHMETAFILE)
3) If true, then use GetClipboardData(CF_ENHMETAFILE) to get an IntPtr to
the EnhancedMetafile handle
4) Use CopyEnhMetaFile to make a copy for your use as the clipboard retains
ownership of the handle.
5) CloseClipboard

Make sure you use the correct P-Invoke signatures. Copy them from
http://www.pinvoke.net


Hi all!

Did anyone try the subj?

I use extern IsClipboardFormatAvailable and GetClipboardData from
user32.dll. IsClipboardFormatAvailable(14) returns true, but
GetClipboardData(14) returns null-pointer after that.

I found working source codes that can get CF_ENHMETAFILE from clipboard,
but they are C++ WinAPI applications. Maybe there's some ruse, but I can't
see the difference in method using.

Thank you in advance for any advices.

Best regards,
A. Dzizenko
 
A

Andrey Dzizenko

Oh... Sorry, I forgot to mention that...

Clipboard.ContainsData(DataFormats.EnhancedMetafile) returns false. And
all other formats too.
..NET Clipboard can't see the data (it's MSWord's picture). Explained at
http://support.microsoft.com/?id=323530

Best regards,
A. Dzizenko.
 
M

Michael Phillips, Jr.

If you enumerate all clipboard formats in c# using the GetFormats methods,
you will see that the clipboard contains the "EnhancedMetafile" custom
clipboard format which cannot be retrieved. However the canonical
CF_ENHMETAFILE clipboard format can be retrieved using the calls in my post
provided that GetFormats presents the "EnhancedMetafile" format as an
available clipboard format.

If you use GetFormats, you will see that Office also places the
"MetaFilePict" custom .Net format on the clipboard. You may retrieve this
clipboard format using the same p-invoke code with CF_METAFILEPICT
substituted for CF_ENHMETAFILE.

The clipboard will synthesize the missing format for you if the application
places only one of them on the clipboard(i.e., "EnhancedMetafile",
"MetafilePict").


Oh... Sorry, I forgot to mention that...

Clipboard.ContainsData(DataFormats.EnhancedMetafile) returns false. And
all other formats too.
..NET Clipboard can't see the data (it's MSWord's picture). Explained at
http://support.microsoft.com/?id=323530

Best regards,
A. Dzizenko.
 
A

Andrey Dzizenko

If I understand you correctly...

<<Immediate window>>
selection.CopyAsPicture()
Expression has been evaluated and has no value
Clipboard.ContainsData(DataFormats.EnhancedMetafile)
false

Best regards,
A. Dzizenko
 
M

Michael Phillips, Jr.

To be clear, "EnhancedMetafile" is a custom clipboard format that the .Net
clr registered with the function RegisterClipboardFormat.

It contains no data because Office does not know how to present the custom
clipboard format.

However, the clipboard knows how to synthesize the 17 canonical clipboard
formats. If you ask for CF_ENHMETAFILE and the clipboard is able to make
this format available to you, then you can retrieve it.
 
A

Andrey Dzizenko

It contains no data because Office does not know how to present the
custom
clipboard format.

But it contains data. As I said the method
IsClipboardFormatAvailable(CF_ENHMETAFILE) of the user32.dll library
returns true. So Office knows how to present the format.
http://www.codeguru.com/cpp/w-p/clipboard/externallinks/article.php/c9155/
There's source code of the clipboard viewer that can view CF_ENHMETAFILE
copied by Office. With no intricate movements.

It's possible. We don't know how - that's the question.
 
M

Michael Phillips, Jr.

I wrote my own clipboard viewer in c# to experiment with all the new .Net
custom clipboard formats as well as all of the canonical clipboard formats.

I retrieve CF_ENHMETAFILE using the method that I posted. I tested my
clipboard viewer with Office. It works!

It contains no data because Office does not know how to present the
custom
clipboard format.

But it contains data. As I said the method
IsClipboardFormatAvailable(CF_ENHMETAFILE) of the user32.dll library
returns true. So Office knows how to present the format.
http://www.codeguru.com/cpp/w-p/clipboard/externallinks/article.php/c9155/
There's source code of the clipboard viewer that can view CF_ENHMETAFILE
copied by Office. With no intricate movements.

It's possible. We don't know how - that's the question.
 
A

Andrey Dzizenko

If not inconvenient to you would you please send your code to
(e-mail address removed)
Maybe I'm just stupid a little :)
 
M

Michael Phillips, Jr.

For the benefit of others, here is a code snippet:

// Must use a windows handle...I used the form's window handle
if ( true == OpenClipboard(this.Handle) )
{
if ( true ==
IsClipboardFormatAvailable((uint)CLIPFORMAT.CF_ENHMETAFILE) )
{
IntPtr hEmfClp = GetClipboardData((uint)CLIPFORMAT.CF_ENHMETAFILE);

if ( IntPtr.Zero != hEmfClp )
{
// Must make a copy. Clipboard retains ownership of the
original handle
IntPtr hEmfCopy = CopyEnhMetaFile(hEmfClp, null);

if ( IntPtr.Zero != hEmfCopy )
{
Metafile metafile = new Metafile(hEmfCopy, true);

// do something with this metafile
...

}
}
}

CloseClipboard();
}
 
A

Andrey Dzizenko

I understood.

In my project there's a class, which works in another thread. Don't know
why but the Clipboard is unavailable in this thread.
I found an information that the thread must be STA for using clipboard. My
application is STA and I don't know what to do any more.

I wrote the delegate method which gets clipboard from the main thread.
This scheme works.

Best regards,
A. Dzizenko.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top