Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C++
Article

Clipboard backup (Visual C++)

Rate me:
Please Sign up or sign in to vote.
4.58/5 (24 votes)
20 Sep 2005CPOL 150.8K   1.6K   51   27
Make a backup copy of clipboard data before your clipboard operation, and when you finish, you can restore clipboard to its original status.

Introduction

This piece of code is to help you backup your clipboard data and restore it after you have finished other clipboard operations.

My experience: while I'm developing Word add-in programs, the icon of the new-added button can only be set through clipboard by "...->PasteFace()". This will empty the clipboard data which is going to be pasted in Word. So, I wrote this class, and it helped me a lot.

Usage:

// the constructor will do backup clipboard operation
CClipboardBackup cbbackup;

// any other clipboard operations
::OpenClipboard(NULL);
::EmptyClipboard();
::SetClipboardData(......);
::CloseClipboard();
....

// restore
cbbackup.Restore();

Advertisement

By the way, my another article about another topic on another website :) :

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
China China
Begin coding from basic, since 1994. Interested in coding and database and website constructing.
My website: http://www.regexlab.com/ - Regular Expression Laboratory
The easiest regex engine: http://www.regexlab.com/deelx/

Comments and Discussions

 
GeneralMy vote of 1 Pin
AdminSam18-Jan-12 19:20
AdminSam18-Jan-12 19:20 
GeneralProblem with CF_BITMAP Pin
MohammadAmiry28-Mar-07 13:06
MohammadAmiry28-Mar-07 13:06 
GeneralRe: Problem with CF_BITMAP Pin
sswater shi28-Mar-07 14:36
sswater shi28-Mar-07 14:36 
Questionproblem with outlook Pin
vice12-Feb-07 6:53
vice12-Feb-07 6:53 
AnswerRe: problem with outlook Pin
sswater shi13-Feb-07 1:58
sswater shi13-Feb-07 1:58 
AnswerRe: problem with outlook Pin
vice13-Feb-07 2:52
vice13-Feb-07 2:52 
GeneralRe: problem with outlook Pin
sswater shi13-Feb-07 21:57
sswater shi13-Feb-07 21:57 
GeneralRe: problem with outlook Pin
pengchengwanli4-Dec-09 18:49
pengchengwanli4-Dec-09 18:49 
GeneralRe: problem with outlook Pin
pengchengwanli4-Dec-09 18:44
pengchengwanli4-Dec-09 18:44 
AnswerRe: problem with outlook Pin
pengchengwanli4-Dec-09 18:46
pengchengwanli4-Dec-09 18:46 
GeneralGreat ! + possible error Pin
ChrisRibe22-Mar-06 8:37
ChrisRibe22-Mar-06 8:37 
GeneralRe: Great ! + possible error Pin
sswater shi22-Mar-06 13:39
sswater shi22-Mar-06 13:39 
GeneralGOOD Pin
pengchengwanli5-Dec-09 20:56
pengchengwanli5-Dec-09 20:56 
GeneralIE Access violation for CF_BITMAP Pin
jnettleton2-Dec-05 6:56
jnettleton2-Dec-05 6:56 
GeneralRe: IE Access violation for CF_BITMAP Pin
sswater shi3-Dec-05 21:00
sswater shi3-Dec-05 21:00 
GeneralCF_ENHMETAFILE Pin
FlyZu10-Nov-05 2:23
FlyZu10-Nov-05 2:23 
AnswerRe: CF_ENHMETAFILE Pin
sswater shi13-Nov-05 15:57
sswater shi13-Nov-05 15:57 
GeneralRe: CF_ENHMETAFILE Pin
FlyZu13-Nov-05 23:00
FlyZu13-Nov-05 23:00 
QuestionHow to do it in c# Pin
nagarsoft13-Oct-05 11:49
nagarsoft13-Oct-05 11:49 
AnswerRe: How to do it in c# Pin
sswater shi13-Oct-05 16:21
sswater shi13-Oct-05 16:21 
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)!

GeneralRe: How to do it in c# Pin
nagarsoft13-Oct-05 20:01
nagarsoft13-Oct-05 20:01 
GeneralRe: How to do it in c# Pin
sswater shi13-Oct-05 20:47
sswater shi13-Oct-05 20:47 
AnswerRe: How to do it in c# Pin
nagarsoft14-Oct-05 6:19
nagarsoft14-Oct-05 6:19 
GeneralVery useful class - thanks a lot! Pin
vkurdukov20-Sep-05 1:43
vkurdukov20-Sep-05 1:43 
GeneralRe: Very useful class - thanks a lot! Pin
sswater shi20-Sep-05 16:45
sswater shi20-Sep-05 16:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.