0

I want to show the clipboard in a textbox as its content changes, I understand that I need to use imports from "user32.dll", but I have never used these kinds of imports and they are a bit confusing for me. I have checked this link and this link to get some ideas about Clipboard change event, so here are my questions: 1. Even though I copied and pasted the codes from the links above, they still don't work: error1 WndProc's error is: 'object' does not contain a definition for 'WndProc'

2. I'd like to know more about the user32.dll and kernel32.dll imports, if possible, please provide some information about them, or provide some sources that explain the basics of these. Thank you

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Iman Irt
  • 145
  • 3
  • 12
  • 1
    If you know nothing at all about Win32, then you are on a hiding to nothing. You should start by working with a C++ test project that does what you need. Learn what you need of Win32 that way. Learning Win32 with p/invoke is just asking for pain. – David Heffernan Sep 29 '15 at 19:12
  • 1
    You need a class with a message pump to get WndProc(). This is typically a Form or Control, but you can also use NativeWindow. Your class simply derives from the base Object class, which doesn't have WndProc. In [my example here](http://stackoverflow.com/a/20029427/2330053), I demonstrate how to use the NativeWindow approach. – Idle_Mind Sep 29 '15 at 19:45
  • 1
    `base.WndProc` makes no sense in this case, since your `ClipboardManager` class has no explicitly defined base class - so it descends from `Object`, which does not have a `WndProc` method. – 3Dave Sep 29 '15 at 20:06
  • @DavidLively yes you're right how silly of me! thank you – Iman Irt Sep 30 '15 at 06:06

1 Answers1

2

To monitor Clipboard in a form, you should create your form first , then use these codes, for example:

public class Form1: Form
{
    public Form1()
    {
        InitializeComponent();
        this.Load += Form1_Load;
    }

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

    public const int WM_DRAWCLIPBOARD = 0x0308;

    private void Form1_Load(object sender, EventArgs e)
    {
        SetClipboardViewer(this.Handle);
    }

    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        if (m.Msg != WM_DRAWCLIPBOARD)
            return;
        //Code To handle Clipboard change event
    }
}

Don't forget to add using System.Runtime.InteropServices;

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • this gave me a few good ideas about my code, but it still doesn't work. – Iman Irt Sep 30 '15 at 06:05
  • @ImanIrt I've checked the code and it works. What's the problem? – Reza Aghaei Sep 30 '15 at 06:06
  • I put the code 'textbox1.Text = "Clipboard changed" ' in the comment section that you provided (Code to handle clipboard change event) but I don't get anything when I copy something to clipboard. – Iman Irt Sep 30 '15 at 06:14
  • 1
    @ImanIrt You should call `SetClipboardViewer` somewhere, for example in your form load. – Reza Aghaei Sep 30 '15 at 06:17
  • @ImanIrt :) you are welcome, I edited the answer to be more clear. Also you can continue using `AddClipboardFormatListener` that you were using, this way. – Reza Aghaei Sep 30 '15 at 06:20