单击组合框的下拉箭头,要求能够在列表中显示本机的目录,该如何写程序?用什么函数?
解决方案 »
- 求助 求大神给提出具体的MFC实现此游戏的思路
- 怎样用软件仿真玩具动作?
- GetDlgItem得到的TreeCtrl指针,怎么不能够响应消息?
- 用于菜单的“COMMAND”和“UPDATE_COMMAND_UI”消息有什么区别呢?
- 大家帮忙啊,线程通信的问题,读名命名管道被阻塞以后...
- 如何在划线时不闪烁 急 在线等!!!!!!!!!!!!!
- 用ODBC编程时,出现的怪异问题。
- 关于树形控件NM_CLICK的问题,求解答?
- 如何用函数安装一个字体到字体文件夹中!(直接拷贝进去不起作用).给分!!
- 疑惑:拷贝构造和附值函数中私有变量的疑惑????
- 怎么将CRichEditCtrl中的汉字显示为粗体。急!
- 高手指路——Unicode转换问题
class CDriveBox : public CComboBoxEx
{
public:
CDriveBox();
virtual ~CDriveBox(); // Generated message map functions
protected:
// {{AFX_MSG(CDriveBox)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
// }}AFX_MSGDECLARE_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 handlersint 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盘。
SetCurrentDirectory("C:\\");((CComboBox *)GetDlgItem(IDC_COMBO1))->ResetContent();((CComboBox *)GetDlgItem(IDC_COMBO1))->Dir(DDL_DIRECTORY, "*.*");
---------
++C++
---------