Child window controls |
|||||||||||||||||||||||
| PGUI: Child window controls | |||||||||||||||||||||||
| Contents: |
Below is a short introduction to the use of child window controls in Windows application programming. The details of individual control types are not covered. Window classesChild window controls are implemented in predefined window classes. The window classes (window procedures) are included in the system DLLs. The simple child window classes are
There is also a set of more complex child window controls called the "common controls". Creating child window controls
Child window controls are created in the similar way as top level windows in Windows
API. We can use functions HWND hLeftLabel, hLeftList;
...
hLeftLabel = CreateWindowEx(0, "STATIC", _T("Left elements:"),
WS_CHILD | WS_VISIBLE | SS_LEFT,
0, 0, 0, 0, hWnd, (HMENU) -1, hInst, NULL);
hLeftList = CreateWindowEx (WS_EX_CLIENTEDGE, _T("LISTBOX"), NULL,
WS_CHILD | WS_VISIBLE | LBS_SORT | LBS_EXTENDEDSEL | LBS_NOTIFY | WS_VSCROLL,
0, 0, 0, 0, hWnd,
(HMENU) LEFT_LIST_ID, hInst, NULL);
If yout need to refer to the child windows later on, you need to store the
returned handle in a variable of type MFC makes it somewhat easier to create child window controls with the predefined MFC classes. For example, to create a list ox, we write CListBox m_fontlist; The last paramater in both cases is the child id of the control. The control can be identified with the help of this id in message processing. The child window controls are usually created while processing the Layout
The simplest way to lay out the components is to specify the coordinates in case WM_SIZE: // calculate average character dimensions HDC hdc = GetDC(hWnd); TEXTMETRIC tm; GetTextMetrics(hdc, &tm); cxChar = tm.tmAveCharWidth; cyChar = tm.tmHeight + tm.tmExternalLeading; ReleaseDC(hWnd, hdc); // get the new dimensions of the window width = LOWORD(lParam); height = HIWORD(lParam); // position every chils window control MoveWindow(hLeftLabel, cxChar, cyChar/2, width/2 - 7*cxChar, cyChar, TRUE); MoveWindow(hLeftList, cxChar, 2*cyChar, width/2 - 7*cxChar, height - 2*cyChar, TRUE); ... break; In MFC this can be done in the same way, the function call differs little: m_fontlist.MoveWindow(m_cxchar*2, m_cychar, m_cxchar*30, cy - m_cychar*2, true); Control messagesWindow messages can be used for getting and/or setting the control attributes. We can use function SendMessage for sending messages to a window. For example, the code // get indices of selected items gets the indices of the selected items in a list referred by MFC provides convenience member functions that hide the message sending operation. Child window controls notify their parent about changes and user actionsby
sending messages to the parent. Usually they send case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
if (lParam == NULL) {
...
}
// Parse control messages:
else {
switch (wmId) {
case LEFT_LIST_ID:
if (wmEvent == LBN_SELCHANGE) {
long c = SendMessage(hLeftList, LB_GETSELCOUNT, 0, 0);
EnableWindow(hL2RBtn, c != 0);
}
break;
...
MFC uses message maps for connecting messages to functions: BEGIN_MESSAGE_MAP(CFontViewerPanel,CWnd ) ... ON_LBN_SELCHANGE(IDC_FONTLIST, FontChanged) ON_CBN_SELCHANGE(IDC_SIZECOMBO, FontChanged) END_MESSAGE_MAP() Example programsComponentDemo with Win API demonstrates the use of lists and buttons with scaling layout. FontViewer with MFC is another example of the use of child window controls. |
See also: |
|||||||||||||||||||||
| Jarkko Leponiemi, Wednesday, 19-Nov-2003 01:53:37 EET | |||||||||||||||||||||||