To process Windows messages in winform, rewrite the wndproc method.
In WPF, system. Windows. sytem. Windows. controls and other namespaces are used, and wndproc functions are not available.
To process a message in WPF, you must first obtain the window handle and create an hwndsource object to add a message processing callback function through the hwndsource object.
Besides, WPF does not have the handle attribute and cannot directly obtain the window handle.
You can specify the callback function for message processing in the constructor or after Resource Initialization, which can be specified in many places.
After the resource is initialized, specify
Protected override void OnSourceInitialized (EventArgs e)
{
Base. OnSourceInitialized (e );
HwndSource hwndSource = PresentationSource. FromVisual (this) as HwndSource;
If (hwndSource! = Null)
HwndSource. AddHook (new HwndSourceHook (this. WndProc ));
}
Note that the system. Windows. InterOP namespace must be used, and a callback delegate is added.
Protected virtual IntPtr WndProc (IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
Switch (msg)
{
Case Win32.WM _ NCHITTEST:
{
Point MouseScreenPoint = new Point (lParam. ToInt32 () & 0 xFFFF, lParam. ToInt32 ()> 16 );
Point MouseWindowPoint = this. PointFromScreen (MouseScreenPoint );
If (MouseWindowPoint. X <10 & MouseWindowPoint. Y <10)
{
Handled = true;
Return (IntPtr) Win32.HTTOPLEFT;
}
Break;
}
}
Return IntPtr. Zero;
}
Here is a reference passed parameter handled. After processing the message, it is set to true to tell the system that the message has been processed.