I have this wpf window that is supposed to retrieve the WM_HOTKEY message and perform an action once it's received. So far, I have been able to retrieve the WM_HOTKEY message from an HwndSourceHook (yes, using a method called WndProc). For example:
HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
source.AddHook(new HwndSourceHook(WndProc));
However, I am not able to perform the desired action as the method is located outside of the HwndSourceHook.
For example, consider the code
private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled)
{
switch(msg)
{
case WM_DESTROY:
System.Console.WriteLine("\nwe were sent a WM_DESTROY\n");
break;
case WM_HOTKEY:
switch (wparam.ToInt32())
{
case 186:
Console.WriteLine("\nsomething should be done here\n");
break;
case 187:
Console.WriteLine("\nsomething else should be done here\n");
break;
}
break;
}
return IntPtr.Zero;
}
As you can see, it is a static method, but I want to use a different method from the class. Would that be possible?
Loading
Theodore GielarowskiPosted Jun 2, 2010, 8:09 AM
private HwndSource source;
private HwndSourceHook delegateHook;
public void Hook(HwndSourceHook hookDelegate)
{
if (source == null)
{
source = HwndSource.FromHwnd(this._hwnd);
delegateHook = new HwndSourceHook(hookDelegate);
source.AddHook(delegateHook);
}
}
public void UnHook()
{
if (source != null)
{
source.RemoveHook(delegateHook);
source.Dispose();
source = null;
}
}
public void Dispose()
{
UnHook();
}