Visual C++ Developer Center > Visual C++ Forums > Visual C++ General > How to show an image in a static control when a treeview item is clicked in MFC?
Ask a questionAsk a question
 

AnswerHow to show an image in a static control when a treeview item is clicked in MFC?

  • Monday, November 02, 2009 12:08 PMKarunagara Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi friends,

       I have a tree view control which contains set of items. If I click an item, an image should be displayed in static control (picture control).

    Help me to solve!

    Thanks
    Pandi

Answers

  • Tuesday, November 03, 2009 10:23 AMNikita Leontiev Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    First of all, I'm very sorry, I was wrong ... you need to use TVN_SELCHANGED notify

    Read info about "how to handle notify in MFC".
    Or you can create tree control in "design time", try to add notify handler and look what code ClassWizard will add.

    Code should look like this:

    BEGIN_MESSAGE_MAP(..., ...)
    ...
    ON_NOTIFY(TVN_SELCHANGED, IDC_TREE, OnTreeSelChanged)
    ...
    END_MESSAGE_MAP()

    and 

    void <YourClass>::OnTreeSelChanged(NMHDR *pNMHDR, LRESULT *pResult)
    {
    }
    • Proposed As Answer byNikita Leontiev Tuesday, November 03, 2009 12:33 PM
    • Marked As Answer byKarunagara Tuesday, November 03, 2009 12:37 PM
    •  
  • Tuesday, November 03, 2009 11:34 AMNikita Leontiev Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    You should use ID of your tree control. Look here to find out how you can specifiy tree control ID.
    • Marked As Answer byKarunagara Tuesday, November 03, 2009 12:14 PM
    •  

All Replies

  • Monday, November 02, 2009 12:15 PMNikita Leontiev Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    You can use TVN_ITEMCHANGED notify to determine when item is changing and use this topic to understand how to display image. It has sample for MFC provided by Wesley.
  • Monday, November 02, 2009 12:20 PMKarunagara Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    But there, you have mentioned, it is not MFC, win 32 API? I'm getting confused or misunderstood! I don't know!

    But, in my form, i have already drawn a picture (static ) control. Should I declare this TVN_ITEMCHANGED , inside Begin Message Map, am I right?

    And what method (function) should i use for writing definition mam?



    Pandi
  • Monday, November 02, 2009 12:54 PMNikita Leontiev Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    You should insert your code for picture drawing in TVN_ITEMCHANGED notify handler.
  • Monday, November 02, 2009 4:17 PMScott McPhillipsMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    But, in my form, i have already drawn a picture (static ) control. Should I declare this TVN_ITEMCHANGED , inside Begin Message Map, am I right?

    And what method (function) should i use for writing definition mam?

    You have asked several times how to add various message handlers.  Visual Studio adds message handlers for you!

    Select the control on the dialog template.  Right click and select Add Event Handler. All of the control's events will be listed. When you select the event you want Visual Studio adds it properly to the message map and adds an empty message handler function in your class.
    • Marked As Answer byKarunagara Tuesday, November 03, 2009 9:25 AM
    • Unmarked As Answer byKarunagara Tuesday, November 03, 2009 9:32 AM
    •  
  • Tuesday, November 03, 2009 9:33 AMKarunagara Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Yes, you're correct! But I didn't add a tree view control at design time. I'm creating at run time. That's what I'm asking you all!

    Guide me...

    Thanks

    Pandi
  • Tuesday, November 03, 2009 10:23 AMNikita Leontiev Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    First of all, I'm very sorry, I was wrong ... you need to use TVN_SELCHANGED notify

    Read info about "how to handle notify in MFC".
    Or you can create tree control in "design time", try to add notify handler and look what code ClassWizard will add.

    Code should look like this:

    BEGIN_MESSAGE_MAP(..., ...)
    ...
    ON_NOTIFY(TVN_SELCHANGED, IDC_TREE, OnTreeSelChanged)
    ...
    END_MESSAGE_MAP()

    and 

    void <YourClass>::OnTreeSelChanged(NMHDR *pNMHDR, LRESULT *pResult)
    {
    }
    • Proposed As Answer byNikita Leontiev Tuesday, November 03, 2009 12:33 PM
    • Marked As Answer byKarunagara Tuesday, November 03, 2009 12:37 PM
    •  
  • Tuesday, November 03, 2009 10:36 AMKarunagara Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    No Nikita! I didn't create the tree view control @ design time. You've already seen my code here!
    http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/658e33fd-c3ff-424e-841e-823240ef212d

    Now, tell me how to handle the events for tree view control. I tried like this. But no use! Guide me please nikita...

        ON_NOTIFY(TVN_SELCHANGED, mTreeviewCtrl, &CTreeviewExampleDlg::OnTvnSelchangedTree1)

    I need to click an item and it should show a picture in the picture box control. Showing picture is not a problem, you've already given a superb link here: http://www.functionx.com/visualc/applications/displaybitmap.htm

    You just tell me how to handle the tree view items!  And also, I can't understand the code when you shown in previous post! I tried to create a new project and draw a tree view control and double clicked it. It shows everything what you posted!

    But I can't use them directly in the dynamic treeview control nikita. Help me to solve!!!!!


    Thanks
    Pandi
  • Tuesday, November 03, 2009 10:47 AMNikita Leontiev Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    You should add

    ON_NOTIFY(TVN_SELCHANGED, IDC_TREE, OnTreeSelChanged)
    where 1 param is notify, 2 - ID of your tree ctrl (not mTreeviewCtrl), 3 - function in your class that will handle notify

    between BEGIN_MESSAGE_MAP(..., ...) ... END_MESSAGE_MAP()

    then create OnTreeSelChanged function in your class

    void <YourClass>::OnTreeSelChanged(NMHDR *pNMHDR, LRESULT *pResult)

    and add it's declaration to your class in h file.
  • Tuesday, November 03, 2009 11:13 AMKarunagara Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    IDC_TREE is pre-defined or user-defined name? Because I didn't specify this ID in my code, instead I've used mTreeviewCtrl! Also, I need to find out which item has been clicked.....

    I'm getting undefined IDC_TREE error!!!!!!!!!!!!!!!!!!!!!!!!!!
    ....
    Pandi
  • Tuesday, November 03, 2009 11:34 AMNikita Leontiev Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    You should use ID of your tree control. Look here to find out how you can specifiy tree control ID.
    • Marked As Answer byKarunagara Tuesday, November 03, 2009 12:14 PM
    •  
  • Tuesday, November 03, 2009 11:41 AMKarunagara Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    In that place, I've used as 1, shown here.

    mTreeviewCtrl->Create(WS_CHILD|WS_VISIBLE,CRect(10,30,170,180),this,1);

    Should i change as IDC_TREE instead 1?   Even though I'm getting the same error Nikita..........

    If I use 1, I'm getting error here!

    => void CTreeviewExampleDlg::OnTreeSelChanged(NMHDR *pNMHDR, LRESULT *pResult)
    {
        MessageBox(L"Hi");
    }

    and declaration too.... What wrong is there........???



    Pandi
  • Tuesday, November 03, 2009 12:14 PMKarunagara Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    In that place, I've used as 1, shown here.

    mTreeviewCtrl->Create(WS_CHILD|WS_VISIBLE,CRect(10,30,170,180),this,1);

    Should i change as IDC_TREE instead 1?   Even though I'm getting the same error Nikita..........

    If I use 1, I'm getting error here!

    => void CTreeviewExampleDlg::OnTreeSelChanged(NMHDR *pNMHDR, LRESULT *pResult)
    {
        MessageBox(L"Hi");
    }

    and declaration too.... What wrong is there........???



    Pandi

    ............................Closed this..................Topic..................

    Pandi
  • Wednesday, November 04, 2009 3:28 AMNancy ShaoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Pandi,

    Glad to hear that you have found solution form Nikita. Cheers. And I highly suggest you do not post two same questions in our forums. If you want to get quick response, you'd better make a good question name or update your thread.

    Thanks,
    Nancy
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Wednesday, November 04, 2009 5:24 AMKarunagara Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks For Your Suggestion Nancy  :)

    Pandi