博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hosting a Win32 Control in WPF 在wpf中使用win32 控件
阅读量:4030 次
发布时间:2019-05-24

本文共 18352 字,大约阅读时间需要 61 分钟。

http://msdn.microsoft.com/en-us/library/ms752055.aspx

 

Walkthrough: Hosting a Win32 Control in WPF

.NET Framework 4

Windows Presentation Foundation (WPF) provides a rich environment for creating applications. However, when you have a substantial investment in Win32 code, it may be more effective to reuse at least some of that code in your WPF application rather than rewrite it completely. WPF provides a straightforward mechanism for hosting a Win32 window, on a WPF page.

This topic walks you through an application, , that hosts a Win32 list box control. This general procedure can be extended to hosting any Win32 window.

This topic contains the following sections.

Requirements

This topic assumes a basic familiarity with both WPF and Win32 programming. For a basic introduction to WPF programming, see . For an introduction to Win32 programming, you should reference any of the numerous books on the subject, in particular Programming Windows by Charles Petzold.

Because the sample that accompanies this topic is implemented in C#, it makes use of Platform Invocation Services (PInvoke) to access the Win32 API. Some familiarity with PInvoke is helpful but not essential.

Note Note

This topic includes a number of code examples from the associated sample. However, for readability, it does not include the complete sample code. You can obtain or view complete code from .

The Basic Procedure

This section outlines the basic procedure for hosting a Win32 window on a WPF page. The remaining sections go through the details of each step.

The basic hosting procedure is:

  1. Implement a WPF page to host the window. One technique is to create a element to reserve a section of the page for the hosted window.

  2. Implement a class to host the control that inherits from .

  3. In that class, override the class member .

  4. Create the hosted window as a child of the window that contains the WPF page. Although conventional WPF programming does not need to explicitly make use of it, the hosting page is a window with a handle (HWND). You receive the page HWND through the hwndParent parameter of the method. The hosted window should be created as a child of this HWND.

  5. Once you have created the host window, return the HWND of the hosted window. If you want to host one or more Win32 controls, you typically create a host window as a child of the HWND and make the controls children of that host window. Wrapping the controls in a host window provides a simple way for your WPF page to receive notifications from the controls, which deals with some particular Win32 issues with notifications across the HWND boundary.

  6. Handle selected messages sent to the host window, such as notifications from child controls. There are two ways to do this.

    • If you prefer to handle messages in your hosting class, override the method of the class.

    • If you prefer to have the WPF handle the messages, handle the class event in your code-behind. This event occurs for every message that is received by the hosted window. If you choose this option, you must still override , but you only need a minimal implementation.

  7. Override the and methods of . You must override these methods to satisfy the contract, but you may only need to provide a minimal implementation.

  8. In your code-behind file, create an instance of the control hosting class and make it a child of the element that is intended to host the window.

  9. Communicate with the hosted window by sending it Microsoft Windows messages and handling messages from its child windows, such as notifications sent by controls.

Implement the Page Layout

The layout for the WPF page that hosts the ListBox Control consists of two regions. The left side of the page hosts several WPF controls that provide a user interface (UI) that allows you to manipulate the Win32 control. The upper right corner of the page has a square region for the hosted ListBox Control.

The code to implement this layout is quite simple. The root element is a that has two child elements. The first is a element that hosts the ListBox Control. It occupies a 200x200 square in the upper right corner of the page. The second is a element that contains a set of WPF controls that display information and allow you to manipulate the ListBox Control by setting exposed interoperation properties. For each of the elements that are children of the , see the reference material for the various elements used for details on what these elements are or what they do, these are listed in the example code below but will not be explained here (the basic interoperation model does not require any of them, they are provided to add some interactivity to the sample).

Selected Text:
Number of Items:
Implement a Class to Host the Microsoft Win32 Control

The core of this sample is the class that actually hosts the control, ControlHost.cs. It inherits from . The constructor takes two parameters, height and width, which correspond to the height and width of the element that hosts the ListBox control. These values are used later to ensure that the size of the control matches the element.

public class ControlHost : HwndHost {
IntPtr hwndControl; IntPtr hwndHost; int hostHeight, hostWidth; public ControlHost(double height, double width) {
hostHeight = (int)height; hostWidth = (int)width; }

There is also a set of constants. These constants are largely taken from Winuser.h, and allow you to use conventional names when calling Win32 functions.

internal const int   WS_CHILD = 0x40000000,   WS_VISIBLE = 0x10000000,   LBS_NOTIFY = 0x00000001,   HOST_ID = 0x00000002,   LISTBOX_ID = 0x00000001,   WS_VSCROLL = 0x00200000,   WS_BORDER = 0x00800000;

Override BuildWindowCore to Create the Microsoft Win32 Window

You override this method to create the Win32 window that will be hosted by the page, and make the connection between the window and the page. Because this sample involves hosting a ListBox Control, two windows are created. The first is the window that is actually hosted by the WPF page. The ListBox Control is created as a child of that window.

The reason for this approach is to simplify the process of receiving notifications from the control. The class allows you to process messages sent to the window that it is hosting. If you host a Win32 control directly, you receive the messages sent to the internal message loop of the control. You can display the control and send it messages, but you do not receive the notifications that the control sends to its parent window. This means, among other things, that you have no way of detecting when the user interacts with the control. Instead, create a host window and make the control a child of that window. This allows you to process the messages for the host window including the notifications sent to it by the control. For convenience, since the host window is little more than a simple wrapper for the control, the package will be referred to as a ListBox control.

Create the Host Window and ListBox Control

You can use PInvoke to create a host window for the control by creating and registering a window class, and so on. However, a much simpler approach is to create a window with the predefined "static" window class. This provides you with the window procedure you need in order to receive notifications from the control, and requires minimal coding.

The HWND of the control is exposed through a read-only property, such that the host page can use it to send messages to the control.

public IntPtr hwndListBox {
get { return hwndControl; } }

The ListBox control is created as a child of the host window. The height and width of both windows are set to the values passed to the constructor, discussed above. This ensures that the size of the host window and control is identical to the reserved area on the page. After the windows are created, the sample returns a object that contains the HWND of the host window.

protected override HandleRef BuildWindowCore(HandleRef hwndParent) {
hwndControl = IntPtr.Zero; hwndHost = IntPtr.Zero; hwndHost = CreateWindowEx(0, "static", "", WS_CHILD | WS_VISIBLE, 0, 0, hostWidth, hostHeight, hwndParent.Handle, (IntPtr)HOST_ID, IntPtr.Zero, 0); hwndControl = CreateWindowEx(0, "listbox", "", WS_CHILD | WS_VISIBLE | LBS_NOTIFY | WS_VSCROLL | WS_BORDER, 0, 0, hostWidth, hostHeight, hwndHost, (IntPtr) LISTBOX_ID, IntPtr.Zero, 0); return new HandleRef(this, hwndHost); }
//PInvoke declarations [DllImport("user32.dll", EntryPoint = "CreateWindowEx", CharSet = CharSet.Unicode)] internal static extern IntPtr CreateWindowEx(int dwExStyle,                                               string lpszClassName,                                               string lpszWindowName,                                               int style,                                               int x, int y,                                               int width, int height,                                               IntPtr hwndParent,                                               IntPtr hMenu,                                               IntPtr hInst,                                               [MarshalAs(UnmanagedType.AsAny)] object pvParam);

Implement DestroyWindow and WndProc

In addition to , you must also override the and methods of the . In this example, the messages for the control are handled by the handler, thus the implementation of and is minimal. In the case of , set handled to false to indicate that the message was not handled and return 0. For , simply destroy the window.

protected override IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) {
handled = false; return IntPtr.Zero; } protected override void DestroyWindowCore(HandleRef hwnd) {
DestroyWindow(hwnd.Handle); }
[DllImport("user32.dll", EntryPoint = "DestroyWindow", CharSet = CharSet.Unicode)] internal static extern bool DestroyWindow(IntPtr hwnd);
Host the Control on the Page

To host the control on the page, you first create a new instance of the ControlHost class. Pass the height and width of the border element that contains the control (ControlHostElement ) to the ControlHost constructor. This ensures that the ListBox is sized correctly. You then host the control on the page by assigning the ControlHost object to the property of the host .

The sample attaches a handler to the event of the ControlHost to receive messages from the control. This event is raised for every message sent to the hosted window. In this case, these are the messages sent to window that wraps the actual ListBox control, including notifications from the control. The sample calls SendMessage to obtain information from the control and modify its contents. The details of how the page communicates with the control are discussed in the next section.

Note Note

Notice that there are two PInvoke declarations for SendMessage. This is necessary because one uses the wParam parameter to pass a string and the other uses it to pass an integer. You need a separate declaration for each signature to ensure that the data is marshaled correctly.

public partial class HostWindow : Window 	{
int selectedItem; IntPtr hwndListBox; ControlHost listControl; Application app; Window myWindow; int itemCount; private void On_UIReady(object sender, EventArgs e) {
app = System.Windows.Application.Current; myWindow = app.MainWindow; myWindow.SizeToContent = SizeToContent.WidthAndHeight; listControl = new ControlHost(ControlHostElement.ActualHeight, ControlHostElement.ActualWidth); ControlHostElement.Child = listControl; listControl.MessageHook += new HwndSourceHook(ControlMsgFilter); hwndListBox = listControl.hwndListBox; for (int i = 0; i < 15; i++) //populate listbox {
string itemText = "Item" + i.ToString(); SendMessage(hwndListBox, LB_ADDSTRING, IntPtr.Zero, itemText); } itemCount = SendMessage(hwndListBox, LB_GETCOUNT, IntPtr.Zero, IntPtr.Zero); numItems.Text = "" + itemCount.ToString(); }
private IntPtr ControlMsgFilter(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) {
int textLength; handled = false; if (msg == WM_COMMAND) {
switch ((uint)wParam.ToInt32() >> 16 & 0xFFFF) //extract the HIWORD {
case LBN_SELCHANGE : //Get the item text and display it selectedItem = SendMessage(listControl.hwndListBox, LB_GETCURSEL, IntPtr.Zero, IntPtr.Zero); textLength = SendMessage(listControl.hwndListBox, LB_GETTEXTLEN, IntPtr.Zero, IntPtr.Zero); StringBuilder itemText = new StringBuilder(); SendMessage(hwndListBox, LB_GETTEXT, selectedItem, itemText); selectedText.Text = itemText.ToString(); handled = true; break; } } return IntPtr.Zero; } internal const int LBN_SELCHANGE = 0x00000001, WM_COMMAND = 0x00000111, LB_GETCURSEL = 0x00000188, LB_GETTEXTLEN = 0x0000018A, LB_ADDSTRING = 0x00000180, LB_GETTEXT = 0x00000189, LB_DELETESTRING = 0x00000182, LB_GETCOUNT = 0x0000018B; [DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Unicode)] internal static extern int SendMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Unicode)] internal static extern int SendMessage(IntPtr hwnd, int msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lParam); [DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Unicode)] internal static extern IntPtr SendMessage(IntPtr hwnd, int msg, IntPtr wParam, String lParam);
Implement Communication Between the Control and the Page

You manipulate the control by sending it Windows messages. The control notifies you when the user interacts with it by sending notifications to its host window. The sample includes a UI that provides several examples of how this works:

  • Append an item to the list.

  • Delete the selected item from the list

  • Display the text of the currently selected item.

  • Display the number of items in the list.

The user can also select an item in the list box by clicking on it, just as they would for a conventional Win32 application. The displayed data is updated each time the user changes the state of the list box by selecting, adding, or appending an item.

To append items, send the list box an LB_ADDSTRING message. To delete items, send LB_GETCURSEL to get the index of the current selection and then LB_DELETESTRING to delete the item. The sample also sends LB_GETCOUNT, and uses the returned value to update the display that shows the number of items. Both these instances of SendMessage use one of the PInvoke declarations discussed in the previous section.

private void AppendText(object sender, EventArgs args) {
if (txtAppend.Text != string.Empty) {
SendMessage(hwndListBox, LB_ADDSTRING, IntPtr.Zero, txtAppend.Text); } itemCount = SendMessage(hwndListBox, LB_GETCOUNT, IntPtr.Zero, IntPtr.Zero); numItems.Text = "" + itemCount.ToString(); } private void DeleteText(object sender, EventArgs args) {
selectedItem = SendMessage(listControl.hwndListBox, LB_GETCURSEL, IntPtr.Zero, IntPtr.Zero); if (selectedItem != -1) //check for selected item {
SendMessage(hwndListBox, LB_DELETESTRING, (IntPtr)selectedItem, IntPtr.Zero); } itemCount = SendMessage(hwndListBox, LB_GETCOUNT, IntPtr.Zero, IntPtr.Zero); numItems.Text = "" + itemCount.ToString(); }

When the user selects an item changes their selection, the control notifies the host window by sending it a WM_COMMAND message, which raises the event for the page. The handler receives the same information as the main window procedure of the host window. It also passes a reference to a Boolean value, handled . You set to handled to true to indicate that you have handled the message and no further processing is needed.

WM_COMMAND is sent for a variety of reasons, so you must examine the notification ID to determine whether it is an event that you wish to handle. The ID is contained in the high word of the wParam parameter. Since Microsoft .NET does not have a HIWORD macro, the sample uses bitwise operators to extract the ID. If the user has made or changed their selection, the ID will be LBN_SELCHANGE.

When LBN_SELCHANGE is received, the sample gets the index of the selected item by sending the control a LB_GETCURSEL message. To get the text, you first create a . You then send the control an LB_GETTEXT message. Pass the empty object as the wParam parameter. When SendMessage returns, the will contain the text of the selected item. This use of SendMessage requires yet another PInvoke declaration.

Finally, set handled to true to indicate that the message has been handled.

See Also

Reference

Concepts

转载地址:http://woqbi.baihongyu.com/

你可能感兴趣的文章
/dev/input/event0 键盘输入
查看>>
qt 创建异形窗体
查看>>
可重入函数与不可重入函数
查看>>
简单Linux C线程池
查看>>
内存池
查看>>
输入设备节点自动生成
查看>>
opencv test code-1
查看>>
eclipse 导入先前存在的项目
查看>>
GNU hello代码分析
查看>>
Qt继电器控制板代码
查看>>
busybox passwd修改密码
查看>>
wpa_supplicant控制脚本
查看>>
rfkill: WLAN hard blocked
查看>>
gstreamer相关工具集合
查看>>
arm 自动升级脚本
查看>>
RS232 四入四出模块控制代码
查看>>
gstreamer插件之 videotestsrc
查看>>
autoupdate script
查看>>
linux 驱动开发 头文件
查看>>
/etc/resolv.conf
查看>>