驱动器组合框

 

 

相信用过VB的人都对那些驱动器,目录,文件列表控件有兴趣,VC中没有,只好自己做了。以下是我自己写的驱动器组合框控件代码。

/// DriveBox.h

class CDriveBox : public CComboBoxEx
{
public:
 CDriveBox();
 virtual ~CDriveBox();

 // Generated message map functions
protected:
 //  {{AFX_MSG(CDriveBox)
 afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
 //  }}AFX_MSG

DECLARE_MESSAGE_MAP()
public:
 int GetSelDrive();
 bool SetSelDrive(char drive);
 void Initialize();
private:
 void AddItem(char *path,int index);
 CImageList m_img;
 char m_drivers[50];
};

 

// DriveBox.cpp : implementation file
//

#include "stdafx.h"
#include "DriveBox.h"


CDriveBox::CDriveBox()
{
 
}

CDriveBox::~CDriveBox()
{
}


BEGIN_MESSAGE_MAP(CDriveBox, CComboBoxEx)
  // {{ AFX_MSG_MAP(CDriveBox)
 ON_WM_CREATE()
 // }}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// CDriveBox message handlers

int CDriveBox::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
 if (CComboBoxEx::OnCreate(lpCreateStruct) == -1)
  return -1;
 
 // TODO: Add your specialized creation code here
 Initialize();
 return 0;
}

void CDriveBox::Initialize()
{
 m_img.Create( GetSystemMetrics(SM_CXSMICON),
            GetSystemMetrics(SM_CYSMICON),
           ILC_MASK|ILC_COLORDDB, 50, 50);
 m_img.SetBkColor( GetSysColor(COLOR_WINDOW) );
 SetImageList(&m_img);
 char *ptr,drivename[500];
 GetLogicalDriveStrings(500,drivename);
 ptr=drivename;int index=0;
 while (*ptr)
 {
 m_drivers[index]=*ptr;
 AddItem(ptr,index++);
 ptr+=strlen(ptr)+1;
 }
 SetSelDrive('c');
}

void CDriveBox::AddItem(char *path,int index)
{
 SHFILEINFO pathInfo,path_SelInfo;
 SHGetFileInfo( path, NULL, &pathInfo,
  sizeof(pathInfo),
  SHGFI_DISPLAYNAME | SHGFI_ICON | SHGFI_SMALLICON);
 SHGetFileInfo( path, NULL, &path_SelInfo,
  sizeof(path_SelInfo),
  SHGFI_DISPLAYNAME |  SHGFI_ICON | SHGFI_OPENICON | SHGFI_SMALLICON);
 int icon=m_img.Add(pathInfo.hIcon);
 int icon_sel=m_img.Add(path_SelInfo.hIcon);
 COMBOBOXEXITEM item;
 item.mask=CBEIF_TEXT|CBEIF_IMAGE|CBEIF_SELECTEDIMAGE;
 item.pszText=pathInfo.szDisplayName;
 item.iSelectedImage=icon;
 item.iImage=icon;//正常图标
 item.iItem=index;
 InsertItem(&item);
}

int CDriveBox::GetSelDrive()//返回所选磁盘
{
 int sel=GetCurSel();
 char driver=m_drivers[sel];
 driver=toupper(driver);
 return (driver-65);//返回数字索引,如0为A盘,2为C盘
 //  return   driver;//返回盘符
}

bool CDriveBox::SetSelDrive(char drive)//设置磁盘
{
 if(!isalpha(drive))return FALSE;
 drive=toupper(drive);int index=0;
 while(drive!=toupper(m_drivers[index]))index++;
 SetCurSel(index);return TRUE;

}


//

如何使用?

建立一个对话框工程CTestDlg,用ResourceEditor加入一个ComboBoxEx控件,用ClassWizard加入一个Control成员m_box, 然后在CTestDlg.h中将CComboBoxEx改为CDriveBox,在CTestDlg.h开始处加一句#include  "CDriveBox.h",在CTestDlg::OnInitDialog()中加上m_box.Initialize()就OK了。用m_box.GetSelDrive()返回当前选择驱动器.如0为A盘,2为C盘。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值