Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In Windows forms applications:

I have a translation program that needs to get the selected text when F1 is pressed globally, it sends Ctrl+C to active window in Windows and then uses the text and translates it, now I want it to restore what was in clipboard before my program sent Ctrl+C.

What I have tried:

//Backup
var lBackup = new Dictionary<string, object>();
var lDataObject = Clipboard.GetDataObject();
var lFormats = lDataObject.GetFormats(false);
foreach(var lFormat in lFormats)
{
  lBackup.Add(lFormat, lDataObject.GetData(lFormat, false));
}

//Set test data
Clipboard.SetText("asd");

//Would be interesting to check the contents of lDataObject here

//Restore data
foreach(var lFormat in lFormats)
{
  lDataObject.SetData(lBackup[lFormat]);
}
//This might be unnecessary
Clipboard.SetDataObject(lDataObject);
Posted
Updated 22-Jan-18 4:41am

 
Share this answer
 
Comments
john1990_1 21-Jan-18 3:36am    
Thx but sorry, I don't think the answer is there.
Richard MacCutchan 21-Jan-18 8:20am    
Then you need to explain the question.
john1990_1 21-Jan-18 9:39am    
I have a translation program that translates selected text in Windows when F1 is pressed globally, when F1 is pressed it sends Ctrl+C to the active window and translates the text in clipboard, and destroys what was in clipboard, I want to store what was in clipboard, then send Ctrl+C to active window, copy the selected text into clipboard, translate the text in clipboard, and restore what was in clipboard before F1 was pressed and Ctrl+C was sent.

http://sites.google.com/site/windowsprogramfortranslation/
phil.o 21-Jan-18 10:14am    
Still, this is not a question, this is a requirement.
You want to save the content of the clipboard prior to changing it. Then why don't you just create a string variable and save the current text content of the clipboard before reassigning it?
Something like string originalText = Clipboard.GetText();
john1990_1 21-Jan-18 10:29am    
clipboard could have complex data not only a string (files, a pic...)
Theres's this but they say in comments that it has errors.

Clipboard backup in C#[^]

Do you have a better code?
 
Share this answer
 
Comments
Richard MacCutchan 21-Jan-18 12:03pm    
This is not difficult. Capture the clipboard content. Do your translation work. Restore the original content.

I gave you a link to the ClipBoard class which has all the information you need to achieve this.
john1990_1 21-Jan-18 12:05pm    
Then since I learned C# by trial and error I don't think I have the ability to accomplish that by myself, would you please help?
Richard MacCutchan 21-Jan-18 12:09pm    
No, because we have no idea what your problem is.
john1990_1 21-Jan-18 12:12pm    
I have a translation program that translates selected text in Windows when F1 is pressed globally, when F1 is pressed it sends Ctrl+C to the active window and translates the text in clipboard (the data that was in clipboard vanishes), I want to store what was in clipboard, and then when F1 is pressed send Ctrl+C to active window of Windows, copy the selected text into clipboard, translate the text in clipboard globally in my program, and restore what was in clipboard before F1 was pressed and Ctrl+C was sent.

http://sites.google.com/site/windowsprogramfortranslation/

Clear?
Richard MacCutchan 22-Jan-18 3:53am    
Yes, you keep repeating this, and I have given you a link to the ClipBoard class which contains all the methods for saving and restoring data. Use the ContainsXXX methods to find out what format the data is in. Use the appropriate Get method to capture the data, and the corresponding Set methods to post it back to the clipboard.
Here is a basic outline of what you need to do.
C#
IDataObject iData = Clipboard.GetDataObject(); // get a link to the ClipBoard data
string strSave = (string)iData.GetData(DataFormats.Text); // get the object's data

// do your copying & translating

Clipboard.SetText(strSave, TextDataFormat.UnicodeText); // restore original cotents

This is just a simple template for saving and restoring some Unicode text. You need to add code to cater for all the different formats of data, as shown in the documentation for the Clipboard class - see the link I gave you above.
 
Share this answer
 
Comments
john1990_1 22-Jan-18 9:26am    
I almost understood that, but because of lack of knowledge and not laziness I'm unable to do that.
Richard MacCutchan 22-Jan-18 10:26am    
I am sorry, but there is not space here, and I do not have the time, to write a tutorial on using the Clipboard. If this is too complicated for you to understand, then I suggest you work on some simpler projects until you gain more knowledge.
john1990_1 22-Jan-18 10:28am    
Well I wanted to make this program, I disabled this functionality in it, thx anyway.
This is not as simple as it might seem initially.

Clipboard data are stored by the system internally in various formats (global memory, IStream, IStorage, file, GDI object).

This is also the reason why the code from the Clipboard backup in C#[^] article can crash:
It uses Win32 API calls but does not check the storage type. It assumes global memory data which results in NULL pointers when trying to access global memory with a handle that is for example an an IStream or a GDI object.

So you have to determine the storage type for each clipboard object and store it together with the format and the raw data. Upon restoring, the same storage types should be used (and must be for GDI objects).

I must confess that I don't know what IDataObject.GetData Method (String, Boolean) (System.Windows.Forms)[^] is doing internally. But it returns a System.Object type which is probably not able to represent all kinds of data that may be on the clipboard and might not use the same storage method when putting such on the clipboard. But it will handle the different storage methods and use them to read the data and create a System.Object.

Even when handling all the above there might be differences in the internal clipboard data because an application must not put data on the clipboard initially but can create the content upon request when the data are retrieved (delay rendering). A clipboard source may also specify a release callback function which can't be stored and re-used by your application when putting the data back.
 
Share this answer
 
Comments
john1990_1 22-Jan-18 10:50am    
So being a hobbyist I can't do it? I have disabled this functionality in my program but if you provide a class to do that I might reconsider adding it...
Jochen Arndt 22-Jan-18 11:25am    
I'm sorry, but I can't provide a class (I'm a C++ developer and have only basic C# knowledge). You might check if your solution works for all kind of clipboard data (I'm not sure about that). Probably with disposing the lDataObject (re-using it might not work). If that works you just have to accept that the clipboard is not restored idfentically (but the content of the objects is the same).

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900