-1

I have this piece of code that works on Winform:

public class GlobalHotkey
{
    private int modifier;
    private int key;
    private IntPtr hWnd;
    private int id;

    public GlobalHotkey(int modifier, Keys key, Form form)
    {
        this.modifier = modifier;
        this.key = (int)key;
        this.hWnd = form.Handle;
        id = this.GetHashCode();
    }

    public bool Register()
    {
        return RegisterHotKey(hWnd, id, modifier, key);
    }

    public bool Unregiser()
    {
        return UnregisterHotKey(hWnd, id);
    }

    public override int GetHashCode()
    {
        return modifier ^ key ^ hWnd.ToInt32();
    }

    [DllImport("user32.dll")]
    private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);

    [DllImport("user32.dll")]
    private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
}

And after InitializeComponent I just initiate it this way:

ghk = new GlobalHotkey(Constants.CTRL + Constants.SHIFT, Keys.A, this);

I have a WPF project and I want to use the same class, so I try to change the constructor this way:

public GlobalHotkey(int modifier, Keys key, System.Windows.Window form)
{
    this.modifier = modifier;
    this.key = (int)key;
    this.hWnd = form.Handle;
    id = this.GetHashCode();
}

But I have a compile-time error at this line:

this.hWnd = form.Handle;

Severity Code Description Project File Line Suppression State Error CS1061 'Window' does not contain a definition for 'Handle' and no accessible extension method 'Handle' accepting a first argument of type 'Window' could be found (are you missing a using directive or an assembly reference?)

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
falukky
  • 1,099
  • 2
  • 14
  • 34
  • Possible duplicate of [How to get the hWnd of Window instance?](https://stackoverflow.com/questions/10675305/how-to-get-the-hwnd-of-window-instance) – Peter Duniho Oct 06 '19 at 16:25

1 Answers1

0

Using WPF, the WindowInteropHelper class exists to get the required handle.

Members of this class allows the caller to have internal access to the Win32 HWND and the parent HWND of a WPF Window.

After creating a WindowInteropHelper, you can use it's handle like done with a Form. In your case, the constructor should look like this:

public GlobalHotkey(int modifier, Keys key, Window window)
{
    this.modifier = modifier;
    this.key = (int)key;
    //Use handle to register or unregister hotkey
    var helper = new WindowInteropHelper(window);
    this.hWnd = helper.Handle;
    id = this.GetHashCode();
}   

Note that you can also pass IntPtr.Zero as handle if the hotkey does not needs to be received by a specific window.

Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
  • Where i need to use this 2 lines ? – falukky Oct 06 '19 at 08:29
  • This replaces the win forms `this.hWnd = form.Handle` – Fruchtzwerg Oct 06 '19 at 08:32
  • My function signature is still GlobalHotkey(int modifier, Keys key, Form form) and i have compile error: Severity Code Description Project File Line Suppression State Error CS1503 Argument 1: cannot convert from 'Hotkeys.classes.core.GlobalHotkey' to 'System.Windows.Window' – falukky Oct 06 '19 at 08:35
  • Sure, you need also to replace the `Form` by a WPF `Window`. – Fruchtzwerg Oct 06 '19 at 08:37