CDeviceTree

 article from:http://www.codeproject.com/

Sample Image - DeviceTree.jpg

Introduction

Here we want to show installed devices of our PC like what a Device Manager shows. In Device Manager, installed devices are shown in specific categories like: DVD/CD-ROM Drives or Floppy disk controllers. In my previous article Enumerate Installed Devices Using Setup API, I show you how we can list all devices. In this article, installed devices are shown in a tree in their categories, like Device Manager.

For this purpose, I created a new class called CDeviceTree that is inherited from CTreeCtrl. Using of the new class is very simple.

How to use

First of all add DeviceTree.h and DeviceTree.cpp files to your project. Then in your dialog resource editor, add new Tree control (figure 2 shows it). Rename the control ID to your desired ID, for example IDC_DEVICE_TREE. Run class wizard (Ctrl+W) and add new member variable with variable type CDeviceTree. (If CDeviceTree does not appear in combo box, choose CTreeCtrl, then in your dialog header file, rename CTreeCtrl to CDeviceTree). Name it as m_DeviceTree.

Figure 2 - Add new tree control to dialog resource

Figure 3- Add member variable with variable type CDeviceTree.

If CDeviceTree does not appear in Variable type combo box, simply choose CTreeCtrl, then rename it to CDeviceTree in your dialog header file. For example:

CTreeCtrl   m_DeviceTree;

to:

CDeviceTree   m_DeviceTree;

Don't forget to include DeviceTree.h to your dialog header file. OK, now everything is ready to compile!

CDeviceTree Class

The CDeviceTree class contains only one public member function: EnumDevices(). The function is responsible for enumerating all of the installed devices in their categories. The user must only use this function to see the tree of devices. The best way for this is OnInitDialog() member function of main dialog. In other hand:

BOOL CYourDialog::OnInitDialog()
{
    CDialog::OnInitDialog();

    // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);         // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon
    
    // TODO: Add extra initialization here
    m_DeviceTree.EnumDevices();   //CDeviceTree will enumerate all of devices
    
    return TRUE;  // return TRUE  unless you set the focus to a control
}

The class definition is as below:

Collapse
#ifndef _DEVICE_TREE_H_
#define _DEVICE_TREE_H_

#include "SetupAPI.h"
#include "cfgmgr32.h"

class CDeviceTree : public CTreeCtrl
{
// Construction
public:
    CDeviceTree();

// Implementation
public:
    void EnumDevices();
    virtual ~CDeviceTree();

private:
    int EnumDevices(int index, TCHAR* DeviceClassName, TCHAR* DeviceName);
    int EnumDeviceClasses(int index, TCHAR* DeviceClassName, 
              TCHAR* DeviceClassDesc, BOOL* DevicePresent, int* ClassImage);
    
    SP_CLASSIMAGELIST_DATA m_ImageListData;
    CImageList m_ImageList;
    
    // Generated message map functions
protected:
    //{{AFX_MSG(CDeviceTree)
        // NOTE - the ClassWizard will add and remove member functions here.
    //}}AFX_MSG

    DECLARE_MESSAGE_MAP()
};

#endif // _DEVICE_TREE_H_

As you see, the application shows devices like Device Manager does (Figure 4):

Enjoy!

<script src="/script/togglePre.js" type="text/javascript"></script>

About A. Riazi


I was born in Shiraz, a very beautiful famous city in Iran. I started programming when I was 12 years old with GWBASIC. Since now, I worked with various programming languages from Basic, Foxpro, C/C++, Visual Basic, Pascal to MATLAB and now Visual C++.
I graduated from Iran University of Science & Technology in Communication Eng., and now work as a system programmer for a telecommunication industry.
I wrote several programs and drivers for Synthesizers, Power Amplifiers, GPIB, GPS devices, Radio cards, Data Acqusition cards and so many related devices.
I'm author of several books like Learning C (primary and advanced), Learning Visual Basic, API application for VB, Teach Yourself Object Oriented Programming (OOP) and etc.
I'm winner of January, May, August 2003 and April 2005 best article of month competetion, my articles are:

 


You can see list of my articles, by clicking here

Click here to view A. Riazi's online profile.


Other popular Tree Controls articles:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值