
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 Hosting a Win32 ListBox Control in Windows Presentation Foundation Sample 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 MessageHook 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 StringBuilder. You then send the control an LB_GETTEXT message. Pass the empty StringBuilder object as the wParam parameter. When SendMessage returns, the StringBuilder 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.