Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to Change Focus on Tree View with Mouse Click

How to Change Focus on Tree View with Mouse Click

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelptutorialquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stuck At Zero
    wrote on last edited by
    #1

    The problem is I have 8 root nodes. By default the program is focused on the 1st root node. Whenever I click on any other root node (or their descendants), the program is still indexing the 1st root node. The reason seems to be how one can highlight a node via the keyboard arrow keys. If I use the keyboard to highlight a different node, then the focus is changed. Mouse clicks seem to do absolutely nothing with changing the focus to a different node. I am wondering how I can force my app to change whatever node it is currently focused on to the one the user specifies by left-clicking with their mouse. The "mouse click" focus is currently non-existent, and the keyboard focus is what is driving things which is what I do not want.

    BOOL CViewFilter::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
    {
    CDialog::OnNotify(wParam, lParam, pResult);
    // TODO: Add your specialized code here and/or call the base class
    if (wParam == IDC_TREE1)
    {
    NMHDR *pHdr = (NMHDR *)lParam;

    	if (pHdr->code == NM\_CLICK)
    	{
    		HTREEITEM hItem = m\_filterTreeCtrl.GetSelectedItem();  // Keyboard Highlighted Selection... Not Mouse Clicked Node
    
    		if ((hItem != 0) && (m\_filterTreeCtrl.GetCheck(hItem) == 0))
    		{
    			// Deselect All Subsequent Children Nodes
    			//SetDecendentNodesOff(hItem);
    			SetDecendentNodesOn(hItem);  // Backwards due to focused node not changing state immediately after click
    		}
    		else if ((hItem != 0) && (m\_filterTreeCtrl.GetCheck(hItem) == 1))
    		{
    			// Select All Subsequent Children Nodes
    			//SetDecendentNodesOn(hItem);
    			SetDecendentNodesOff(hItem);// Backwards due to focused node not changing state immediately after click
    		}
    	}
    }
    
    return CDialog::OnNotify(wParam, lParam, pResult);
    

    }

    M 1 Reply Last reply
    0
    • S Stuck At Zero

      The problem is I have 8 root nodes. By default the program is focused on the 1st root node. Whenever I click on any other root node (or their descendants), the program is still indexing the 1st root node. The reason seems to be how one can highlight a node via the keyboard arrow keys. If I use the keyboard to highlight a different node, then the focus is changed. Mouse clicks seem to do absolutely nothing with changing the focus to a different node. I am wondering how I can force my app to change whatever node it is currently focused on to the one the user specifies by left-clicking with their mouse. The "mouse click" focus is currently non-existent, and the keyboard focus is what is driving things which is what I do not want.

      BOOL CViewFilter::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
      {
      CDialog::OnNotify(wParam, lParam, pResult);
      // TODO: Add your specialized code here and/or call the base class
      if (wParam == IDC_TREE1)
      {
      NMHDR *pHdr = (NMHDR *)lParam;

      	if (pHdr->code == NM\_CLICK)
      	{
      		HTREEITEM hItem = m\_filterTreeCtrl.GetSelectedItem();  // Keyboard Highlighted Selection... Not Mouse Clicked Node
      
      		if ((hItem != 0) && (m\_filterTreeCtrl.GetCheck(hItem) == 0))
      		{
      			// Deselect All Subsequent Children Nodes
      			//SetDecendentNodesOff(hItem);
      			SetDecendentNodesOn(hItem);  // Backwards due to focused node not changing state immediately after click
      		}
      		else if ((hItem != 0) && (m\_filterTreeCtrl.GetCheck(hItem) == 1))
      		{
      			// Select All Subsequent Children Nodes
      			//SetDecendentNodesOn(hItem);
      			SetDecendentNodesOff(hItem);// Backwards due to focused node not changing state immediately after click
      		}
      	}
      }
      
      return CDialog::OnNotify(wParam, lParam, pResult);
      

      }

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Looks like you changed the sample code I sent you in a previous post :) You're calling the base class OnNotify twice (BAD!). Always look at the docs for message handlers and return the appropriate value. In this case, you should return non-zero if you handle the message, otherwise call the base class...

      BOOL CViewFilter::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
      {
      if (wParam == IDC_TREE1)
      {
      NMHDR *pHdr = (NMHDR *)lParam;

      	if (pHdr->code == NM\_CLICK)
      	{
      		HTREEITEM hItem = m\_filterTreeCtrl.GetSelectedItem();  // Keyboard Highlighted Selection... Not Mouse Clicked Node
      
      		if ((hItem != 0) && (m\_filterTreeCtrl.GetCheck(hItem) == 0))
      		{
      			// Deselect All Subsequent Children Nodes
      			//SetDecendentNodesOff(hItem);
      			SetDecendentNodesOn(hItem);  // Backwards due to focused node not changing state immediately after click
      		}
      		else if ((hItem != 0) && (m\_filterTreeCtrl.GetCheck(hItem) == 1))
      		{
      			// Select All Subsequent Children Nodes
      			//SetDecendentNodesOn(hItem);
      			SetDecendentNodesOff(hItem);// Backwards due to focused node not changing state immediately after click
      		}
      
      		**return TRUE;**
      	}
      }
      
      return CDialog::OnNotify(wParam, lParam, pResult);
      

      }

      I still don't think NM_CLICK is the notification you're looking for... Have you seen the Tree View Control Docs[^]?

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      S 1 Reply Last reply
      0
      • M Mark Salsbery

        Looks like you changed the sample code I sent you in a previous post :) You're calling the base class OnNotify twice (BAD!). Always look at the docs for message handlers and return the appropriate value. In this case, you should return non-zero if you handle the message, otherwise call the base class...

        BOOL CViewFilter::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
        {
        if (wParam == IDC_TREE1)
        {
        NMHDR *pHdr = (NMHDR *)lParam;

        	if (pHdr->code == NM\_CLICK)
        	{
        		HTREEITEM hItem = m\_filterTreeCtrl.GetSelectedItem();  // Keyboard Highlighted Selection... Not Mouse Clicked Node
        
        		if ((hItem != 0) && (m\_filterTreeCtrl.GetCheck(hItem) == 0))
        		{
        			// Deselect All Subsequent Children Nodes
        			//SetDecendentNodesOff(hItem);
        			SetDecendentNodesOn(hItem);  // Backwards due to focused node not changing state immediately after click
        		}
        		else if ((hItem != 0) && (m\_filterTreeCtrl.GetCheck(hItem) == 1))
        		{
        			// Select All Subsequent Children Nodes
        			//SetDecendentNodesOn(hItem);
        			SetDecendentNodesOff(hItem);// Backwards due to focused node not changing state immediately after click
        		}
        
        		**return TRUE;**
        	}
        }
        
        return CDialog::OnNotify(wParam, lParam, pResult);
        

        }

        I still don't think NM_CLICK is the notification you're looking for... Have you seen the Tree View Control Docs[^]?

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        S Offline
        S Offline
        Stuck At Zero
        wrote on last edited by
        #3

        Yes, I did change the code around. :) Someone here at work suggested I use a call to OnNotify from the base class because since I was overriding OnNotify, the call to the overridden function would handle all the other stuff I'm not handling. I've tried using other notifications and nothing seems to work. The other thing I'm trying to do away with is cutting the umbilical cord to the default indexing of nodes by way of the rectangular region which pops up when you use the arrow keys on the keyboard. If I don't use NM_CLICK, the only other thing I can do is the node changing states, but I think the above problem is compounding the issue I'm observing.

        M 1 Reply Last reply
        0
        • S Stuck At Zero

          Yes, I did change the code around. :) Someone here at work suggested I use a call to OnNotify from the base class because since I was overriding OnNotify, the call to the overridden function would handle all the other stuff I'm not handling. I've tried using other notifications and nothing seems to work. The other thing I'm trying to do away with is cutting the umbilical cord to the default indexing of nodes by way of the rectangular region which pops up when you use the arrow keys on the keyboard. If I don't use NM_CLICK, the only other thing I can do is the node changing states, but I think the above problem is compounding the issue I'm observing.

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Stuck At Zero wrote:

          since I was overriding OnNotify, the call to the overridden function would handle all the other stuff I'm not handling

          Right, but you were calling the base class BEFORE handling the notification and afterwards as well. You should also return TRUE when you handle a notification.

          Stuck At Zero wrote:

          I've tried using other notifications and nothing seems to work.

          It depends on what you want to do. There's a lot of more specific notifications for a tree view control that may be more appropriate than the generic NM_CLICK notification, which is sent when the user clicks anywhere on the control. That notification may come BEFORE the control changes the selected item, which will mess up your handler logic. In your previous post, you needed to load child nodes when an item was expanded. What else are you wanting to do? Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          S 1 Reply Last reply
          0
          • M Mark Salsbery

            Stuck At Zero wrote:

            since I was overriding OnNotify, the call to the overridden function would handle all the other stuff I'm not handling

            Right, but you were calling the base class BEFORE handling the notification and afterwards as well. You should also return TRUE when you handle a notification.

            Stuck At Zero wrote:

            I've tried using other notifications and nothing seems to work.

            It depends on what you want to do. There's a lot of more specific notifications for a tree view control that may be more appropriate than the generic NM_CLICK notification, which is sent when the user clicks anywhere on the control. That notification may come BEFORE the control changes the selected item, which will mess up your handler logic. In your previous post, you needed to load child nodes when an item was expanded. What else are you wanting to do? Mark

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            S Offline
            S Offline
            Stuck At Zero
            wrote on last edited by
            #5

            Okay, so returning true on the part I'm handling will do the trick while the other return does so for the base class. On the matter of NM_CLICK, I'm pretty much going straight down the list of notifications. It seems part, if not my whole problem could be that clicking on a node's checkbox does absolutely nothing with respect to where the TreeView thinks the focus is at. The focus seems to be whatever node text is highlighted (regardless of what checkbox to a node was clicked on). Is there something I can do to remedy this cumbersome process? A user will not want to click on a node's text and then click on its checkbox for proper operation.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups