WPF interoperability with Win32

Source: Internet
Author: User
Tags win32 window

I. How WPF uses HWND

When you create a WPF window, WPF creates a top-level HWND and uses HwndSource to put the window and its WPF content into the HWND. The rest of the WPF content in the application shares this single HWND. However, the menu, combo box drop-down list, and other pop-up windows are exceptions. These elements create their own top-level windows, which is why the WPF menu can jump out of the window HWND that contains it.

Second, HwndSource class

A HwndSource Implementation Win32 window can contain WPF content. WPF content in a window is configurable, measurable, and can be rendered, and can be interactively entered. Because HwndSource is designed to interact with WIN32, this class shows the characteristics of the underlying Win32. You can use this class to perform the following actions:

    • Specifies window styles, window class styles, and extended window styles.
    • Add a handler function to the Hook of the window program.
    • Provides a window handle (HWND).
    • Destroys the window.

The HwndSource class is designed to handle general interoperability and is not designed to be a role of a wrapper management hwnd. In general, it does not provide a managed method for managing windows or checking property state. Instead, the HwndSource class can access the Win32 window handle (HWND) through the handle property, which can be manipulated through the PInvoke Win32 API Technology window.
1 Create
HwndSource can only be specified at construction time. To create a hwndsource, first create a hwndsourceparameters structure and populate it with the required parameters. These parameters include the following:

    • The window styles for classes, Windows, and extensions. You must use PInvoke to change the style before the window is created. Not all styles can be changed after the window is created. Refer to the Win32 documentation before changing the window style.
    • The initial position of the window.
    • The initial size of the window, including whether to specify the size or size of the window to determine the size of the WPF content.
    • The parent window.
    • HwndSourceHook includes the window's processing function chain. If you specify a hook at build time, it will receive all messages from the window. You can use Addhook to add a hook after creating the window.
    • Transparency settings.

2 Object Life cycle
HwndSource is a formal common language runtime (CLR) object whose life cycle is managed by the garbage collector. Because HwndSource represents unmanaged resources, HwndSource implements the IDisposable interface. If called from the owner thread, the synchronous call processing immediately destroys the Win32 window. If called from another thread, the Win32 window is destroyed asynchronously. Explicitly calling Dispose in interop code may be necessary for some interop scenarios.
3 Window processing functions
The HwndSource class implements its own window processing function. This window handler functions to handle important window messages, such as those related to layout, rendering, and input. However, you can also add your own handler functions on the hooks. You can specify your own handler function in the hooks when constructing, by setting the Hwndsourceparameters.hwndsourcehook property, or you can use Addhook and removehook to add and remove handlers from the hooks after the window is created.

Third, HwndSource class

This example extends the handler function in the hook.

//in the window classprotectedOverriedvoidonsourceinitialized (EventArgs e) {Base.    Onsourceinitialized (e); //get form handleINTPTR hwnd =NewWindowinterophelper ( This).    Handle; //Get HwndSource ObjectHwndSource HwndSource =Hwndsource.fromhwnd (HWND); //adding handlers to hooks    if(HwndSource! =NULL) Hwndsource.addhook (WndProc);}PrivateIntPtr WndProc (IntPtr hWnd,intMSG, IntPtr Wideparam, IntPtr Longparam,ref BOOLhandled) {SWICTH (msg) {//to process a specific message    }    returnIntPtr.Zero;}

WPF interoperability with Win32

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.