📄 drivecombobox.cpp
字号:
// DriveComboBox.cpp : implementation file
//
#include "stdafx.h"
#include "TryDriveSerialNo.h"
#include "DriveComboBox.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define ICONSTARTX 3 // the output x position of the icon in the item.
#define SPACE 5 // the space between the icon and the text.
/////////////////////////////////////////////////////////////////////////////
// CDriveComboBox
CDriveComboBox::CDriveComboBox() : m_pwndParent(NULL), m_uCustomMessage(0)
{
}
CDriveComboBox::~CDriveComboBox()
{
}
BEGIN_MESSAGE_MAP(CDriveComboBox, CComboBox)
//{{AFX_MSG_MAP(CDriveComboBox)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDriveComboBox message handlers
BOOL CDriveComboBox::InitDriveList(UINT nID, CWnd *pwndParent, UINT uCustomMessage)
{
// Attached already
// if ( !SubclassDlgItem(nID, pwndParent) )
// return FALSE;
m_pwndParent = pwndParent;
m_uCustomMessage = uCustomMessage;
if ( !Dir(DDL_DRIVES | DDL_EXCLUSIVE, _T("*")) )
return FALSE;
SetCurSel(1);
return TRUE;
}
void CDriveComboBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
if ( lpDrawItemStruct->CtlType == ODT_COMBOBOX
&& lpDrawItemStruct->itemID >= 0 )
{
CImageList* pImageList;
HIMAGELIST hImageList;
SHFILEINFO sfi;
COLORREF textColor = ::GetSysColor(COLOR_WINDOWTEXT);
COLORREF backColor = ::GetSysColor(COLOR_WINDOW);
CString csItem; GetLBText(lpDrawItemStruct->itemID, csItem);
CString csPath(csItem.Mid(2, 1) + _T(":\\"));
CRect itemRect(lpDrawItemStruct->rcItem);
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
// Draw the background
if ( lpDrawItemStruct->itemState & ODS_FOCUS )
{
// Invert the text color
backColor = ::GetSysColor(COLOR_HIGHLIGHT);
textColor = 0x00FFFFFF & ~textColor;
}
pDC->FillRect(&itemRect, &CBrush(backColor));
// If the item has focus, we should draw a focus rect.
if ( lpDrawItemStruct->itemState & ODS_FOCUS )
{
pDC->DrawFocusRect(itemRect);
}
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(textColor);
// Draw the icon and text.
// Get the file's small icon
// Use SHGetFileInfo, we can get a lot of information about files,
// directory, drives such as icon, status, etc.
hImageList = (HIMAGELIST)::SHGetFileInfo(csPath, 0, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_DISPLAYNAME);
// hImageList = (HIMAGELIST)::SHGetFileInfo(csPath, 0, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
pImageList = CImageList::FromHandle(hImageList);
// csItem = sfi.szDisplayName;
// Draw the icon
pImageList->Draw(pDC, sfi.iIcon, CPoint(itemRect.left + ICONSTARTX, itemRect.top + 1), ILD_TRANSPARENT);
// At last, we'll draw the string.
itemRect.OffsetRect(ICONSTARTX + SPACE + 16, 0);
DrawText(pDC->m_hDC, sfi.szDisplayName, -1, itemRect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
// DrawText(pDC->m_hDC, csPath, -1, itemRect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
}
}
CString CDriveComboBox::GetCurDrive() const
{
CString csItem;
GetLBText(GetCurSel(), csItem);
return csItem.Mid(2, 1);
}
BOOL CDriveComboBox::SetCurDrive(const CString& csDrive)
{
TCHAR tDrive[2];
int nCount;
int i;
CString csItem;
if ( csDrive.IsEmpty() || csDrive.GetLength() < 3 || csDrive[1] != _T(':') || csDrive[2] != _T('\\') )
return FALSE;
if ( csDrive[0] >= _T('a') && csDrive[0] >= _T('z') )
tDrive[0] = csDrive[0];
else if ( csDrive[0] >= _T('A') && csDrive[0] <= _T('Z') )
tDrive[0] = csDrive[0] - _T('A') + _T('a');
else
return FALSE;
tDrive[1] = NULL;
nCount = GetCount();
for ( i = 0; i < nCount; i++ )
{
GetLBText(i, csItem);
if ( csItem.Mid(2,1).CompareNoCase(tDrive) == 0 )
{
SetCurSel(i);
return TRUE;
}
}
return FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -