wpf로 윈도우즈 메시지 처리하기

private void OnWindowLoaded(object sender, RoutedEventArgs e)
{
    HwndSource source = (HwndSource)PresentationSource.FromVisual(this);

    source.AddHook(WndProc);
}

IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
   ...  

   return IntPtr.Zero;
}

AddHookHwndSourceHook라는 델리개이트를 아규먼트로 받는다.

메떠드에 메떠드를 아규먼트로 전달할 때에는 메떠드 자체를 넘길 수 없고 델리게이트라는 레퍼런스 타입으로 넘겨야 한다. 메떠드는 밸류가 아니기 때문이다. Func와 Action도 모두 델리개이트다.

Delegates are used to pass methods as arguments to other methods.
Delegates (C# Programming Guide)

AddHook가 HwndSourceHook라는 델리개이트를 콕 집어서 지정한 건 이게 갖는 패러미터들을 특정하기 위해서다. 따라서 아래 사양에 따른 패러미터들을 써야 한다.

public delegate IntPtr HwndSourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled);

패러미터의 이름은 달라도 되지만 순서는 바꾸면 안 된다.