tags:

views:

430

answers:

1

Hello,

i found this code:

    protected override void OnSourceInitialized(EventArgs e)
    {
        base.OnSourceInitialized(e);
        HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;

        if (hwndSource != null)
        {
            installedHandle = hwndSource.Handle;
            viewerHandle = SetClipboardViewer(installedHandle);
            hwndSource.AddHook(new HwndSourceHook(this.hwndSourceHook));
        }
    }

to start a hwndSourceHook (to capture the clipboard). But this code only works with a "Window" but not with a "Windows.Form".

How i can get the hwndSource of my Form to add the hwndSourceHook?

(instead of the override I should use the Form_Load function I think...)

EDIT: Thanks, but the Form doesnt have the AddHook function to add my hwndSourceHook

+3  A: 

If you are using WinForms, then it is just myForm.Handle

The HwndSource is for WPF.

So you can just do:

viewerHandle = SetClipboardViewer(myForm.Handle);

Edit: AddHook is also a WPF method.

You need to use either:

Application.AddMessageFilter(...);

or, in your Form class override the WndProc method:

protected override void WndProc(ref Message m) {...}

AddMessageFilter can capture messages for any window in your application, whereas WndProc will only receive messages for the given window.

Steven