ATL Tree Control and OLE DB

This small example is for the beginner who wants to create an ActiveX control by superclassing the windows common control using ATL 3.0. If you don’t have VC6, you need to install OLE DB1.1 from VC5 for this project to work properly.

On this site, a lot of excellent examples about tree controls are already there so why this example? Because:

  1. My tree control create by using ATL instead of MFC
  2. Using OLD DB to save tree structure and load it back
  3. Right click to open popup menu and more…

The main reason I want to post my article is because I am using OLE DB and OLE DB is still new to some programmers, especially for a new beginner.

Before you use this article, you should have Access database to see OLE DB in action. If you don’t have Access database but you have another database like Oracle, SQLServer or SQL Anywhere and you want to know how to do it is beyond the scope of this article.

Open Access database and create a table, and name it any name as you like. Create four fields in the table with the names as follow:

  • ITEM_NODE ( int )
  • ITEM_NAME ( text )
  • INDENT_LEVEL ( int )
  • ITEM_DATA ( long ) [ primary key ]

if you want to create some bogus data, go head and do it, otherwise the example will create default data for you. If you don’t want to use any database at all, two things you need to do:

  1. Open your project, click on FileView tab, remove the file name: Tree.h
  2. Open MyTreeControl.h, go to the method name ” OnDestroy() ” and comment out the line: ” this->SaveTree(); “, after all select Build on the menu and select Rebuild All.

When the project is running, the first thing you need to do is select ” Microsoft Jet 3.51 OLE DB Provider ( if you have another OLE DB Provider on your system, go head and select it. “, second is select your data source. If you don’t have any data in your table yet, just click cancel when the dialog asking you to select a driver and you will have the default data which I created for you.

To test this control, You can select the ActiveX Control test container to test it or you can create a new MFC project with Dialog base. When the project is completely constructed, click on Resource View, open Dialog resource, right click on dialog and select insert ActiveX control…, that is all you need to do. If you have any question about this project or you do not understand some line of codes, please post your comment and I will try to answer as soon as possible but within my knowledge, because I just have 2 year experience on Visual C. I hope you can understand my example and can apply part of it in your project in the future.

Download demo project – 198 KB

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read