Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

MFC - TreeCtrl problems, clicking an item in a Tree control

Hi!

I'm sitting right here and have a problem... how

to catch a mouse click on a TreeCtrl item??



a source code exampel would be great!

but it's ok with words to describe how to do it.



THANKS!



Using VicualC++ 6.0


Comments

  • I cant understand what are you looking for! Sorry, but isnt NM_XXX (NM_CLICK etc) enough? Or what are you trying to do exactly?



    : Hi!

    : I'm sitting right here and have a problem... how

    : to catch a mouse click on a TreeCtrl item??

    :

    : a source code exampel would be great!

    : but it's ok with words to describe how to do it.

    :

    : THANKS!

    :

    : Using VicualC++ 6.0

    :






  • KMS



    I don't know how to erite the code right!

    If I click one time on TreCtrl and the choose NM_CLICK

    in a pop domn menu, then I can edit the code for

    clicking the TREE control, not the items.... how to do so it

    knows it is an item thats was clicked.



    hope that will work..... :)


  • I got it now!



    What you need to do is use WM_NOTIFY (Which of course is sent to the parent of the control ) and look for TVN_SELCHANGED.



    For example, say you have IDC_TREE1 on CMyDlg, you would do:



    BOOL CMyDlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)

    {

    LPNM_TREEVIEW p = (LPNM_TREEVIEW) lParam;

    if( wParam == IDC_TREE1 && p->hdr.code == TVN_SELCHANGED )

    {

    // p->itemNew.hItem holds the handle of the new item to be selected.



    // we want to show the text in a message box, so

    CHAR szBuffer[ 1024 ];



    TV_ITEM Item;

    ZeroMemory( &Item, sizeof( Item ) );

    Item.mask = TVIF_TEXT;

    Item.hItem = p->itemNew.hItem;

    Item.pszText = szBuffer;

    Item.cchTextMax = sizeof( szBuffer );

    m_MyTree.GetItem( &Item );

    AfxMessageBox( szBuffer );

    }



    return CDialog::OnNotify(wParam, lParam, pResult);

    }





    : KMS

    :

    : I don't know how to erite the code right!

    : If I click one time on TreCtrl and the choose NM_CLICK

    : in a pop domn menu, then I can edit the code for

    : clicking the TREE control, not the items.... how to do so it

    : knows it is an item thats was clicked.

    :

    : hope that will work..... :)

    :






  • I see, but it is a few things I don't realy

    understand:

    1. LPNM_TREEVIEW

    2. hdr (where is it declared, or is it a standard thing?)

    3. code(where is it declared, or is it a standard thing?)



    where did you learned this stuff??



    with a book? the web?? pleas tell me!



    now I promise i wont send any more messages! :)





    : I got it now!

    :

    : What you need to do is use WM_NOTIFY (Which of course is sent to the parent of the control ) and look for TVN_SELCHANGED.

    :

    : For example, say you have IDC_TREE1 on CMyDlg, you would do:

    :

    : BOOL CMyDlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)

    : {

    : LPNM_TREEVIEW p = (LPNM_TREEVIEW) lParam;

    : if( wParam == IDC_TREE1 && p->hdr.code == TVN_SELCHANGED )

    : {

    : // p->itemNew.hItem holds the handle of the new item to be selected.

    :

    : // we want to show the text in a message box, so

    : CHAR szBuffer[ 1024 ];

    :

    : TV_ITEM Item;

    : ZeroMemory( &Item, sizeof( Item ) );

    : Item.mask = TVIF_TEXT;

    : Item.hItem = p->itemNew.hItem;

    : Item.pszText = szBuffer;

    : Item.cchTextMax = sizeof( szBuffer );

    : m_MyTree.GetItem( &Item );

    : AfxMessageBox( szBuffer );

    : }

    :

    : return CDialog::OnNotify(wParam, lParam, pResult);

    : }

    :

    :

    : : KMS

    : :

    : : I don't know how to erite the code right!

    : : If I click one time on TreCtrl and the choose NM_CLICK

    : : in a pop domn menu, then I can edit the code for

    : : clicking the TREE control, not the items.... how to do so it

    : : knows it is an item thats was clicked.

    : :

    : : hope that will work..... :)

    : :

    :

    :

    :






  • I see, but it is a few things I don't realy

    understand:

    1. LPNM_TREEVIEW

    2. hdr (where is it declared, or is it a standard thing?)

    3. code(where is it declared, or is it a standard thing?)



    where did you learned this stuff??



    with a book? the web?? pleas tell me!



    now I promise i wont send any more messages! :)





    : I got it now!

    :

    : What you need to do is use WM_NOTIFY (Which of course is sent to the parent of the control ) and look for TVN_SELCHANGED.

    :

    : For example, say you have IDC_TREE1 on CMyDlg, you would do:

    :

    : BOOL CMyDlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)

    : {

    : LPNM_TREEVIEW p = (LPNM_TREEVIEW) lParam;

    : if( wParam == IDC_TREE1 && p->hdr.code == TVN_SELCHANGED )

    : {

    : // p->itemNew.hItem holds the handle of the new item to be selected.

    :

    : // we want to show the text in a message box, so

    : CHAR szBuffer[ 1024 ];

    :

    : TV_ITEM Item;

    : ZeroMemory( &Item, sizeof( Item ) );

    : Item.mask = TVIF_TEXT;

    : Item.hItem = p->itemNew.hItem;

    : Item.pszText = szBuffer;

    : Item.cchTextMax = sizeof( szBuffer );

    : m_MyTree.GetItem( &Item );

    : AfxMessageBox( szBuffer );

    : }

    :

    : return CDialog::OnNotify(wParam, lParam, pResult);

    : }

    :

    :

    : : KMS

    : :

    : : I don't know how to erite the code right!

    : : If I click one time on TreCtrl and the choose NM_CLICK

    : : in a pop domn menu, then I can edit the code for

    : : clicking the TREE control, not the items.... how to do so it

    : : knows it is an item thats was clicked.

    : :

    : : hope that will work..... :)

    : :

    :

    :

    :






  • NM_TREEVIEW is in commctrl.h, hdr code etc are members of this structure.



    Learned from books of course! Inside Visual C++, is a good one. Also Using Visual C++ although it spends alot of time on the beginning stuff.



    MSDN CD's is a must also, but I'm sure you have them, I have the Sep 2000 ver on DVD, its fast! I understand that this only comes with MSDN subscription (not free), maybe you can find a friend who has it.



    You are welcome to send new messages!



    : I see, but it is a few things I don't realy

    : understand:

    : 1. LPNM_TREEVIEW

    : 2. hdr (where is it declared, or is it a standard thing?)

    : 3. code(where is it declared, or is it a standard thing?)

    :

    : where did you learned this stuff??

    :

    : with a book? the web?? pleas tell me!

    :

    : now I promise i wont send any more messages! :)

    :

    :

    : : I got it now!

    : :

    : : What you need to do is use WM_NOTIFY (Which of course is sent to the parent of the control ) and look for TVN_SELCHANGED.

    : :

    : : For example, say you have IDC_TREE1 on CMyDlg, you would do:

    : :

    : : BOOL CMyDlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)

    : : {

    : : LPNM_TREEVIEW p = (LPNM_TREEVIEW) lParam;

    : : if( wParam == IDC_TREE1 && p->hdr.code == TVN_SELCHANGED )

    : : {

    : : // p->itemNew.hItem holds the handle of the new item to be selected.

    : :

    : : // we want to show the text in a message box, so

    : : CHAR szBuffer[ 1024 ];

    : :

    : : TV_ITEM Item;

    : : ZeroMemory( &Item, sizeof( Item ) );

    : : Item.mask = TVIF_TEXT;

    : : Item.hItem = p->itemNew.hItem;

    : : Item.pszText = szBuffer;

    : : Item.cchTextMax = sizeof( szBuffer );

    : : m_MyTree.GetItem( &Item );

    : : AfxMessageBox( szBuffer );

    : : }

    : :

    : : return CDialog::OnNotify(wParam, lParam, pResult);

    : : }

    : :

    : :

    : : : KMS

    : : :

    : : : I don't know how to erite the code right!

    : : : If I click one time on TreCtrl and the choose NM_CLICK

    : : : in a pop domn menu, then I can edit the code for

    : : : clicking the TREE control, not the items.... how to do so it

    : : : knows it is an item thats was clicked.

    : : :

    : : : hope that will work..... :)

    : : :

    : :

    : :

    : :

    :

    :

    :






Sign In or Register to comment.