Process Windows messages in WPF

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.