⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ngrecentitemcombobox.cpp

📁 ResOrg 图形化管理Vc项目的资源ID的工具的源代码。 ResOrg - Manage and Renumber Resource Symbol IDs Introduction The
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/////////////////////////////////////////////////////////////////////////////
//
// MRUCombo.cpp: Implementation file for the CNGRecentItemComboBox class.
//
// Written by Michael Dunn <mdunn at inreach dot com>
//
// Modified by Anna-Jayne Metcalfe to use CNGRecentItemList instead of CRecentFileList
//
/////////////////////////////////////////////////////////////////////////////
//
// Revision history:
//
//  9/9/1998: First release.
//
/////////////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "NGRecentItemComboBox.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define MRUC_DEFAULT_MRU_SIZE   10


/////////////////////////////////////////////////////////////////////////////
// CNGRecentItemComboBox constructor & destructor


//////////////////////////////////////////////////////////////////////////
//
// Function:    CNGRecentItemComboBox()
//
// Description:
//  Class constructor.
//
// Notes:
//  Calls base class constructor and initializes member variables.
//
//////////////////////////////////////////////////////////////////////////

CNGRecentItemComboBox::CNGRecentItemComboBox() : CComboBox(),
    m_bRefreshAfterAdd   ( FALSE ),
    m_bSaveAfterAdd      ( FALSE ),
    m_bSaveOnDestroy     ( TRUE ),
    m_nMaxMRUSize        ( MRUC_DEFAULT_MRU_SIZE ),
    m_pMRU               ( NULL ),
    m_bParamsChanged     ( FALSE )
{
}


//////////////////////////////////////////////////////////////////////////
//
// Function:    CNGRecentItemComboBox()
//
// Description:
//  Class destructor.
//
//////////////////////////////////////////////////////////////////////////

CNGRecentItemComboBox::~CNGRecentItemComboBox(void)
{                                        // Save the MRU if we need to.
    if ( m_bSaveOnDestroy )
	{
        if ( !SaveMRU() )
		{
            TRACE( _T("CNGRecentItemComboBox -- Warning - SaveMRU() in destructor failed. MRU was not saved.\n") );
		}
	}

                                        // Free up the CRecentFileList object.
    if ( NULL != m_pMRU )
	{
        delete m_pMRU;
	}
}


BEGIN_MESSAGE_MAP(CNGRecentItemComboBox, CComboBox)
	//{{AFX_MSG_MAP(CNGRecentItemComboBox)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CNGRecentItemComboBox MRU operations


//////////////////////////////////////////////////////////////////////////
//
// Function:    AddToMRU()
//
// Description:
//  Adds a string to the MRU list.
//
// Input:
//  szNewItem: [in] The string to add.
//
// Returns:
//  TRUE if successful, FALSE if not.
//
//////////////////////////////////////////////////////////////////////////

BOOL CNGRecentItemComboBox::AddToMRU ( LPCTSTR szNewItem )
{
                                        // String can't be a null pointer!
    ASSERT ( NULL != szNewItem );

                                        // Allocate a new CRecentFileList 
                                        // if necessary.
    if ( NULL == m_pMRU )
	{
		if ( !AllocNewMRU() )
		{
			TRACE( _T("CNGRecentItemComboBox -- AllocNewMRU() failed in AddToMRU().\n") );
            return FALSE;
		}
	}

	m_pMRU->Add ( szNewItem );			// Add it to the MRU list.
    
                                        // Automagically refresh the combobox?
    if ( m_bRefreshAfterAdd )
	{
		RefreshCtrl();
	}

                                        // Automagically save the MRU?
    if ( m_bSaveAfterAdd )
	{
		SaveMRU();
	}


    return TRUE;
}


BOOL CNGRecentItemComboBox::RemoveFromMRU(int nItem)
{
	if ( (NULL != m_pMRU) && (nItem >= 0) && (nItem < GetCount() ) )
	{
		DeleteString(nItem);

		ASSERT( (*m_pMRU)[nItem].GetLength() > 0 );

		m_pMRU->Remove(nItem);

		AllocNewMRU();

		if (m_bSaveAfterAdd)
		{
	        SaveMRU();
		}
		RefreshCtrl();

		return TRUE;
	}
	return FALSE;
}


//////////////////////////////////////////////////////////////////////////
//
// Function:    EmptyMRU()
//
// Description:
//  Removes all strings from the MRU list.
//
// Input:
//  Nothing.
//
// Returns:
//  Nothing.
//
//////////////////////////////////////////////////////////////////////////

void CNGRecentItemComboBox::EmptyMRU()
{
	int i = 0;

    if ( NULL != m_pMRU )
	{
                                        // Remove all strings from the MRU list.
                                        // Go in reverse order to keep the
                                        // strings from changing positions 
                                        // while we're doing our dirty work.
        for ( i = m_pMRU->GetSize() - 1; i >= 0; i-- )
		{
            m_pMRU->Remove(i);
		}
	}
}


//////////////////////////////////////////////////////////////////////////
//
// Function:    LoadMRU()
//
// Description:
//  Loads an MRU from the registry or the app's INI file.
//
// Input:
//  Nothing.
//
// Returns:
//  TRUE if successful, FALSE if not.
//
//////////////////////////////////////////////////////////////////////////

BOOL CNGRecentItemComboBox::LoadMRU()
{
                                        // We always allocate a new
                                        // CRecentFileList object when loading.
    if ( !AllocNewMRU() )
	{
        TRACE( _T("CNGRecentItemComboBox -- AllocNewMRU() failed in LoadMRU().\n") );
        return FALSE;
	}

    m_pMRU->ReadList();

    return TRUE;
}


//////////////////////////////////////////////////////////////////////////
//
// Function:    SaveMRU()
//
// Description:
//  Writes the MRU to the registry or app's INI file.
//
// Input:
//  Nothing.
//
// Returns:
//  TRUE if successful, FALSE if not.
//
//////////////////////////////////////////////////////////////////////////

BOOL CNGRecentItemComboBox::SaveMRU()
{
                                        // If we haven't created a 
                                        // CRecentFileList yet, then there's
                                        // nothing to save.
    if ( NULL == m_pMRU )
	{
        TRACE( _T("CNGRecentItemComboBox -- SaveMRU failed - no CRecentFileList created.\n") );
        return FALSE;
	}

                                        // Check that the CRecentFileList 
                                        // parameters are kosher.
    if ( !VerifyMRUParams() )
	{
        TRACE( _T("CNGRecentItemComboBox -- SaveMRU() failed - params not set.\n") );
        return FALSE;
	}

                                        // If the registry key/value strings
                                        // have been changed, we need to make
                                        // a new CRecentFileList.
    if ( m_bParamsChanged )
	{
        if ( !AllocNewMRU() )
		{
            TRACE( _T("CNGRecentItemComboBox -- SaveMRU failed - couldn't reallocate CRecentFileList with new MRU params.\n") );
            return FALSE;
		}
	}

    m_pMRU->WriteList();

    return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// CNGRecentItemComboBox combobox control operations


//////////////////////////////////////////////////////////////////////////
//
// Function:    RefreshCtrl()
//
// Description:
//  Sets the contents of the combobox according to the MRU list.
//
// Input:
//  Nothing.
//
// Returns:
//  Nothing.
//
//////////////////////////////////////////////////////////////////////////

void CNGRecentItemComboBox::RefreshCtrl()
{
	CString cstrComboText;
                                        // Save the contents of the edit
                                        // portion of the combobox.
    GetWindowText ( cstrComboText );

    ResetContent();

    for ( int i = 0; i < m_pMRU->GetSize(); i++ )
	{
                                        // Don't add empty strings to the combobox.
        if ( (*m_pMRU)[i].GetLength() > 0 )
		{
            if ( AddString ( (*m_pMRU)[i] ) < 0 )
			{
                TRACE( _T("CNGRecentItemComboBox -- Warning - RefreshCtrl() couldn't add MRU item %d to combobox.\n"),
                       i );
			}
		}
	}

	// Restore the editbox text.
	int nItem = (int)SendMessage(CB_FINDSTRINGEXACT,
								 (WPARAM)-1,
								 (LPARAM)(LPCTSTR)cstrComboText);
	if (nItem >= 0)
	{
		// select it
		SetCurSel(nItem);
	}
	else
	{
		// just set the edit text (this will be ignored if this is a drop list)
		SetWindowText(cstrComboText);
	}
}


/////////////////////////////////////////////////////////////////////////////
// CNGRecentItemComboBox data accessor functions


//////////////////////////////////////////////////////////////////////////
//
// Function:    SetMRURegKey()
//
// Description:
//  Sets the registry key (or INI file section) in which the MRU will be
//  saved.
//
// Input:
//  szRegKey: [in] The key/section name.
//
// Returns:
//  Nothing.
//
//////////////////////////////////////////////////////////////////////////

void CNGRecentItemComboBox::SetMRURegKey ( LPCTSTR szRegKey )
{
                                        // The key name can't be a null string.
    ASSERT ( NULL != szRegKey );

    try
	{
                                        // Store the reg key name & set the
                                        // changed flag.
        m_cstrRegKey = szRegKey;

        m_bParamsChanged = TRUE;
	}
    catch (CMemoryException* e)
	{
        TRACE( _T("CNGRecentItemComboBox -- Memory exception in CNGRecentItemComboBox::SetMRURegKey()!\n") );
		e->Delete();
        throw;
	}
}


//////////////////////////////////////////////////////////////////////////
//
// Function:    GetMRURegKey
//
// Description:
//  Returns the current registry key or INI file section in which the MRU
//  will be saved.
//
// Input:
//  Nothing.
//
// Returns:
//  The key name.
//
//////////////////////////////////////////////////////////////////////////

const CString& CNGRecentItemComboBox::GetMRURegKey() const
{
    return m_cstrRegKey;
}


//////////////////////////////////////////////////////////////////////////
//
// Function:    SetMRUValueFormat()
//
// Description:
//  Sets the format to be used for writing MRU items to the registry or
//  the app's INI file.
//
// Input:
//  szValueFormat: [in] The format to use.
//
// Returns:
//  TRUE if the format is acceptable (i.e., contains "%d"), FALSE if not.
//
//////////////////////////////////////////////////////////////////////////

BOOL CNGRecentItemComboBox::SetMRUValueFormat ( LPCTSTR szValueFormat )
{
BOOL bRetVal = FALSE;

                                        // The key name can't be a null string.
    ASSERT ( NULL != szValueFormat );


                                        // Check that the format strings 
                                        // contains "%d"
    if ( NULL == _tcsstr ( szValueFormat, _T("%d") ) )
	{
        TRACE( _T("CNGRecentItemComboBox -- SetMRUValueFormat() returning FALSE - argument didn't contain \"%d\"\n") );
        return FALSE;
	}
    else
	{
        try
		{
                                        // Save the format string and set the
                                        // changed flag.
            m_cstrRegValueFormat = szValueFormat;
            m_bParamsChanged = TRUE;
            bRetVal = TRUE;
		}
        catch (CMemoryException* e)
		{
            TRACE( _T("CNGRecentItemComboBox -- Memory exception in CNGRecentItemComboBox::SetMRUValueFormat()!\n") );
			e->Delete();
            throw;
		}
	}

    return bRetVal;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -