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?
How to show an image in a static control when a treeview item is clicked in MFC?
- 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
- First of all, I'm very sorry, I was wrong ... you need to use TVN_SELCHANGED notifyRead 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()andvoid <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
- 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
- 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.
- 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 - Look at Wesley's post. The link was: http://www.functionx.com/visualc/applications/displaybitmap.htmYou should insert your code for picture drawing in TVN_ITEMCHANGED notify handler.
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
- 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 - First of all, I'm very sorry, I was wrong ... you need to use TVN_SELCHANGED notifyRead 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()andvoid <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
- 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 - You should addON_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 notifybetween BEGIN_MESSAGE_MAP(..., ...) ... END_MESSAGE_MAP()then create OnTreeSelChanged function in your classvoid <YourClass>::OnTreeSelChanged(NMHDR *pNMHDR, LRESULT *pResult)and add it's declaration to your class in h file.
- 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 - 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
- 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 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- 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. - Thanks For Your Suggestion Nancy :)
Pandi


