Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 21: Tree and List Controls


IDC_YEAR int m_Year
IDC_DOORS int m_Doors
IDC_MILEAGE long m_Mileage
IDC_TRANSMISSION int m_Transmission
IDC_AC BOOL m _AC
IDC_AIR_BAGS BOOL m _AirBags
IDC_CRUISE_CONTROL BOOL m _CruiseControl
IDC_CONVERTIBLE BOOL m _Convertible
IDC_CASSETTE BOOL m _Cassette
IDC_CD_PLAYER BOOL m _CDPlayer
IDC_PICTURE BOOL CStatic m_Picture


  1. Save All


21.1.4..Tree Control Messages.......................................................................


Most messages of the tree controls are notification messages that are sent to its parent
window. For example, the NM_CLICK message is sent to the dialog box or the form,
that acts as the parent, that the tree control has been clicked. If the click was done with
the right mouse button, the NM_RCLICK message is sent instead. In the same way, if
the user double-clicks the control an NM_DBLCLK message is sent. If the user double-
clicked with the right mouse button, the NM_CDBLCLK message is sent.

As mentioned already, the user has the ability to expand a node that has at least one child.
When the user initiates an action that would expand a node, the tree control sends a
TVN_ITEMEXPANDING. After the item has expanded, the control sends the
TVN_ITEMEXPANDED message.

Practical Learning: Using Tree Control Messages



  1. On the dialog box, right-click the tree control and click Add Event Handler...

  2. In the Message Type list box, click TVN_SELCHANGED. In the Class List, make
    sure CCarInventory2Dlg is selected

  3. In the Function Handle Name, change the name to OnCarSelectedChange and click
    Add And Edit

  4. Implement the OnPaint and the OnCarSelectedChange events as follows:


void CCarInventory2Dlg::OnPaint()
{
CPaintDC dc(this); // device context for painting

// Change the car picture based on the selected tag
CTreeCtrl *pTree = reinterpret_cast<CTreeCtrl
*>(GetDlgItem(IDC_CAR_TREE));
HTREEITEM hTree = pTree->GetSelectedItem();

CString ItemSelected;
CBitmap Bmp;

ItemSelected = pTree->GetItemText(hTree);

if( ItemSelected == "BH-733" )
Free download pdf