How to handle KEY_DOWN and VK_SPACE bar messages in a TreeCntrl

Posted on 2005-04-12
Last Modified: 2013-11-20
I have a tree control in a dialog with check boxes. I am handling NM_CLICK when the
      checkbox is changed. Now i need to be able to support accesiblitiy as well, so i have
      to handle TVN_KEYDOWN messages to capture VK_SPACE and VK_RETURN.
      The problem that i have is, i am not able to get a valid HTREEITEM.  How to
      get the valid one ?

            LPNMTVKEYDOWN pTVKeyDown = reinterpret_cast<LPNMTVKEYDOWN>(pNMHDR);
            if( ( pTVKeyDown->wVKey == VK_RETURN ) ||
             ( pTVKeyDown->wVKey == VK_SPACE  ))
                  POINT pt;
                  UINT uFlags = 0;
                  HTREEITEM hItemNew = m_treeCtrl.HitTest(pt,&uFlags);
                  m_Isdirty = TRUE;
                  OnClickAgentTree(pNMHDR, pResult);        

This code however does not work.... Any thoughts on this one ....

Thanks in advance            
Question by:sjcu
    LVL 43

    Expert Comment

    There is no guarantee the mouse cursor is where the focus is so I would avoid the GetCursorPos.

    You can just query the tree for what item has the selection as this code snippet shows
          HTREEITEM hItem = GetSelectedItem();
          if(hItem == NULL)
                return NULL;      //Nothing selected

    Author Comment

    Thanks. It works. I also need to get the pFlags.... As in my code i have a check for (uFlags & TVHT_ONITEM).
    LVL 43

    Accepted Solution

    If you are using the keyboard then the check for TVHT_ONITEM is not required (or do I misunderstand what you are wanting to achieve with the keyboard support)

    Author Comment

    Thanks for the fast response

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Expand LInux Boot partition remotly 3 71
    Named range not carried over 10 53
    move a line in eclipse 3 45
    EvenOdd challenge 10 69
    Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
    Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now