-


CListCtrl 을 사용중 안에 내용을 저장 하기 위해 후딱 만들어 봤다...

아직 충분한 테스트가 안되어 있기에 버그가 있을수 있으니 사용에 주의 바람..



ListCtrlEx.h

class CListCtrlEx : public CListCtrl
{
// Construction
public:
	CListCtrlEx();

// Attributes
public:

// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CListCtrlEx)
	//}}AFX_VIRTUAL

// Implementation
public:
	typedef enum
	{
		TEXT_MODE,
		BINARY_MODE
	}FILE_MODE;
	BOOL LoadData(CString strFileName, FILE_MODE nMode = BINARY_MODE);
	BOOL SaveData(CString strFileName, FILE_MODE nMode = BINARY_MODE);
	virtual ~CListCtrlEx();

	// Generated message map functions
protected:
	//{{AFX_MSG(CListCtrlEx)
		// NOTE - the ClassWizard will add and remove member functions here.
	//}}AFX_MSG

	DECLARE_MESSAGE_MAP()

private:
	CString strFileKey;			// Data 의 유효성을 검사하기 위한 키 값
};





ListCtrlEx.cpp

CListCtrlEx::CListCtrlEx()
{
	// GUID Generator Tool 을 이용해 값을 만들었다

 	strFileKey = "12345678_90AB_CDEF_GHIJ_KLMNOPQRSTUX";
}

CListCtrlEx::~CListCtrlEx()
{
}


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

/////////////////////////////////////////////////////////////////////////////
// CListCtrlEx message handlers



BOOL CListCtrlEx::SaveData(CString strFileName, FILE_MODE nMode)
{
	int nRow = 0, nCol = 0;
	CString strTemp = "";
	CFile file;

	TCHAR pszPathName[_MAX_PATH];
	
	::GetModuleFileName(::AfxGetInstanceHandle(), pszPathName, _MAX_PATH);

	PathRemoveFileSpec(pszPathName);

	CString strFullPath;

	strFullPath.Format("%s\\%s", pszPathName, strFileName);

	if(nMode == TEXT_MODE)
	{
		if(!file.Open(strFullPath, CFile::modeCreate | CFile::modeWrite))
		{
			return FALSE;
		}
		
	}
	else if(nMode == BINARY_MODE)
	{
		if(!file.Open(strFullPath, CFile::modeCreate |
				CFile::modeWrite | CFile::typeBinary))
		{
			return FALSE;
		}
	}

	CArchive	ar(&file, CArchive::store);

	ar << strFileKey;

	// Item 개수
	nRow = GetItemCount();
	// Column  개수
	nCol = GetColumns();
	
	ar << nRow;
	ar << nCol;

	for(int i=0; i < nRow; i++)
	{
		for(int j=0; j < nCol; j++)
		{
			strTemp = GetItemText(i, j);
			ar << strTemp;
		}
	}

	ar.Close();
	file.Close();

	return TRUE;
}

BOOL CListCtrlEx::LoadData(CString strFileName, FILE_MODE nMode)
{
	int nRow = 0, nCol = 0;
	int nIdx = 0;
	CString strTemp = "";
	CFile file;

	TCHAR pszPathName[_MAX_PATH];
	
	::GetModuleFileName(::AfxGetInstanceHandle(), pszPathName, _MAX_PATH);

	PathRemoveFileSpec(pszPathName);

	CString strFullPath;

	strFullPath.Format("%s\\%s", pszPathName, strFileName);
	
	if(nMode == TEXT_MODE)
	{
		if(!file.Open(strFullPath, CFile::modeRead))
		{
			return FALSE;
		}
		
	}
	else if(nMode == BINARY_MODE)
	{
		if(!file.Open(strFullPath, CFile::modeRead | CFile::typeBinary))
		{
			return FALSE;
		}
	}
	
	CArchive	ar(&file, CArchive::load);

	ar >> strTemp;

	if(strTemp != strFileKey)
	{
		ar.Close();
		file.Close();
		return FALSE;
	}
	
	ar >> nRow;
	ar >> nCol;

	if(nRow <= 0 || nCol <= 0)
	{
		ar.Close();
		file.Close();

		return FALSE;
	}

	if(nCol > GetColumns())
	{
		ar.Close();
		file.Close();

		return FALSE;
	}

	for(int i=0; i < nRow; i++)
	{
		ar >> strTemp;
		nIdx = InsertItem(i, strTemp);
		for(int j=1; j < nCol; j++)
		{
			ar >> strTemp;
			SetItemText(nIdx, j, strTemp);
		}
	}
	
	ar.Close();
	file.Close();

	return TRUE;
}





다른 카테고리의 글 목록

Programming/MFC 카테고리의 포스트 목록