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

Howdy, Stranger!

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

Categories

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.