Child Control Macros

[Previous] [Next]

The child control macros make it easier to send messages to child controls. They are very similar to the FORWARD_WM_* macros. Each macro starts with the type of control you are sending the message to, followed by an underscore and the name of the message. For example, to send an LB_GETCOUNT message to a list box, you use the following macro from WindowsX.h:

 #define ListBox_GetCount(hwndCtl) \ ((int)(DWORD)SendMessage((hwndCtl), LB_GETCOUNT, 0, 0L)) 

Let me point out a couple of things about this macro. First, it takes only one parameter, hwndCtl, which is the window handle of the list box. Because the LB_GETCOUNT message ignores the wParam and lParam parameters, you don't need to bother with them. The macro passes zeros, as you can see above. Second, when SendMessage returns, the result is cast to an int so you don't have to supply your own cast.

The one thing I don't like about the child control macros is that they take the handle of the control window. Most of the time, the controls you need to send messages to are children of a dialog box. So you end up having to call GetDlgItem all the time, producing code like this:

 int n = ListBox_GetCount(GetDlgItem(hDlg, ID_LISTBOX)); 

This code doesn't run any slower than it would if you used SendDlgItemMessage, but your application will contain some extra code because of the additional call to GetDlgItem. If you need to send several messages to the same control, you might want to call GetDlgItem once, save the child window's handle, and then call all the macros you need, as shown in the following code:

 HWND hwndCtl = GetDlgItem(hDlg, ID_LISTBOX); int n = ListBox_GetCount(hwndCtl); ListBox_AddString(hwndCtl, "Another string");  

If you design your code in this way, your application will run faster because it won't have to repeatedly call GetDlgItem. GetDlgItem can be a slow function if your dialog box has many controls and the control you are looking for is toward the end of the z-order.



Programming Applications for Microsoft Windows
Programming Applications for Microsoft Windows (Microsoft Programming Series)
ISBN: 1572319968
EAN: 2147483647
Year: 1999
Pages: 193

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net