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 could I get the parent of an embedded control generically ?

How could I get the parent of an embedded control generically ?

Scheduled Pinned Locked Moved C / C++ / MFC
dockerhardwarehelpquestion
8 Posts 3 Posters 4 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
    SherTeks
    wrote on last edited by
    #1

    I have a dialog-based application that contains a combo box. I've subclassed the combo box like this :

    HBRUSH CSubClassedComboBox::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {

      if (nCtlColor == CTLCOLOR\_EDIT)
      {
         //\[ASCII 160\]\[ASCII 160\]\[ASCII 160\]Edit control
         if (m\_CmbEditBox.GetSafeHwnd() == NULL)
            m\_CmbEditBox.SubclassWindow(pWnd->GetSafeHwnd());
      }
      else if (nCtlColor == CTLCOLOR\_LISTBOX)
      {
    
         //ListBox control
    	  if (m\_CmbListBox == NULL)
    	  {
    		  m\_CmbListBox = new CListBoxEx;
    
    		  if (m\_CmbListBox->GetSafeHwnd() == NULL)
    		  {			  
    			  m\_CmbListBox->SubclassWindow(pWnd->GetSafeHwnd());
    		  }
    	  }
      }
    
    HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);
    
    return hbr;
    

    }

    m_CmbEditBox and m_CmbListBox are objects of CEdit and CListBoxEx(derived from CListBox) respectively and declared in SubClassedComboBox.h as the class CSubClassedComboBox's members. During run-time of my application, I pull down the list box and right-click on any of the list box items. My control reaches here :

    void CListBoxEx::OnContextMenu(CWnd* pWnd, CPoint point)
    {
    GetParent->TestFn();
    }

    Now, in CListBoxEx::OnContextMenu, I want to call a member function ( say TestFn() ) available in class CSubClassedComboBox. Please see the following code :

    void CListBoxEx::OnContextMenu(CWnd* pWnd, CPoint point)
    {
    ((CSubClassedComboBox *)GetParent())->TestFn(); //This works and TestFn() of CSubClassedComboBox is called

    GetParent())->TestFn();		//But this doesn't work
    

    }

    As in the comments, the second statement doesn't work. I don't want to cast the pointer returned by GetParent() to CSubClassedComboBox * bcoz this wouldn't be in generic sense. By this, I mean to say that GetParent() should be able to give the parent pointer which is CSubClassedComboBox * in this case, but could be anything else (eg. another container window that contains the CListBoxEx object). In summary, GetParent()->TestFn(); should call TestFn() of CSubClassedComboBox as per the requirement above. But GetParent())->TestFn(); should be able to call TestFn() of say CSubClassedSomeContainerWindow if m_ListBox is a member of CSubClassedSomeContainerWindow So, I am looking for a solution that is generic, to find the parent. Any help would be appreciated. Thanks in advance.

    N N 2 Replies Last reply
    0
    • S SherTeks

      I have a dialog-based application that contains a combo box. I've subclassed the combo box like this :

      HBRUSH CSubClassedComboBox::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
      {

        if (nCtlColor == CTLCOLOR\_EDIT)
        {
           //\[ASCII 160\]\[ASCII 160\]\[ASCII 160\]Edit control
           if (m\_CmbEditBox.GetSafeHwnd() == NULL)
              m\_CmbEditBox.SubclassWindow(pWnd->GetSafeHwnd());
        }
        else if (nCtlColor == CTLCOLOR\_LISTBOX)
        {
      
           //ListBox control
      	  if (m\_CmbListBox == NULL)
      	  {
      		  m\_CmbListBox = new CListBoxEx;
      
      		  if (m\_CmbListBox->GetSafeHwnd() == NULL)
      		  {			  
      			  m\_CmbListBox->SubclassWindow(pWnd->GetSafeHwnd());
      		  }
      	  }
        }
      
      HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);
      
      return hbr;
      

      }

      m_CmbEditBox and m_CmbListBox are objects of CEdit and CListBoxEx(derived from CListBox) respectively and declared in SubClassedComboBox.h as the class CSubClassedComboBox's members. During run-time of my application, I pull down the list box and right-click on any of the list box items. My control reaches here :

      void CListBoxEx::OnContextMenu(CWnd* pWnd, CPoint point)
      {
      GetParent->TestFn();
      }

      Now, in CListBoxEx::OnContextMenu, I want to call a member function ( say TestFn() ) available in class CSubClassedComboBox. Please see the following code :

      void CListBoxEx::OnContextMenu(CWnd* pWnd, CPoint point)
      {
      ((CSubClassedComboBox *)GetParent())->TestFn(); //This works and TestFn() of CSubClassedComboBox is called

      GetParent())->TestFn();		//But this doesn't work
      

      }

      As in the comments, the second statement doesn't work. I don't want to cast the pointer returned by GetParent() to CSubClassedComboBox * bcoz this wouldn't be in generic sense. By this, I mean to say that GetParent() should be able to give the parent pointer which is CSubClassedComboBox * in this case, but could be anything else (eg. another container window that contains the CListBoxEx object). In summary, GetParent()->TestFn(); should call TestFn() of CSubClassedComboBox as per the requirement above. But GetParent())->TestFn(); should be able to call TestFn() of say CSubClassedSomeContainerWindow if m_ListBox is a member of CSubClassedSomeContainerWindow So, I am looking for a solution that is generic, to find the parent. Any help would be appreciated. Thanks in advance.

      N Offline
      N Offline
      Nibu babu thomas
      wrote on last edited by
      #2

      SherTeks wrote:

      So, I am looking for a solution that is generic, to find the parent.

      If I understood your question properly... Define a message for your parent class, for e.g. WM_TEST_FN. So all the parent needs to do is define a handler for this message. So if the handler is there then the function will be called. So then you can use SendMessage to send this message.

      Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

      N S 2 Replies Last reply
      0
      • S SherTeks

        I have a dialog-based application that contains a combo box. I've subclassed the combo box like this :

        HBRUSH CSubClassedComboBox::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
        {

          if (nCtlColor == CTLCOLOR\_EDIT)
          {
             //\[ASCII 160\]\[ASCII 160\]\[ASCII 160\]Edit control
             if (m\_CmbEditBox.GetSafeHwnd() == NULL)
                m\_CmbEditBox.SubclassWindow(pWnd->GetSafeHwnd());
          }
          else if (nCtlColor == CTLCOLOR\_LISTBOX)
          {
        
             //ListBox control
        	  if (m\_CmbListBox == NULL)
        	  {
        		  m\_CmbListBox = new CListBoxEx;
        
        		  if (m\_CmbListBox->GetSafeHwnd() == NULL)
        		  {			  
        			  m\_CmbListBox->SubclassWindow(pWnd->GetSafeHwnd());
        		  }
        	  }
          }
        
        HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);
        
        return hbr;
        

        }

        m_CmbEditBox and m_CmbListBox are objects of CEdit and CListBoxEx(derived from CListBox) respectively and declared in SubClassedComboBox.h as the class CSubClassedComboBox's members. During run-time of my application, I pull down the list box and right-click on any of the list box items. My control reaches here :

        void CListBoxEx::OnContextMenu(CWnd* pWnd, CPoint point)
        {
        GetParent->TestFn();
        }

        Now, in CListBoxEx::OnContextMenu, I want to call a member function ( say TestFn() ) available in class CSubClassedComboBox. Please see the following code :

        void CListBoxEx::OnContextMenu(CWnd* pWnd, CPoint point)
        {
        ((CSubClassedComboBox *)GetParent())->TestFn(); //This works and TestFn() of CSubClassedComboBox is called

        GetParent())->TestFn();		//But this doesn't work
        

        }

        As in the comments, the second statement doesn't work. I don't want to cast the pointer returned by GetParent() to CSubClassedComboBox * bcoz this wouldn't be in generic sense. By this, I mean to say that GetParent() should be able to give the parent pointer which is CSubClassedComboBox * in this case, but could be anything else (eg. another container window that contains the CListBoxEx object). In summary, GetParent()->TestFn(); should call TestFn() of CSubClassedComboBox as per the requirement above. But GetParent())->TestFn(); should be able to call TestFn() of say CSubClassedSomeContainerWindow if m_ListBox is a member of CSubClassedSomeContainerWindow So, I am looking for a solution that is generic, to find the parent. Any help would be appreciated. Thanks in advance.

        N Offline
        N Offline
        Naveen
        wrote on last edited by
        #3

        One method is to send a USER message to the parent window and you should write a ahndler for that message in the parent window instead of TestFn(). And offourse it is generic.

        nave [OpenedFileFinder]

        1 Reply Last reply
        0
        • N Nibu babu thomas

          SherTeks wrote:

          So, I am looking for a solution that is generic, to find the parent.

          If I understood your question properly... Define a message for your parent class, for e.g. WM_TEST_FN. So all the parent needs to do is define a handler for this message. So if the handler is there then the function will be called. So then you can use SendMessage to send this message.

          Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

          N Offline
          N Offline
          Naveen
          wrote on last edited by
          #4

          ok. You won this time :(

          nave [OpenedFileFinder]

          N 1 Reply Last reply
          0
          • N Naveen

            ok. You won this time :(

            nave [OpenedFileFinder]

            N Offline
            N Offline
            Nibu babu thomas
            wrote on last edited by
            #5

            Naveen wrote:

            ok. You won this time [Frown]

            ;)

            Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

            1 Reply Last reply
            0
            • N Nibu babu thomas

              SherTeks wrote:

              So, I am looking for a solution that is generic, to find the parent.

              If I understood your question properly... Define a message for your parent class, for e.g. WM_TEST_FN. So all the parent needs to do is define a handler for this message. So if the handler is there then the function will be called. So then you can use SendMessage to send this message.

              Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

              S Offline
              S Offline
              SherTeks
              wrote on last edited by
              #6

              Thanks for your reply. May be I need to make my question a bit more clearer. Just assume that my dialog application has 2 controls : 1) a Combo Box and 2) another container control similar to combo box (but not exactly combo box). The common trait between the two controls is that they both embed the list box object derived from CListBoxEx. Now, I could right-click on any one list box at a particular point of time during run-time. The right-click would bring the control to the OnContextMenu of CListBoxEx::OnContextMenu. In this place, I should be able to identify the parent ie. either control 1) or 2) using GetParent(). But the only condition is that I shouldn't cast the pointer returned by GetParent() to any of the parent control pointers ie. GetParent()->TestFn() must call the TestFn() member function of the appropriate control of which the list box was right-clicked. Thanks

              N 1 Reply Last reply
              0
              • S SherTeks

                Thanks for your reply. May be I need to make my question a bit more clearer. Just assume that my dialog application has 2 controls : 1) a Combo Box and 2) another container control similar to combo box (but not exactly combo box). The common trait between the two controls is that they both embed the list box object derived from CListBoxEx. Now, I could right-click on any one list box at a particular point of time during run-time. The right-click would bring the control to the OnContextMenu of CListBoxEx::OnContextMenu. In this place, I should be able to identify the parent ie. either control 1) or 2) using GetParent(). But the only condition is that I shouldn't cast the pointer returned by GetParent() to any of the parent control pointers ie. GetParent()->TestFn() must call the TestFn() member function of the appropriate control of which the list box was right-clicked. Thanks

                N Offline
                N Offline
                Nibu babu thomas
                wrote on last edited by
                #7

                SherTeks wrote:

                But the only condition is that I shouldn't cast the pointer returned by GetParent() to any of the parent control pointers ie. GetParent()->TestFn() must call the TestFn() member function of the appropriate control of which the list box was right-clicked.

                Well you don't need any casting right if you are using messages. Take a look... GetParent()->SendMessage( WM_TEST_FN, SomewParam, SomelParam ); Now all the parent class needs to do is add a message map entry to handle this event. BEGIN_MESSAGE_MAP(...) ON_MESSAGE( WM_TEST_FN, TestFn) END_MESSAGE_MAP() TestFn signature should look like this... LRESULT TestFn( WPARAM wParam, LPARAM lParam );

                Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

                S 1 Reply Last reply
                0
                • N Nibu babu thomas

                  SherTeks wrote:

                  But the only condition is that I shouldn't cast the pointer returned by GetParent() to any of the parent control pointers ie. GetParent()->TestFn() must call the TestFn() member function of the appropriate control of which the list box was right-clicked.

                  Well you don't need any casting right if you are using messages. Take a look... GetParent()->SendMessage( WM_TEST_FN, SomewParam, SomelParam ); Now all the parent class needs to do is add a message map entry to handle this event. BEGIN_MESSAGE_MAP(...) ON_MESSAGE( WM_TEST_FN, TestFn) END_MESSAGE_MAP() TestFn signature should look like this... LRESULT TestFn( WPARAM wParam, LPARAM lParam );

                  Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

                  S Offline
                  S Offline
                  SherTeks
                  wrote on last edited by
                  #8

                  Correction : In my previous post, please consider m_CmbEditBox as of type CEditEx and not of type CEdit. My exact requirement is like this : On right-clicking the list box item the control reaches CListBoxEx::OnContextMenu. Please see the comment :

                  void CListBoxEx::OnContextMenu(CWnd* pWnd, CPoint point)
                  {
                  /* The GetParent() here returns CTempWnd * (obtained while debugging).
                  I am expecting the return pointer to be of type CSubClassedComboBox
                  bcoz the subclassed combo box of type CSubClassedComboBox is the
                  list box's parent */
                  GetParent()->TestFn();
                  }

                  What am I doing wrong for list box bcoz the same mechanism for the child edit box of the CSubClassedComboBox works fine ie.

                  void CEditEx::OnContextMenu(CWnd* pWnd, CPoint point)
                  {
                  /* The GetParent() here returns CSubClassedComboBox * (obtained while debugging) which is correct and is the expected result. */
                  GetParent()->TestFn();
                  }

                  Is there anything to do with the temporary creation of the list box which is not the case with edit box ? If so, how could I acquire the desired result ? Thanks

                  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