Working with Windows messages in WPF

Source: Internet
Author: User

Processing Windows messages in WinForm by overriding the WndProc method

The system.windows is used in WPF. Sytem.Windows.Controls and other namespaces, no WndProc function

In WPF, processing a message first to get the window handle, creating a HwndSource object to add a message-handling callback function through the HwndSource object.

In addition, WPF does not have a handle property and cannot directly get a handle to a window

You can specify a callback function for message processing in the constructor, or it can be specified after the resource is initialized, and can be specified in many places.

The following is specified after the resource is initialized

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 To use the System.Windows.Interop namespace, the above adds a callback delegate

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 () & 0xFFFF, Lparam.toint32 () >> 16);
Point mousewindowpoint = this. Pointfromscreen (Mousescreenpoint);
if (Mousewindowpoint.x < && Mousewindowpoint.y < 10)
{
handled = TRUE;
Return (INTPTR) Win32.httopleft;
}

Break
}
}
return IntPtr.Zero;
}

Here is a reference to pass the parameter handled, after processing the message set to True tells the system that the message has been processed.

Working with Windows messages in WPF

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.