MFC版 日记本 源代码
笔者大二时做的,现在又改了改,拿出来。分享下吧~虽然当年的代码水平不高,不懂架构,代码挺乱,但是却是我很喜欢的作品。看图~
日记默认保存在Daily文件夹里,可用笔记本打开查看。Setting记录了窗体的位置,删除也没事。有BUG的话欢迎大家提出~
搞个下载路径吧:http://pan.baidu.com/s/1c05ot2o
欢迎下载试用~以下是部分源码:
#include "stdafx.h" #include "MFC对话框模版.h" #include "MFC对话框模版Dlg.h" #ifdef _DEBUG #define new DEBUG_NEW #endif CMFC对话框模版Dlg::CMFC对话框模版Dlg(CWnd* pParent /*=NULL*/) : CDialog(CMFC对话框模版Dlg::IDD, pParent) , bWorking(false) , buff(_T("")) , buff2(_T("")) , Ready(false) , Daily(_T("")) , path(_T("Daily")) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CMFC对话框模版Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); DDX_Control(pDX, IDC_EDIT1, edit); } BEGIN_MESSAGE_MAP(CMFC对话框模版Dlg, CDialog) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_LBN_SELCHANGE(IDC_LIST1, &CMFC对话框模版Dlg::OnLbnSelchangeList1) ON_WM_SIZE() ON_WM_CLOSE() ON_EN_CHANGE(IDC_EDIT1, &CMFC对话框模版Dlg::OnEnChangeEdit1) END_MESSAGE_MAP() BOOL CMFC对话框模版Dlg::OnInitDialog() { CDialog::OnInitDialog(); wchar_t buffer[1000]; GetCurrentDirectory(1000,buffer); path=buffer; if(path[path.GetLength()-1]!='\\') path+="\\"; path+="Daily\\"; fun.CreateDir(path); if(fun.IsFileExist(L"Setting.ini")) { CFile file; file.Open(L"Setting.ini",CFile::modeRead|CFile::typeBinary); file.Read((DWORD*)&rect.left,4); file.Read((DWORD*)&rect.top,4); file.Read((DWORD*)&rect.right,4); file.Read((DWORD*)&rect.bottom,4); this->SetWindowPos(0,rect.left,rect.top,rect.right,rect.bottom,0); file.Close(); } SetIcon(m_hIcon, TRUE); // 设置大图标 SetIcon(m_hIcon, FALSE); // 设置小图标 fun.SetTransport(this->GetSafeHwnd(),0,200,2); this->FindFile(); this->OnLbnSelchangeList1(); this->Ready=true; this->OnSize(0,0,0); return TRUE; // 除非将焦点设置到控件,否则返回 TRUE } void CMFC对话框模版Dlg::OnSysCommand(UINT nID, LPARAM lParam) { CDialog::OnSysCommand(nID, lParam); } void CMFC对话框模版Dlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // 用于绘制的设备上下文 SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0); int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } HCURSOR CMFC对话框模版Dlg::OnQueryDragIcon() { return static_cast<HCURSOR>(m_hIcon); } void CMFC对话框模版Dlg::FindFile(void) { bWorking = finder.FindFile(path+"*"); if(bWorking==false) MessageBox(L"空目录"); else { ((CListBox *)this->GetDlgItem(IDC_LIST1))->ResetContent(); while (bWorking) { bWorking = finder.FindNextFile(); if(finder.GetFileName()!=L"." && finder.GetFileName()!=L"..") ((CListBox *)this->GetDlgItem(IDC_LIST1))->AddString(finder.GetFileName()); } CTime t = CTime::GetCurrentTime(); buff.Format(L"%d-%02d-%02d.dly",t.GetYear(),t.GetMonth(),t.GetDay()); if(!fun.IsFileExist(path+buff)) ((CListBox *)this->GetDlgItem(IDC_LIST1))->AddString(buff); ((CListBox *)this->GetDlgItem(IDC_LIST1))->SetCurSel(((CListBox *)this->GetDlgItem(IDC_LIST1))->GetCount()-1); } } void CMFC对话框模版Dlg::OnLbnSelchangeList1() { ((CListBox *)this->GetDlgItem(IDC_LIST1))->GetText(((CListBox *)this->GetDlgItem(IDC_LIST1))->GetCurSel(),buff); if(fun.IsFileExist(path+buff)) { fun.ReadFile(buff2,path+buff); this->GetDlgItem(IDC_EDIT1)->SetWindowTextW(buff2); } else this->GetDlgItem(IDC_EDIT1)->SetWindowTextW(Daily); } void CMFC对话框模版Dlg::OnSize(UINT nType, int cx, int cy) { if(this->Ready) this->MoveWindows(); } void CMFC对话框模版Dlg::MoveWindows(void) { GetClientRect(&rect); int i=rect.right; if(rect.right/4>120) rect.right=120; else rect.right=rect.right/4; this->GetDlgItem(IDC_LIST1)->MoveWindow(rect); rect.left=rect.right; rect.right=i; this->GetDlgItem(IDC_EDIT1)->MoveWindow(rect); } BOOL CMFC对话框模版Dlg::PreTranslateMessage(MSG* pMsg) { if(pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_TAB) { this->GetFocus()->SendMessage(WM_USER+10); return 1; } return CDialog::PreTranslateMessage(pMsg); } void CMFC对话框模版Dlg::OnClose() { if(((CListBox *)this->GetDlgItem(IDC_LIST1))->GetCurSel()==((CListBox *)this->GetDlgItem(IDC_LIST1))->GetCount()-1) { ((CListBox *)this->GetDlgItem(IDC_LIST1))->GetText(((CListBox *)this->GetDlgItem(IDC_LIST1))->GetCurSel(),buff); if(!fun.IsFileExist(path+buff)) { this->GetDlgItem(IDC_EDIT1)->GetWindowTextW(buff2); if(buff2!=L"") { CTime t = CTime::GetCurrentTime(); buff2.Format(L"%s\r\n%02d:%02d:%02d",buff2,t.GetHour(),t.GetMinute(),t.GetSecond()); fun.WriteFile(buff2,path+buff); } } } CFile file; if(fun.IsFileExist(L"Setting.txt")) { file.Open(L"Setting.ini",CFile::modeWrite|CFile::typeBinary); } else { file.Open(L"Setting.ini",CFile::modeCreate|CFile::modeWrite|CFile::typeBinary); } this->GetWindowRect(&rect); file.Write((DWORD*)&rect.left,4); file.Write((DWORD*)&rect.top,4); file.Write((DWORD*)&(rect.right-=rect.left),4); file.Write((DWORD*)&(rect.bottom-=rect.top),4); file.Close(); CDialog::OnClose(); } void CMFC对话框模版Dlg::OnEnChangeEdit1() { if(((CListBox *)this->GetDlgItem(IDC_LIST1))->GetCurSel()==((CListBox *)this->GetDlgItem(IDC_LIST1))->GetCount()-1) this->GetDlgItem(IDC_EDIT1)->GetWindowTextW(Daily); }