0

I need to create unfocusable form in C#. On that form there will be button. After pressing that button I want to paste content of System.Clipboard to the place where the cursot is. This is what I have till now:

 public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    const int WS_EX_NOACTIVATE = 0x08000000;
    const int WS_EX_TOPMOST = 0x00000008;
    const int WS_EX_TOOLWINDOW = 0x00000080;
    const int WM_MOUSEACTIVATE = 0x0021;
    const int WS_EX_WINDOWEDGE = 0x00000100;
    const int MA_NOACTIVATEANDEAT = 4;
    const int WM_NCHITTEST = 0x0084;
    //const int WM_ACTIVATEAPP = 0x001C;
    const int WS_THICKFRAME = 0x00040000;
    const int WS_CHILD = 0x40000000;
    const int WS_BORDER = 0x00800000;
    const int WS_DLGFRAME = 0x00400000;
    const int WS_CAPTION = WS_BORDER | WS_DLGFRAME;
    const int WS_SYSMENU = 0x00080000;

    protected override bool ShowWithoutActivation { get { return true; } }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams ret = base.CreateParams;
            ret.Style = WS_THICKFRAME |
               WS_CHILD | 
                WS_CAPTION | 
                WS_SYSMENU;
            ret.ExStyle |= WS_EX_NOACTIVATE | 
                WS_EX_TOOLWINDOW |
                WS_EX_TOPMOST | 
                WS_EX_WINDOWEDGE;
            ret.X = this.Location.X;
            ret.Y = this.Location.Y;
            return ret;
        }
    }

    private void simpleButton1_Click(object sender, EventArgs e)
    {
        Clipboard.SetText("text1 text2");
        SendKeys.SendWait("^v");
    }
}

The form is not the main form of my application. The code works only if the focus is in my application. If I focus notepad or any other program clipboard content is not pasted to it. Any one can help with this?

Pablo notPicasso
  • 3,031
  • 3
  • 17
  • 22
  • "After pressing that button I want to paste content of System.Clipboard to the place where the cursor is." - I don't understand this requirement: Surely the cursor will always be over the button that you just pressed? Or do you mean that you press the button via the keyboard? – Matthew Watson Sep 25 '12 at 08:27
  • I don't meen Mouse cursor, but place where keyboard input was last placed. – Pablo notPicasso Sep 25 '12 at 09:14
  • Is hte problem with making it unfocasable or pasting? – Deanna Sep 25 '12 at 09:59
  • If the form will not steel focus, I will be able to paste text from clipboard to current window. This works but only inside my application. – Pablo notPicasso Sep 25 '12 at 10:14

3 Answers3

0

I think at first you must determ a window to send a keystroke.

I found a good sample at codeproject and many questions here: 1 and other

Community
  • 1
  • 1
Ivan Kochurkin
  • 4,413
  • 8
  • 45
  • 80
0

Learn to title your questions properly. I believe you are looking for a clipboard monitor snippet. You will have to register your appplication to intercept clipboard changed events.

[DllImport("User32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer);

_ClipboardViewerNext = SetClipboardViewer(this.Handle);

protected override void WndProc(ref Message m)
{
    switch ((Win32.Msgs)m.Msg)
    {
        case Win32.Msgs.WM_DRAWCLIPBOARD:
        // Handle clipboard changed
        break;
        // ... 
   }
}

See this answer and this article.

Community
  • 1
  • 1
Chibueze Opata
  • 9,856
  • 7
  • 42
  • 65
  • This is not what I want. I want to paste text from clipboard to the window that has focus. For example if I have notepad opened and i click Paste button on my form, text from clipboard should be pasted to notepad. – Pablo notPicasso Sep 25 '12 at 10:17
0

When your application receives focus, it will be sent the WM_ACTIVATE or WM_ACTIVATEAPP messages which contain the previous focused window handle. You can then set the focus back using SetActiveWindow. You should still receive the click as usual.

Deanna
  • 23,876
  • 7
  • 71
  • 156
  • Unfortunately this works only inside my application. If the focus was in other program, than WM_ACTIVATE does not have information about that program. – Pablo notPicasso Sep 26 '12 at 07:31