CDeviceTree






4.83/5 (23投票s)
2004年4月3日
2分钟阅读

203174

6283
一个显示已安装设备的树控件,
引言
在这里,我们希望显示我们PC上安装的设备,就像设备管理器显示的那样。在设备管理器中,安装的设备显示在特定的类别中,例如:DVD/CD-ROM驱动器或软盘控制器。在我的上一篇文章中,使用Setup API枚举已安装的设备,我向您展示了如何列出所有设备。在本文中,安装的设备以树形结构显示在它们的类别中,就像设备管理器一样。
为此,我创建了一个名为CDeviceTree
的新类,它继承自CTreeCtrl
。使用新类非常简单。
如何使用
首先,将DeviceTree.h和DeviceTree.cpp文件添加到您的项目中。然后在您的对话框资源编辑器中,添加一个新的树形控件(图2显示了它)。将控件ID重命名为您想要的ID,例如IDC_DEVICE_TREE
。运行类向导(Ctrl+W),并添加一个新的成员变量,变量类型为CDeviceTree
。(如果CDeviceTree
没有出现在组合框中,选择CTreeCtrl
,然后在您的对话框头文件中,将CTreeCtrl
重命名为CDeviceTree
)。将其命名为m_DeviceTree
。
如果CDeviceTree
没有出现在变量类型组合框中,只需选择CTreeCtrl
,然后在您的对话框头文件中将其重命名为CDeviceTree
。例如
CTreeCtrl m_DeviceTree;
to
CDeviceTree m_DeviceTree;
不要忘记将DeviceTree.h包含到您的对话框头文件中。好了,现在一切都准备好编译了!
CDeviceTree 类
CDeviceTree
类仅包含一个public
成员函数:EnumDevices()
。该函数负责枚举其类别中所有已安装的设备。用户只需使用此函数即可查看设备树。最好的方法是在主对话框的OnInitDialog()
成员函数中进行操作。另一方面
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
}
类定义如下
#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_
如您所见,应用程序显示设备的方式与设备管理器相同(图4)
尽情享用!
许可证
本文未附加明确的许可证,但可能在文章文本或下载文件本身中包含使用条款。如有疑问,请通过下面的讨论区联系作者。
作者可能使用的许可证列表可以在此处找到。