开发者

C# - Overwrite Ctrl+C control

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 开发者_如何学JAVAnothing gets stored in...


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!


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜