구마왕
구마왕

Reputation: 498

MFC) Making TreeCtrl loses its item focus

I have used TVN_SELCHANGED message to find out what user select in item tree (Menu).

However, if user continually click same item, that message does not occur.

I want treeCtrl to lose its item selection for occurring TVN_SELCHANGED. (In other words, I want to make event happened even if user click same item consecutively)

How do I do that?

Upvotes: 1

Views: 549

Answers (2)

Rohit Somawanshi
Rohit Somawanshi

Reputation: 21

The only time the TreeCtrl will get notified when an item is selected is: TVN_SELCHANGE. In case of same selection, this won't help. But there is another way to get notified. Add PreTranslateMessage command in your dialog class where TreeCtrl is used and add the code written below.

//---------------------------------------------------------------------------

BOOL MyDlgClass::PreTranslateMessage(MSG* pMsg)
{
    UINT msgValue = pMsg->message;
    //here I have compared L button down event, you can use any 
    //mouse/keyboard event that you want to compare.
    if (msgValue == WM_LBUTTONDOWN)
    {
        CPoint point;
        point.x = (int)(short)LOWORD(pMsg->lParam);
        point.y = (int)(short)HIWORD(pMsg->lParam);

        OnLButtonDown(pMsg->message, point);
    }
}

void MyDlgClass::OnLButtonDown(UINT nType, CPoint point)
{
    UINT uFlags;
    HTREEITEM hItem = m_treeCtrl.HitTest(point, &uFlags);

    if ((hItem != NULL) && (TVHT_ONITEMBUTTON & uFlags))
    {
        return;
    }
    //TVHT_ONITEMBUTTON detects if user has clicked + or - button of tree 
    //view.
    //Add code to perform your operations on hItem.
    

}

Upvotes: 1

xMRi
xMRi

Reputation: 15375

TVN_SELCHANGE will not help. Nothing is changed, so the notification isn't sent. Even it makes no sense for me. What should a UI do, if a user clicks on an already selected item? Nothing... I would guess.

If you want to handle this, you have to do it by yourself.

  • You can use WM_LBUTTONDOWN or NM_CLICK, to track the click.
  • Than use TVM_HITTEST to check what was clicked by the user.
  • Now you can compare the current selection (TVM_GETNEXTITEM and check for TVGN_CARET)
  • compare old and new selection.
  • After all, pass the click to the default handler.

Upvotes: 2

Related Questions