CRecentFileList 雑記 |
Last Updated: Dec.11,2008 |
| 使用しているコンパイラは、Microsoft Visual C++ 6.0 です。これ以外のバージョンでは記述と食い違うことがあるかも知れません。 | |
IE のアドレス欄のように、コンボボックスのドロップダウンリストに、コンボボックスへの入力履歴を残したいと思うことがあります。MFC を眺めていると、ファイルメニューに表示される「最近使ったファイル」で使われている CRecentFileList がちょっと便利そうだったので、派生クラスを作ってみました。
使い方は、
CRecentComboList* m_pHogeList;
m_pHogeList = new CRecentComboList(_T("Hoge"), _T("hoge%d"), 8);
m_pHogeList->ReadList();
delete m_pHogeList;
UpdateData(TRUE); m_pHogeList->Add(m_sHogeNew); m_pHogeList->WriteList();
m_pHogeList->OnDropDownCombo((CComboBox*)GetDlgItem(IDC_COMBO_ID));
以上で完了です。引数等については、MFC のヘルプ「CRecentFileList」をご覧下さい。
// RecentComboList.h: CRecentComboList クラスのインターフェイス
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_RECENTCOMBOLIST_H_INCLUDED_)
#define AFX_RECENTCOMBOLIST_H_INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <afxadv.h>
class CRecentComboList : public CRecentFileList
{
public:
CRecentComboList(LPCTSTR lpszSection, LPCTSTR lpszEntryFormat, int nSize);
virtual ~CRecentComboList();
virtual void Add(LPCTSTR lpszPathName);
virtual void OnDropDownCombo(CComboBox* pCombo);
};
#endif // !defined(AFX_RECENTCOMBOLIST_H_INCLUDED_)
// RecentComboList.cpp: CRecentComboList クラスのインプリメンテーション
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "RecentComboList.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// 構築/消滅
//////////////////////////////////////////////////////////////////////
CRecentComboList::CRecentComboList(LPCTSTR lpszSection,
LPCTSTR lpszEntryFormat,
int nSize)
: CRecentFileList(1, lpszSection, lpszEntryFormat, nSize)
{
}
CRecentComboList::~CRecentComboList()
{
}
void CRecentComboList::Add(LPCTSTR lpszPathName)
{
ASSERT(m_arrNames != NULL);
ASSERT(lpszPathName != NULL);
ASSERT(AfxIsValidString(lpszPathName));
for (int iMRU = 0; iMRU < m_nSize-1; iMRU++)
{
if (m_arrNames[iMRU].Compare(lpszPathName) == 0)
break;
}
for (; iMRU > 0; iMRU--)
{
ASSERT(iMRU > 0);
ASSERT(iMRU < m_nSize);
m_arrNames[iMRU] = m_arrNames[iMRU-1];
}
m_arrNames[0] = lpszPathName;
}
void CRecentComboList::OnDropDownCombo(CComboBox *pCombo)
{
ASSERT(m_arrNames != NULL);
ASSERT(pCombo != NULL);
CString sText;
pCombo->GetWindowText(sText);
pCombo->ResetContent();
pCombo->SetWindowText(sText);
for (int iMRU = 0; iMRU < m_nSize; iMRU++)
if (!m_arrNames[iMRU].IsEmpty())
pCombo->AddString(m_arrNames[iMRU]);
}
| Back | Copyright (c) 2008 AOK. All Rights Reserved. |