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

📄 diarystore.cpp

📁 很好的日记软件,能让你认识到文件的存储的实现!
💻 CPP
字号:
// DiaryStore.cpp : implementation file
//

#include "stdafx.h"
#include "MyDiary.h"
#include "DiaryStore.h"

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

#define IDVIEW 0x00001

/////////////////////////////////////////////////////////////////////////////
// CDiaryStore dialog


CDiaryStore::CDiaryStore(CWnd* pParent /*=NULL*/)
	: CDialog(CDiaryStore::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDiaryStore)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CDiaryStore::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDiaryStore)
	DDX_Control(pDX, IDC_DIARY_LIST, m_list);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDiaryStore, CDialog)
	//{{AFX_MSG_MAP(CDiaryStore)
	ON_BN_CLICKED(IDC_EXT_BUTTON, OnExtButton)
	ON_BN_CLICKED(IDC_VIEW_BUTTON, OnViewButton)
	ON_BN_CLICKED(IDC_DELALL_BUTTON, OnDelallButton)
	ON_BN_CLICKED(IDC_DELSEL_BUTTON, OnDelselButton)
	ON_NOTIFY(NM_DBLCLK, IDC_DIARY_LIST, OnDblclkDiaryList)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDiaryStore message handlers


void CDiaryStore::OnExtButton() 
{
	// TODO: Add your control notification handler code here
	OnOK();
}

BOOL CDiaryStore::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	CMyDiaryApp * pApp = (CMyDiaryApp *)AfxGetApp();
	m_list.SetBkColor(RGB(0,128,128));
	m_list.SetTextBkColor(RGB(0,128,128));
	m_list.SetTextColor(RGB(255,255,0));
	
	m_list.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP );
	TCHAR szBuf[20];
	LV_COLUMN lvc;

	int i;
	for(i = 0;i <4;i ++)
	{
		switch(i)
		{
		case 0:
			wsprintf(szBuf,_T("日期"));
			lvc.cx = 100;
			break;
		case 1:
			wsprintf(szBuf,_T("星期"));
			lvc.cx = 50;
			break;
		case 2:
			wsprintf(szBuf,_T("天气"));
			lvc.cx = 40;
			break;
		case 3:
			wsprintf(szBuf,_T("内容"));
			lvc.cx = 200;
			break;
		}
		lvc.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH ;
		lvc.fmt = LVCFMT_LEFT;
		lvc.pszText = szBuf;
		lvc.iSubItem = i;
		m_list.InsertColumn(i,&lvc);
	}
	
	WORD nCount;
	POSITION pos;

	CMyDiaryApp * pDiary = (CMyDiaryApp *)AfxGetApp();
	nCount = pDiary->m_DiaryList.GetCount();
	pos = pDiary->m_DiaryList.GetHeadPosition();

	CString my_date;
	for(int k = 0; k < nCount && pos != NULL;k ++)
	{
		CDiary * pd = pApp->m_DiaryList.GetNext(pos);
		m_list.InsertItem(k,LPSTR_TEXTCALLBACK);
		my_date = pApp->TimeToCString(pd->m_date);
		m_list.SetItemText(k,0,my_date);
		m_list.SetItemText(k,1,pd->m_week);
		m_list.SetItemText(k,2,pd->m_weather);
		m_list.SetItemText(k,3,pd->m_txtBody);
	}
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDiaryStore::OnViewButton() 
{
	// TODO: Add your control notification handler code here
	
	int nSel;
	POSITION pos;
	POSITION posNext;
	
	posNext = m_list.GetFirstSelectedItemPosition();
	CMyDiaryApp * pApp = (CMyDiaryApp *)AfxGetApp();
	if(posNext == NULL)
		MessageBox("你没有选择记录","提示信息",MB_OK);
	else
	{
		nSel = m_list.GetNextSelectedItem(posNext);
		pos = pApp->m_DiaryList.FindIndex(nSel);

		pApp->m_pos = pos;
		pApp->is_old = TRUE;	
//		CString m_body;
//		m_body = 
		ry = pApp->m_DiaryList.GetAt(pos);
//		m_Body
//		m_body = pd->m_txtBody;

		CDialog::OnOK();
//		EndDialog(IDVIEW);
	}
}

void CDiaryStore::OnDelallButton() 
{
	// TODO: Add your control notification handler code here
	CMyDiaryApp * pApp = (CMyDiaryApp *)AfxGetApp();
	pApp->m_DiaryList.RemoveAll();
	m_list.DeleteAllItems();
	pApp->OnSave();
}

void CDiaryStore::OnDelselButton() 
{
	// TODO: Add your control notification handler code here
	int nSel;
	POSITION pos;
	CMyDiaryApp * pdiary = (CMyDiaryApp *)AfxGetApp();
	if(FindDiary(nSel,pos))
	{
		pdiary->m_DiaryList.RemoveAt(pos);
		m_list.DeleteItem(nSel);
		pdiary->OnSave();
	}
	else
		MessageBox("错误","提示信息",MB_OK);
}


int CDiaryStore::FindDiary(int& nSel, POSITION& pos)
{

    CMyDiaryApp *pApp = (CMyDiaryApp *)AfxGetApp();
	POSITION pos1 = m_list.GetFirstSelectedItemPosition();
	if (pos1 == NULL)
	{
		MessageBox("请先选择所要删除的记录!","提示",MB_OK);
		return NULL;
	}
	else
	{
	   nSel = m_list.GetNextSelectedItem(pos1);
	}

	pos = pApp->m_DiaryList.FindIndex(nSel);
	ASSERT(pos != NULL);
	return 1;

}

void CDiaryStore::OnDblclkDiaryList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	CMyDiaryApp * pApp = (CMyDiaryApp *)AfxGetApp();
	POSITION pos,pos1;
	int nIndex;
	pos1 = m_list.GetFirstSelectedItemPosition(); //得到选择的列的位置
	nIndex = m_list.GetNextSelectedItem(pos1); //得到list中的index值
	pos = pApp->m_DiaryList.FindIndex(nIndex); //
	pApp->m_pos = pos;
	pApp->is_old = TRUE;
	ry = pApp->m_DiaryList.GetAt(pos);
	CDialog::OnOK();
	*pResult = 0;
}

⌨️ 快捷键说明

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