Tech Off Thread

8 posts

How to Capture Control-V in .Net

Back to Forum: Tech Off
  • User profile image
    qwert231

    I actually have 2 questions for adding Copy/Paste into my app.

    How do I capture Control-V/Control-P?

    If I have the Handles on the entire form, is there a way I can find the currently focused control? (For instance, I have 1 handles to watch for ControlV/P for the whole form, and want the paste/copy to deal with the Textbox control the user is currently in.)

  • User profile image
    JChung2006

    Handle drag-drop commands, not Ctrl-V/Ctrl-P, which are merely keyboard shortcuts to those commands.

  • User profile image
    qwert231

    Since I haven't turned on Drag-Drop, could you explain a bit more? Or point me to an article? I googled for this, but must be typing the wrong thing.

  • User profile image
    JChung2006

    My apologies.  It isn't drag-and-drop; it's clipboard functionality.

    Instead of capturing Ctrl-C, Ctrl-X, and Ctrl-P keystrokes, turn on your window's NotifyMessage capture ability with SetStyle(ControlStyles.EnableNotifyMessage, true);, and capture these messages in your OnNotifyMessage handler.

    WM_CLEAR
    WM_COPY
    WM_CUT
    WM_PASTE

    You can also use the Clipboard class in System.Windows.Forms to access the contents of the clipboard.

    What does your source control look like?  It may be just as simple as using the Clipboard class to populate the clipboard upon some action in your control so that a Paste to the other TextBox you mentioned will fill it with the clipboard data you specified.

  • User profile image
    qwert231

    Sorry if I seem slow... but I'm still having trouble. I wrote this:

    <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
    Protected Overrides Sub WndProc(ByRef m As Message)
     ' Listen for operating system messages
     Select Case (m.Msg)
      Case WM_COPY
       ' Copy code
       Clipboard.SetDataObject(Me.txtInput.Text, True)

     Case WM_PASTE
       ' Past code
       If Clipboard.GetDataObject.GetDataPresent(DataFormats.Text) Then
        Me.txtInput.Text = Clipboard.GetDataObject.GetData(DataFormats.Text).ToString
       End If
     End Select
     MyBase.WndProc(m)
    End Sub


    But I need to define WM_COPY and WM_PASTE. I can't find a nice WParam reference out there.

     

  • User profile image
    JChung2006

    http://www.pinvoke.net/default.aspx/Constants.WM

    There's an Enum on that page where you can get all the WM_ constants.  WM_COPY is &H301 and WM_PASTE is &H302 if you don't want to include the whole Enum.

  • User profile image
    figuerres

    this may help:

    http://www.net-recon.com/blogs/david/archive/2006/12/17/how-to-make-standard-cut-copy-paste-and-undo-work-on-a-menustrip-in-c-vs2005.aspx


    How to make standard cut, copy, paste, and undo work on a MenuStrip in C# (VS2005)

    Okay, the “add standard menu items” in the menustrip in C#/VS2005 is really cool, but it breaks cut/paste/copy/undo in windows (including the default ctrl+c, ctrl+v, etc. hotkeys).

    So after tons of looking around for a c# example of how to do this with no success, I stumbled across a VB example that pointed me in the right direction (do a search for winuser.h - open it, then look for the constants below to see where I finally found the right pointers I needed.)

    Anyway, in the end I got cut/paste/copy/undo working by calling out to windows for the default behaviour..

    Post if you have any questions or comments - next .NET post will give some code examples on how to do cascade/minimize all/tile windows under the Window standard menu.

    See my below code example on how to make cut/paste/copy/undo work.

    ---------------------------------

    Edit: If you have a MDI based app, and the menu or toolbar resides on the main window, you will need to pass “ActiveMdiChild.ActiveControl.Handle” vs. “ActiveControl.Handle” to the SendMessage function.
    ———————————————–

    using System;
    using
    System.Collections.Generic;
    using
    System.ComponentModel;
    using
    System.Data;
    using
    System.Drawing;
    using
    System.Text;
    using
    System.Windows.Forms;
    using
    System.Runtime.InteropServices;

    namespace testwindows
    {

    public partial class Form1 : Form
    {

    [DllImport(“user32.dll”, CharSet=CharSet.Auto)]
    public static extern int SendMessage(IntPtr hwnd, int wMsg,

    IntPtr wParam,
    IntPtr lParam);
    private const int WM_CUT = 0×0300;
    private const int WM_COPY = 0×0301;
    private const int WM_PASTE = 0×0302;
    private const int WM_CLEAR = 0×0303;
    private const int WM_UNDO = 0×0304;

    public Form1()
    {
    InitializeComponent();
    }

    private void copyToolStripMenuItem_Click(object sender, EventArgs e)
    {
    SendMessage(ActiveControl.Handle, WM_COPY, IntPtr.Zero, IntPtr.Zero);
    }

    private void cutToolStripMenuItem_Click(object sender, EventArgs e)
    {
    SendMessage(ActiveControl.Handle, WM_CUT, IntPtr.Zero, IntPtr.Zero);
    }

    private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
    {
    SendMessage(ActiveControl.Handle, WM_PASTE, IntPtr.Zero, IntPtr.Zero);
    }

    private void undoToolStripMenuItem_Click(object sender, EventArgs e)
    {
    SendMessage(ActiveControl.Handle, WM_UNDO, IntPtr.Zero, IntPtr.Zero);
    }

    }
    }

  • User profile image
    qwert231

    Hmm... okay...

    I don't have a Toolstrip on THIS screen, but I do on the MDI Parent. This is VB .Net.

    On my MyBase.Load I have SetStyle(ControlStyles.EnableNotifyMessage, True)

    Hitting Ctrl-C on the keyboard doesn't get caught in the select.

    Private Const WM_CUT As Integer = &H300
    Private Const WM_COPY As Integer = &H301
    Private Const WM_PASTE As Integer = &H302
    Private Const WM_CLEAR As Integer = &H303
    Private Const WM_UNDO As Integer = &H304

    <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
    Protected Overrides Sub WndProc(ByRef m As Message)
     ' Listen for operating system messages
     Select Case (m.Msg)
      'Case WM_CLEAR
      Case WM_COPY
       ' Copy code
       Clipboard.SetDataObject(Me.txtInput.Text, True)

      'Case WM_CUT
      Case WM_PASTE
       ' Past code
       If Clipboard.GetDataObject.GetDataPresent(DataFormats.Text) Then
        Me.txtInput.Text = Clipboard.GetDataObject.GetData(DataFormats.Text).ToString

       End If
     End Select
     MyBase.WndProc(m)
    End Sub

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.