views:

716

answers:

3

Im thinking to create a simple app to store what I have copied using Ctrl+C. Now I have Googled some interesting code: (I will rather post link to it as its huge)

http://www.prilepi.com/221 (by http://www.liensberger.it/web/blog/?p=207)

http://www.prilepi.com/222

Now the thing works fine, the only problem is that it totally overwrites everything. That means that when I select text and hit Ctrl+C I cannot get the text I selected.

I'm aware of Clipboard class but nothing gets stored in...

A: 

The hot key completely hides the message from other windows, as you said. What you need to do is to find the window with the focus and then read the text it has selected (and then possibly add it to the clipboard manually I guess?)

This snippet should find the focused window. It's in C++ but you can easily translate it to C#.

HWND GetGlobalFocus()
{
   GUITHREADINGO info;
   info.cbSize = sizeof(info);

   if (!GetGUIThreadInfo(0, &info))
      return NULL;

   return info.hwndFocus;
}

Once you have that.. this is where it gets tricky. You could do a PostMessage(hWnd, WM_COPY, 0, 0); but it won't work if the control doesn't support this (any syntax highlighted control is most likely non standard and as such might not reply to this).

You could manually send a WM_GETTEXT message to get the text then manually add it to the clipboard, but again this will most likely fail it the control is heavily non-standard, not to mention that it won't preserve the applications possible multiple clipboard formats (think Word).

Another option is when you receive your hot key, disable your hook, send the key combination again with keybd_event and then enable your hook again, and you'll have the data in your clipboard. This seems clunky but it could work depending if keybd_event is blocking or not, I can't remember.

Hope this helps!

Blindy
Hm, as I understand it, it copies the selected text on focused window. Now I found few solutions on internet but none of them worked. Maybe because when I debug, VS is focused window and there is not selected text...
FrEaKmAn
`WM_COPY`? Yea, that's what it does, but the selected window must support the message. You'll find that half the text editors won't do anything when they receive that message. I think your best bet is the last option.
Blindy
And yes you obviously cant step through code like this, you should just spam the debug log with everything your code is doing instead and see where it fails by reading the output.
Blindy
A: 

To add somthing to the clipboard you can use:

Clipboard.SetText(Text, TextDataFormat.Text);

There are several other format options than just text as well

Mike B
its not problem to set something. It problem to retrieve it!
FrEaKmAn
A: 

There is another way of doing it, although it is a bit weak, suppose you have a textbox that is awaiting for input, you can assign a blank context menu by creating a new context menu with nothing on it, then assign it to the textbox. In that way, a user cannot right click to bring up the default context menu for copying/pasting.

Edit: Since the OP posted a question that is not exactly clear, 'C# Overwrite Ctrl+C code', I thought he meant, to prevent the usage of Ctrl+C to copy to the clipboard. Sorry if my answer is wrong. Can you please clarify your question a bit more as it sounds misleading.

Edit#2: After the OP's comments, I can give a clue on what he is trying to accomplish, for starters, head over to CodeProject and read this article and hook in the clipboard hook on a global basis and the job is done! No need to put in a global keyboard hook! Wire up the event ClipboardChanged and ClipboardStateChanged.

Hope this helps, Best regards, Tom.

tommieb75
Let me simplify it. Imagine a program where you can monitor what has been copy/pasted. So I need to detect when user hits CTRL+C, get the selected text but at the same time I dont modify the default CTRL+C...
FrEaKmAn
@FrEaKmAn: That sounds like installing a global keyboard hook to monitor the Ctrl+C and find out which window is it from...on another note, Ctrl+C is system wide thing for copying to clipboard..perhaps, from within the global hook, when ctrl+C is pressed, send custom message to your window along with information on which window handle is it on where the copying to clipboard is in progress..
tommieb75
@tommieb75 it sounds possible. But I am still lost how to accomplish this.
FrEaKmAn