📄 ngrecentitemcombobox.cpp
字号:
//////////////////////////////////////////////////////////////////////////
//
// Function: GetMRUValueFormat
//
// Description:
// Returns the current registry value format string.
//
// Input:
// Nothing.
//
// Returns:
// The format string.
//
//////////////////////////////////////////////////////////////////////////
const CString& CNGRecentItemComboBox::GetMRUValueFormat() const
{
return m_cstrRegValueFormat;
}
//////////////////////////////////////////////////////////////////////////
//
// Function: SetMaxMRUSize()
//
// Description:
// Sets the max number of entries that the MRU can hold.
//
// Input:
// nMaxSize: [in] The new MRU size.
//
// Returns:
// The previous MRU size, or 0 if there was no previous MRU allocated, or
// -1 on error.
//
// Notes:
// This function always reallocates a CRecentFileList so that the size
// change (and registry string changes, if any) takes place immediately.
//
//////////////////////////////////////////////////////////////////////////
int CNGRecentItemComboBox::SetMaxMRUSize ( int nMaxSize )
{
int nRetVal = m_nMaxMRUSize;
// New size needs to be a positive
// number.
ASSERT ( nMaxSize >= 1 );
if ( nMaxSize <= 0 )
return -1;
m_nMaxMRUSize = nMaxSize;
if ( NULL == m_pMRU )
{
nRetVal = 0; // no previous size
}
if ( !AllocNewMRU() )
{
nRetVal = -1; // error!!
TRACE( _T("CNGRecentItemComboBox -- SetMaxMRUSize() failed - couldn't allocate new CRecentFileList.\n") );
}
return nRetVal;
}
//////////////////////////////////////////////////////////////////////////
//
// Function: GetMaxMRUSize
//
// Description:
// Returns the current max MRU size.
//
// Input:
// Nothing.
//
// Returns:
// The current max size.
//
//////////////////////////////////////////////////////////////////////////
int CNGRecentItemComboBox::GetMaxMRUSize() const
{
return m_nMaxMRUSize;
}
//////////////////////////////////////////////////////////////////////////
//
// Function: SetAutoSaveOnDestroy()
//
// Description:
// Sets whether the CNGRecentItemComboBox will automatically save the MRU when
// the object is destroyed.
//
// Input:
// bAutoSave: [in] Flag: enable auto-saving?
//
// Returns:
// The previous value of this setting.
//
//////////////////////////////////////////////////////////////////////////
BOOL CNGRecentItemComboBox::SetAutoSaveOnDestroy ( BOOL bAutoSave )
{
BOOL bRetVal = m_bSaveOnDestroy;
m_bSaveOnDestroy = bAutoSave;
return bRetVal;
}
//////////////////////////////////////////////////////////////////////////
//
// Function: SetAutoSaveAfterAdd()
//
// Description:
// Sets whether the CNGRecentItemComboBox will automatically save the MRU after
// an item is added successfully.
//
// Input:
// bAutoSave: [in] Flag: enable auto-saving?
//
// Returns:
// The previous value of this setting.
//
//////////////////////////////////////////////////////////////////////////
BOOL CNGRecentItemComboBox::SetAutoSaveAfterAdd ( BOOL bAutoSave )
{
BOOL bRetVal = m_bSaveAfterAdd;
m_bSaveAfterAdd = bAutoSave;
return bRetVal;
}
//////////////////////////////////////////////////////////////////////////
//
// Function: SetAutoRefreshAfterAdd()
//
// Description:
// Sets whether the CNGRecentItemComboBox will automatically refresh the combobox
// control after an item is added successfully.
//
// Input:
// bAutoSave: [in] Flag: enable auto-refresh?
//
// Returns:
// The previous value of this setting.
//
//////////////////////////////////////////////////////////////////////////
BOOL CNGRecentItemComboBox::SetAutoRefreshAfterAdd ( BOOL bAutoSave )
{
BOOL bRetVal = m_bRefreshAfterAdd;
m_bRefreshAfterAdd = bAutoSave;
return bRetVal;
}
/////////////////////////////////////////////////////////////////////////////
// CNGRecentItemComboBox misc. functions
//////////////////////////////////////////////////////////////////////////
//
// Function: VerifyMRUParams()
//
// Description:
// Checks the registry and size parameters and makes sure they're valid
// for CRecentFileList.
//
// Input:
// Nothing. (uses member variables)
//
// Returns:
// TRUE if the params are OK, FALSE if not.
//
//////////////////////////////////////////////////////////////////////////
BOOL CNGRecentItemComboBox::VerifyMRUParams() const
{
BOOL bRetVal = TRUE;
// 1. The registry key string must be
// non-empty.
if ( m_cstrRegKey.IsEmpty() || 0 == m_cstrRegKey.GetLength() )
{
TRACE( _T("CNGRecentItemComboBox -- VerifyMRUParams() - registry key name not set.\n") );
bRetVal = FALSE;
}
// 2. The reg value must be non-empty
// and contain "%d"
if ( m_cstrRegValueFormat.IsEmpty() ||
0 == m_cstrRegValueFormat.GetLength() )
{
TRACE( _T("CNGRecentItemComboBox -- VerifyMRUParams() - registry value format not set.\n") );
bRetVal = FALSE;
}
else if ( -1 == m_cstrRegValueFormat.Find ( _T("%d") ) )
{
TRACE( _T("CNGRecentItemComboBox -- VerifyMRUParams() - registry value format doesn't contain \"%d\"\n") );
bRetVal = FALSE;
}
// 3. The Max MRU size must be > 0.
if ( m_nMaxMRUSize <= 0 )
{
TRACE( _T("CNGRecentItemComboBox -- VerifyMRUParams() - max MRU size is set to <= 0\n") );
bRetVal = FALSE;
}
return bRetVal;
}
//////////////////////////////////////////////////////////////////////////
//
// Function: AllocNewMRU()
//
// Description:
// Allocates a new CRecentFileList, and copies the contents of the previous
// MRU (if any) to the new one.
//
// Input:
// Nothing.
//
// Returns:
// TRUE if successful, FALSE if not.
//
//////////////////////////////////////////////////////////////////////////
BOOL CNGRecentItemComboBox::AllocNewMRU()
{
CString* acstrOldList = NULL;
int nItemsToCopy;
int i;
// Make sure the MRU params are OK.
if ( !VerifyMRUParams() )
{
TRACE( _T("CNGRecentItemComboBox -- AllocNewMRU() returning FALSE - MRU list params invalid or not set.\n") );
return FALSE;
}
try
{
// Figuring out how many strings to
// copy: The lesser of the new MRU
// size and the previous MRU's size.
// Of course, if there was no previous
// MRU, then nothing will be copied.
nItemsToCopy = m_nMaxMRUSize;
if ( NULL != m_pMRU )
{
nItemsToCopy = __min ( m_nMaxMRUSize, m_pMRU->GetSize() );
// Save the contents of the old MRU list.
acstrOldList = new CString [ nItemsToCopy ];
for ( i = 0; i < nItemsToCopy; i++ )
{
acstrOldList[i] = (*m_pMRU)[i];
}
// Nuke the old CRecentItemList object...
delete m_pMRU;
}
// and make a new one!
m_pMRU = new CNGRecentItemList( 1,
m_cstrRegKey,
m_cstrRegValueFormat,
m_nMaxMRUSize );
// Copy the MRU strings if there was a previous MRU. We add
// the strings in reverse numerical order so they end up in the same
// order as they were in the old MRU.
if ( NULL != acstrOldList )
{
for ( i = nItemsToCopy - 1; i >= 0; i-- )
{
if (!acstrOldList[i].IsEmpty() )
{
m_pMRU->Add ( acstrOldList[i] );
}
}
delete [] acstrOldList;
}
}
catch (CMemoryException* e)
{
TRACE( _T("CNGRecentItemComboBox -- Memory exception in AllocNewMRU()!\n") );
e->Delete();
if ( NULL != m_pMRU )
{
delete m_pMRU;
m_pMRU = NULL;
}
throw;
}
// Reset the changed flag.
m_bParamsChanged = FALSE;
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CNGRecentFileComboBox
CNGRecentFileComboBox::CNGRecentFileComboBox(void)
{
}
CNGRecentFileComboBox::~CNGRecentFileComboBox(void)
{
}
BEGIN_MESSAGE_MAP(CNGRecentFileComboBox, CNGRecentItemComboBox)
//{{AFX_MSG_MAP(CNGRecentFileComboBox)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CNGRecentFileComboBox message handlers
void CNGRecentFileComboBox::PreSubclassWindow(void)
{
ASSERT( (GetStyle() & (CBS_OWNERDRAWFIXED | CBS_HASSTRINGS)) == (CBS_OWNERDRAWFIXED | CBS_HASSTRINGS) );
CNGRecentItemComboBox::PreSubclassWindow();
}
void CNGRecentFileComboBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
if (((LONG)(lpDrawItemStruct->itemID) >= 0) &&
(lpDrawItemStruct->itemAction & (ODA_DRAWENTIRE | ODA_SELECT)))
{
BOOL bDisabled = !IsWindowEnabled ();
COLORREF newTextColor = bDisabled ?
RGB(0x80, 0x80, 0x80) : GetSysColor (COLOR_WINDOWTEXT); // light gray
COLORREF oldTextColor = pDC->SetTextColor (newTextColor);
COLORREF newBkColor = GetSysColor (COLOR_WINDOW);
COLORREF oldBkColor = pDC->SetBkColor (newBkColor);
if (newTextColor == newBkColor)
newTextColor = RGB(0xC0, 0xC0, 0xC0); // dark gray
if (!bDisabled && ((lpDrawItemStruct->itemState & ODS_SELECTED) != 0))
{
pDC->SetTextColor (GetSysColor (COLOR_HIGHLIGHTTEXT));
pDC->SetBkColor (GetSysColor (COLOR_HIGHLIGHT));
}
CString sText;
GetLBText(lpDrawItemStruct->itemID, sText);
const RECT &rc=lpDrawItemStruct->rcItem;
CString sCompactedPath = sText;
LPTSTR pszCompactedPath = sCompactedPath.GetBuffer(_MAX_PATH);
::PathCompactPath( pDC->GetSafeHdc(),
pszCompactedPath,
rc.right - rc.left);
sCompactedPath.ReleaseBuffer();
pDC->ExtTextOut(rc.left + 2,
rc.top + 2,// + max(0, (cyItem - m_cyText) / 2),
ETO_OPAQUE, &rc,
sCompactedPath, sCompactedPath.GetLength (), NULL);
pDC->SetTextColor (oldTextColor);
pDC->SetBkColor (oldBkColor);
}
else if ((LONG)(lpDrawItemStruct->itemID)<0) // drawing edit text
{
COLORREF newTextColor = GetSysColor (COLOR_WINDOWTEXT); // light gray
COLORREF oldTextColor = pDC->SetTextColor (newTextColor);
COLORREF newBkColor = IsWindowEnabled() ? ::GetSysColor(COLOR_WINDOW) : ::GetSysColor(COLOR_3DFACE);
COLORREF oldBkColor = pDC->SetBkColor (newBkColor);
if ((lpDrawItemStruct->itemState & ODS_SELECTED) != 0)
{
pDC->SetTextColor (GetSysColor (COLOR_HIGHLIGHTTEXT));
pDC->SetBkColor (GetSysColor (COLOR_HIGHLIGHT));
}
CString sText;
GetWindowText(sText);
if (!sText.IsEmpty() )
{
const RECT& rc = lpDrawItemStruct->rcItem;
CString sCompactedPath = sText;
LPTSTR pszCompactedPath = sCompactedPath.GetBuffer(_MAX_PATH);
::PathCompactPath( pDC->GetSafeHdc(),
pszCompactedPath,
rc.right - rc.left);
sCompactedPath.ReleaseBuffer();
pDC->ExtTextOut(rc.left + 2,
rc.top + 2,// + max(0, (cyItem - m_cyText) / 2),
ETO_OPAQUE, &rc,
sCompactedPath, sCompactedPath.GetLength (), NULL);
pDC->SetTextColor (oldTextColor);
pDC->SetBkColor (oldBkColor);
}
}
if ((lpDrawItemStruct->itemAction & ODA_FOCUS) != 0)
{
pDC->DrawFocusRect(&lpDrawItemStruct->rcItem);
}
}
void CNGRecentFileComboBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
// TODO: Add your code to determine the size of specified item
UNREFERENCED_PARAMETER(lpMeasureItemStruct);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -