Win32 Tree-View Notifications

When a user event occurs on a TreeView control, TreeView control notifies the parent window with WM_NOTIFY message in order to handle the event. The following code shows all possible notifications that TreeView control generates. You need to cast the LPARAM of WM_NOTIFY into an appropriate struct type for each notification. LPARAM of WM_NOTIFY contains what event is triggered on the TreeView, and the parameters to handle the event properly.


// handle WM_NOTIFY message
int notify(WPARAM wParam, LPARAM lParam)
{
    // first cast lParam to NMHDR* to know what event is
    NMHDR* nmhdr = (NMHDR*)lParam;

    // TreeView notifications start with TVN_
    switch(nmhdr->code)
    {
    // drag-and-drop operation has begun
    case TVN_BEGINDRAG:
        // cast again lParam to NMTREEVIEW*
        break;

    // drag-and-drop operation using right mouse button has begun
    case TVN_BEGINRDRAG:
        break;

    // label editing has begun
    case TVN_BEGINLABELEDIT:
        // cast again lParam to NMTVDISPINFO*
        break;

    // label editing has ended
    case TVN_ENDLABELEDIT:
        // cast again lParam to NMTVDISPINFO*
        break;

    // an item has been deleted
    case TVN_DELETEITEM:
        break;

    // TreeView needs info(such as item text) to display an item
    case TVN_GETDISPINFO:
        break;

    // parent window must update the item information
    case TVN_SETDISPINFO:
        break;

    // list of items was expanded or collapsed
    case TVN_ITEMEXPANDED:
        break;

    // list of items are about to be expanded or collapsed
    case TVN_ITEMEXPANDING:
        break;

    // a keyboard event has occurred
    case TVN_KEYDOWN:
        // When the TreeView control is contained in a dialog box,
        // IsDialogMessage() processes the ESC and ENTER keystrokes and
        // does not pass them on to the edit control that is created by
        // the TreeView control. The result is that the keystrokes have
        // no effect.
        // Cast again lParam to NMTVKEYDOWN*
        break;

    // the item selection has changed
    case TVN_SELCHANGED:
        break;

    // the item selection is about to change
    case TVN_SELCHANGING:
        break;

    default:
        break;
    }

    return 0;

}
←Back