|
Not able to compile in Visual Studio c . Compilation Errors
|
|
|
|
|
Hi and thank you for your great and helpful article!
If the clipboard contains BITMAP, GetClipboardFormatName deos not fill data.m_szFormatName and also HANDLE hMem = ::GetClipboardData( format ); Makes hMem=0
This happens only to BITMAPs and works perfect on nther formats I checked.
I need to have bitmaps saved too. I'd be really greatfull if you help me out with this.
|
|
|
|
|
HBITMAP and HMETAFILE may have similar problems.
In fact, if we saved CF_DIB, we need not save CF_BITMAP. The system will transform CF_DIB into CF_BITMAP.
I have made a small change on 'CClipboardBackup' and I have used it in another project:
http://www.codeproject.com/com/wordhighlight.asp[^]
Take another try on the 'CClipboardBackup' in 'Wordhighlight' project.
|
|
|
|
|
hi,
first thanks for this class.
but i have a problem in combination with outlook and only i a special case.
if you copy an attachment of a mail in the clipboard and paste it in a new email, it doesn't work if your backup and restore function is called between the copy and paste. ( it also not work if you paste the attachment somewhere else... )
perhaps anyone has an idear?
thanks
Thomas R.
|
|
|
|
|
|
Hello again,
your Word-Addin has the same bug but you use the backup and restore of the clipboard very seldom. so the bug only apears if your toolbar is created at the first time.
I will look if i can find a solution.
Thanks for your help.
|
|
|
|
|
Maybe, when outlook is copying file between email and new email, it used 'provides data upon request'.
When 'SetClipboardData', if the data is 'NULL', it indicates that the window provides data in the specified clipboard format (renders the format) upon request. When 'paste', the owner window of the clipboard will be informed to provide data.
I have not found a way to backup the whole clipboard status completely. If the clipboard data is backuped and restored once, the owner window of the clipboard will be changed.
Maybe, this is the reason. I'm not so sure.
|
|
|
|
|
Maybe, this is the reason. I'm not so sure.
|
|
|
|
|
GOOD Idea!
|
|
|
|
|
Thank You!
|
|
|
|
|
Nice class thanks.
I was looking at the problem and thinking gee this should be easy to do. But when I realized all the different formats that can be copied I was stuck asking myself "how should I do this?" and "this is harder than I thought". So thanks for the help.
Also I think you are missing an unlock in the Backup() method no?
----------- Snip -----------
LPVOID pMem = ::GlobalLock( hMem );
data.m_pData = new byte[data.m_nLength];
memcpy(data.m_pData, pMem, data.m_nLength);
//HERE you need to add this no? --> ::GlobalUnlock( hMem );
m_lstData.AddTail(data);
----------- Snip end -----------
Chris
|
|
|
|
|
Thank you very much for your patience on the problem.
Yes, I missed an unlock indeed.
This class may has other problems too. I am not so sure how to deal with formats such as CF_ENHMETAFILE. Your can refer to former comment "CF_ENHMETAFILE" by Mr FlyZu and the replies.
Thanks again.
|
|
|
|
|
|
When I copied at bitmap image from an IE browser window, my Word COM add-in tried to save the clipboard and encountered an access violation. Any ideas?
Thanks, Jerry
Here's the debug output:
CLIPBOARD FORMAT: 0xC009, DataObject
CLIPBOARD FORMAT: 0xC2EC, UntrustedDragDrop
CLIPBOARD FORMAT: 15
CLIPBOARD FORMAT: 8
CLIPBOARD FORMAT: 0xC0CE, HTML Format
CLIPBOARD FORMAT: 0xC013, Ole Private Data
CLIPBOARD FORMAT: 2
First-chance exception at 0x77f9cec6 (ntdll.dll) in WINWORD.EXE: 0xC0000005: Access violation reading location 0x50050d4f.
|
|
|
|
|
|
Thanks a lot for your code. It helped me a lot. But, there is a problem. It's not possible to restore the data of type CF_ENHMETAFILE. I think it's because we have to create meta files to store the data and after that, we can restore it. I dont' know how to do this. It would be nice if you can have a look to this problem. Or if you have any hints, don't hesitate.
|
|
|
|
|
|
Don't be sorry.
I think I've found a possible solution. I added a field of type HENHMETAFILE in the structure.
Backup :
When the format is of type CF_ENHMETAFILE, I simply save the metafile like this : data.metafile = CopyEnhMetaFile((HENHMETAFILE)GetClipboardData(data.format))
Restore :
To restore the clipboard, if the format is equal to CF_ENHMETAFILE, I do a SetClipboard(data.format, data.metafile).
It works fine.
-- modified at 5:39 Monday 14th November, 2005
|
|
|
|
|
Hi,
is there a way to do it in C#? Do I have to know the data type currently present into the clipboard? Can I make it completely generic?
Thanks a lot.
Andrea
|
|
|
|
|
Windows api can be invoked in C#, so clipboard backup can be implemented. The following apis are needed:
class Win32ClipboardAPI
{
[DllImport("user32.dll")]
public static extern bool OpenClipboard(IntPtr hWndNewOwner);
[DllImport("user32.dll")]
public static extern bool EmptyClipboard();
[DllImport("user32.dll")]
static extern IntPtr GetClipboardData(uint uFormat);
[DllImport("user32.dll")]
public static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem);
[DllImport("user32.dll")]
public static extern bool CloseClipboard();
[DllImport("user32.dll")]
static extern uint EnumClipboardFormats(uint format);
[DllImport("user32.dll")]
static extern int GetClipboardFormatName(uint format, [Out] StringBuilder lpszFormatName, int cchMaxCount);
[DllImport("user32.dll", SetLastError=true)]
static extern uint RegisterClipboardFormat(string lpszFormat);
}
class Win32MemoryAPI
{
[DllImport("Kernel32.dll", EntryPoint="RtlMoveMemory", SetLastError=false)]
public static extern void CopyMemory(IntPtr dest, IntPtr src, int size);
[DllImport("kernel32.dll")]
public static extern IntPtr GlobalAlloc(uint uFlags, UIntPtr dwBytes);
[DllImport("kernel32.dll")]
public static extern IntPtr GlobalLock(IntPtr hMem);
[DllImport("kernel32.dll")]
public static extern IntPtr GlobalFree(IntPtr hMem);
[DllImport("kernel32.dll")]
public static extern UIntPtr GlobalSize(IntPtr hMem);
public const uint GMEM_DDESHARE = 0x2000;
public const uint GMEM_MOVEABLE = 0x2;
}
you can rewrite my class CClipboardBackup into C# with these apis.
Good luck! Write to me if you need help (sswater@gmail.com)!
|
|
|
|
|
Thanks for your help. I'll give it a try. I thought there was a solution not involving PInvoke. Actually, .NET has a clipboard class with two methods GetData and Setdata.
I was using the following code
//backup original data
IDataObject data=Clipboard.GetDataObject();
//insert new data into the clipboard
string test="test data";
Clipboard.SetDataObject(test);
//restore old data
Clipboard.SetDataObject(data);
but I get an exception (or no data is restored) when I call the last function.
I think the problem has to do with the first instruction where I save the data. Do I have to necessarily know the format of the data into the clipboard? Can't I just do a snapshot of it and restore it into place later?
Thanks a lot.
|
|
|
|
|
I have never used class Clipboard in .NET before, so I'm sorry and I could not give you a complete explanation.
I read the MSDN just now, I thought that the SetDataObject (in .NET) could only set one data of one certain format. After my glance at MSDN, I have not find a way to set datas of more than one format.
In my C++ implementation, I regard each clipboard format data as binary data and make a backup copy for each.
|
|
|
|
|
I've found how to do it in C# and would like to share the solution.
Here's the code.
<br />
IDataObject oldClipboard = Clipboard.GetDataObject();<br />
DataObject newClipboard = new DataObject();<br />
string[] s;<br />
s = oldClipboard.GetFormats();
<br />
foreach(string ns in s) {<br />
newClipboard.SetData(ns,oldClipboard.GetData(ns));
}<br />
<br />
Clipboard.SetDataObject(newClipboard,true);
and here's the link where I found it. Hope other people will find it useful.
http://objectsharp.com/blogs/dan/archive/2004/08/20/836.aspx[^]
|
|
|
|
|
A small notion: may be it is worth to have
#include "ClipboardBackup.h"<>
in ClipboardBackup.cpp
|
|
|
|
|
Thank you for your notion, I have updated the zip file
|
|
|
|