Thread: Help with a tree

  1. #1
    Registered User Nova_Collision's Avatar
    Join Date
    Feb 2003
    Posts
    40

    Help with a tree

    I've been all over the 'net and this forum trying out stuff but I just can't get it. I used MSVC's resource editor to create a dialog with a tree control already in it. Unfortunately, I can't find ANYTHING telling me how to add content to it. Pretty much all I know is that the id for my tree control is IDC_DIRTREE
    When in doubt, empty your magazine - Murphy's Laws Of Combat #7

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981

  3. #3
    Registered User Nova_Collision's Avatar
    Join Date
    Feb 2003
    Posts
    40
    Yeah, that was pretty much the same as the MSDN tutorial in VS6. It didn't say anything about accessing a tree view that had been created in the resource editor, not to mention that I dunno what libraries they're using in that example and it didn't say anywhere. All I want is how to add an item to a treeview I created in MSVC's resource editor with the id ID_DIRTREE. I can figure it out beyond that once I get that working. I hope.
    When in doubt, empty your magazine - Murphy's Laws Of Combat #7

  4. #4
    Registered User Nova_Collision's Avatar
    Join Date
    Feb 2003
    Posts
    40
    Okay, I've made some progress, but the biggest thing I've learned is that the MFC has some whacked out stuff. I've figured out that I need to create the object, then assign it the resource, but that leaves me with some problems.

    The first is something that I dunno about yet, but maybe someone can help me understand. I've been using windows.h, but had to junk it in favor of afxcmn.h for the CTreeCtrl class, since you can't have both. I dunno if that means I need to rewrite all my dialog creation code, but that's where I'm at for that.

    The next is about the CTreeCtrl needing a CWnd parent object. In the MSDN class description, it says the most common CWnd parent is a CDialog object. I tried passing it one and the compiler tells me that it can't convert class "CDialog" to class "CWnd *", which leaves me really confused because according to the MSDN libraries, CDialog IS a CWnd object. At least, I would assume, since they're telling me to pass it as one.

    Thirdly, when I try to initialize my CDialog object, it tells me I can't use the assignment operator "=" with CDialog. How do I create an object if I can't call it's constructor?

    Anyway, I've been struggling with this a lot over the pass 48 hours, so believe me when I say that I'm not just posting out of laziness. I really need someone to explain to me how this works because I just don't get it.
    When in doubt, empty your magazine - Murphy's Laws Of Combat #7

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Read this.
    If you still want to get into MFC after reading that, read this.

    It'll be a long, hard, and frustrating path learning those two subjects at the same time, especially using this forum alone.

    gg

  6. #6
    Registered User Nova_Collision's Avatar
    Join Date
    Feb 2003
    Posts
    40
    Uh....okay. Well, since I can't afford over $100 (CDN) for books, does that mean that there is no way for me to add in some kind of directory browser to my program? If not, I guess that's that and I'll have to abandon this for my project. It makes no sense to me why MS would make it this hard to write code for the windows API.
    When in doubt, empty your magazine - Murphy's Laws Of Combat #7

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You can get the HWND of a dialog control using:
    Code:
    hwndTree = GetDlgItem(hDlg, ID_DIRTREE);
    You can then use the normal functions for tree-view controls:
    Code:
    TreeView_InsertItem(hwndTree, &tvi);
    Alternatively, you can use SendDlgItemMessage in place of SendMessage:
    Code:
    SendDlgItemMessage(hDlg, ID_DIRTREE, TVM_DELETEITEM, 0, (LPARAM) hItem);
    With this knowledge, you should be able to convert the MSDN sample code to your needs.

    This applies to all dialog controls, not just tree-views.

    I don't know much about MFC but:

    >> tried passing it one and the compiler tells me that it can't convert class "CDialog" to class "CWnd *",<<

    No, but it can probably convert a pointer to a CDialog object to a pointer to a CWnd object.
    Last edited by anonytmouse; 03-21-2004 at 10:42 AM.

  8. #8
    Registered User Nova_Collision's Avatar
    Join Date
    Feb 2003
    Posts
    40
    Guys, I'm starting to feel a bit guilty about this.....I hope I don't wear out my welcome.

    I feel as though I'm almost there. But not quite.

    I have no idea how to use TreeView_InsertItem as MSDN doesn't tell me what library it resides in. Besides, I want to use the CTreeCtrl::InsertItem() method because I only need to use a name and position to add an item. Here's what I've figured:

    Code:
    CTreeCtrl *dirTree = (CTreeCtrl*)GetDlgItem(hwnd, IDC_DIRTREE);
    So far as I can tell, I now have a pointer to a CTreeCtrl object. So I try this:

    Code:
    dirTree->InsertItem("Root", TVI_ROOT, TVI_LAST);
    I've also tried:

    Code:
    dirTree->InsertItem("Root", NULL, NULL);
    But I dunno if that's any different.

    Anyway, my program crashes with an exception error. As I said, I'm not so good with pointers, so I'm sure I'm violating memory somewhere.
    When in doubt, empty your magazine - Murphy's Laws Of Combat #7

  9. #9
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Sorry to confuse you. My last post was for the WinApi route. It doesn't apply for MFC.

    >>I have no idea how to use TreeView_InsertItem as MSDN doesn't tell me what library it resides in.<<

    It's a macro in commctrl.h.

    Code:
    CTreeCtrl *dirTree = (CTreeCtrl*)GetDlgItem(hwnd, IDC_DIRTREE);
    Hopefully casting a HWND to a CTreeCtrl * is unfortunately not going to work.

    I don't know but I think you have to use "class wizard(Ctrl-W)". Have a look at some tutorials(and a few more).

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    First step is learn how to use MSDN...

    TreeView_InsertItem
    As the documentation clearly shows, it's a macro that resides in the "commctrl.h" header.
    It also says that you can use the macro as a convienence or send the TVM_INSERTITEM message yourself.

    Next step is to understand that MFC is a C++ framework of the raw Win32 API. And mixing the two can get you in trouble fast if you don't know what you're doing.

    Luckily, MFC is fully documented on MSDN as well with plenty of sample applications to download.
    There are 3 samples applications for CTreeCtrl alone.

    In this case, what you want to call is your CDialog's GetDlgItem() member function, not the Win32 API function ::GetDlgItem().

    gg

  11. #11
    Registered User Nova_Collision's Avatar
    Join Date
    Feb 2003
    Posts
    40
    CodePlug, Anonytmouse, I want to thank you guys for your help. And for your patience. It's clear that I don't understand the MFC as well as I need to and I'm just out of time. I'm going to have to abandon this, at least for now, so that I can get my project completed on time. Perhaps, if I'm lucky, I'll spend the rest of my C++ days in OpenGL and/or Direct3D and DirectX and won't have to worry about the Win32 API.

    Thanks again, guys.

    PS. I was using the MSDN libraries that came with VS6. In that offline version of the MSDN, the description of TreeView_InsertItem doesn't say anything about it's header. I didn't think to look on the online MSDN. Sorry.
    Last edited by Nova_Collision; 03-21-2004 at 04:26 PM.
    When in doubt, empty your magazine - Murphy's Laws Of Combat #7

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM