본문 바로가기

.NET/WPF

WPF에서 Window Message 다루기

You can do this via the System.Windows.Interop namespace which contains a class named HwndSource.

using System; using System.Windows;
using
System.Windows.Interop;

namespace
WpfApplication1
{
   
public partial class Window1 : Window
   
{
       
public Window1()
       
{
           
InitializeComponent();
       
}

       
protected override void OnSourceInitialized(EventArgs e)
       
{
           
base.OnSourceInitialized(e);
           
HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
            source
.AddHook(WndProc);
       
}

       
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
       
{
           
// Handle messages...

            if(msg ==     )

           { }


           
return IntPtr.Zero;
       
}
   
}
}

 

MVVM에서 구현하려면 View의 비하인드 코드에 위 코드를 넣으면 된다.

(ex. MainWindows.xaml.cs)