📄 worlddialog.cpp
字号:
// WorldDialog.cpp : implementation file
//
#include "stdafx.h"
#include "SuperWords.h"
#include "WorldDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CWorldDialog dialog
CWorldDialog::CWorldDialog(CWnd* pParent /*=NULL*/)
: CDialog(CWorldDialog::IDD, pParent)
{
//{{AFX_DATA_INIT(CWorldDialog)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CWorldDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWorldDialog)
// NOTE: the ClassWizard will add DDX and DDV calls here
DDX_Control(pDX, IDC_EDIT1, m_edit);
DDX_Control(pDX, IDC_COMBO2, m_weather);
DDX_Control(pDX, IDC_COMBO1, m_week);
DDX_Control(pDX, IDC_DATETIMEPICKER1, m_date);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CWorldDialog, CDialog)
//{{AFX_MSG_MAP(CWorldDialog)
// NOTE: the ClassWizard will add message map macros here
ON_BN_CLICKED(IDC_BUTTON_NEW, OnButtonNew)
ON_BN_CLICKED(IDC_BUTTON_SAVE, OnButtonSave)
ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen)
ON_WM_CTLCOLOR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWorldDialog message handlers
void CWorldDialog::OnButtonNew()
{
// TODO: Add your control notification handler code here
CString szBuffer;
szBuffer.Empty();
m_edit.GetWindowText(szBuffer);
if(!szBuffer.IsEmpty()) //not null
{
if(m_edit.GetModify())
{
int nRes=MessageBox("是否保存当前的日记?","提示",MB_YESNO|MB_ICONQUESTION);
if(nRes==IDYES)
{
//SAVE
TCHAR szTempPath[400];
TCHAR szBuffer[400];
memset(szBuffer,0,sizeof(szBuffer));
memset(szTempPath,0,sizeof(szTempPath));
CSuperWordsApp *pApp = (CSuperWordsApp *)AfxGetApp();
if(::GetModuleFileName(NULL,szBuffer,MAX_PATH)!=0)
{
pApp->GetCurDir(szBuffer,szTempPath);
}
lstrcat(szTempPath,"日记");
//create directory
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
hFind = ::FindFirstFile(szTempPath, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
::CreateDirectory(szTempPath,NULL);
}
else
FindClose(hFind);
//
CString str;
TCHAR szFileName[30];
memset(szFileName,0,sizeof(szFileName));
CTime time;
m_date.GetTime(time);
//str = time.Format(_T("%X"));
wsprintf(szFileName,"%d-%d-%d.dat",time.GetYear(),time.GetMonth(),time.GetDay());
lstrcat(szTempPath,"\\");
lstrcat(szTempPath,szFileName);
CFileDialog dlg(FALSE, "dat", szTempPath, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"日记文件 (*.dat)|*.dat||");
if (dlg.DoModal() == IDCANCEL)
{
return;
}
else
{
CString szPathName;
szPathName.Empty();
szPathName = dlg.GetPathName();
CFile hFile;
CFileException e;
if(!hFile.Open( szPathName, CFile::modeCreate | CFile::modeWrite , &e ) )
{
AfxMessageBox("创建文件失败!",MB_OK);
return;
}
CString szText;
szText.Empty();
//write head
CString szWeek;
szWeek.Empty();
m_week.GetWindowText(szWeek);
CString szWeather;
szWeather.Empty();
m_weather.GetWindowText(szWeather);
hFile.Seek(NULL,CFile::current);
szText.Format("日期:%s %s 天气:%s\r\n",szFileName,szWeek,szWeather);
hFile.Write(szText,szText.GetLength());
szText.Empty();
szText="----------------------------------------------------------------\r\n";
hFile.Seek(NULL,CFile::current);
hFile.Write(szText,szText.GetLength());
szText.Empty();
m_edit.GetWindowText(szText);
hFile.Seek(NULL,CFile::current);
hFile.Write(szText,szText.GetLength());
hFile.Close();
}
}
}
m_edit.SetWindowText("");
m_szFileName.Empty();
}
m_edit.SetFocus();
}
void CWorldDialog::OnButtonSave()
{
// TODO: Add your control notification handler code here
if(m_szFileName.IsEmpty())
{
//SAVE
TCHAR szTempPath[400];
TCHAR szBuffer[400];
memset(szBuffer,0,sizeof(szBuffer));
memset(szTempPath,0,sizeof(szTempPath));
CSuperWordsApp *pApp = (CSuperWordsApp *)AfxGetApp();
if(::GetModuleFileName(NULL,szBuffer,MAX_PATH)!=0)
{
pApp->GetCurDir(szBuffer,szTempPath);
}
lstrcat(szTempPath,"日记");
//create directory
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
hFind = ::FindFirstFile(szTempPath, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
::CreateDirectory(szTempPath,NULL);
}
else
FindClose(hFind);
//
CString str;
TCHAR szFileName[30];
memset(szFileName,0,sizeof(szFileName));
CTime time;
m_date.GetTime(time);
//str = time.Format(_T("%X"));
wsprintf(szFileName,"%d-%d-%d.dat",time.GetYear(),time.GetMonth(),time.GetDay());
lstrcat(szTempPath,"\\");
lstrcat(szTempPath,szFileName);
CFileDialog dlg(FALSE, "dat", szTempPath, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"日记文件 (*.dat)|*.dat||");
if (dlg.DoModal() == IDCANCEL)
{
return;
}
else
{
CString szPathName;
szPathName.Empty();
szPathName = dlg.GetPathName();
m_szFileName.Empty();
m_szFileName = szPathName;
CFile hFile;
CFileException e;
if(!hFile.Open( szPathName, CFile::modeCreate | CFile::modeWrite , &e ) )
{
AfxMessageBox("创建文件失败!",MB_OK);
return;
}
CString szText;
szText.Empty();
//write head
CString szWeek;
szWeek.Empty();
m_week.GetWindowText(szWeek);
CString szWeather;
szWeather.Empty();
m_weather.GetWindowText(szWeather);
hFile.Seek(NULL,CFile::current);
szText.Format("日期:%s %s 天气:%s\r\n",szFileName,szWeek,szWeather);
hFile.Write(szText,szText.GetLength());
szText.Empty();
szText="----------------------------------------------------------------\r\n";
hFile.Seek(NULL,CFile::current);
hFile.Write(szText,szText.GetLength());
szText.Empty();
m_edit.GetWindowText(szText);
hFile.Seek(NULL,CFile::current);
hFile.Write(szText,szText.GetLength());
hFile.Close();
if(IDYES==MessageBox("保存成功,开始新的日记?","提示",MB_YESNO|MB_ICONQUESTION))
{
m_edit.SetWindowText("");
m_edit.SetFocus();
m_szFileName.Empty();
}
}
}
else
{
//SAVE
CString szPathName;
szPathName.Empty();
CFile hFile;
CFileException e;
if(!hFile.Open( m_szFileName, CFile::modeCreate | CFile::modeWrite , &e ) )
{
AfxMessageBox("创建文件失败!",MB_OK);
return;
}
CString szText;
szText.Empty();
CString szFile;
TCHAR ch='\\';
int n=m_szFileName.ReverseFind(ch);
if(n!=-1)
{
szFile = m_szFileName.Mid(n+1);
}
//write head
CString szWeek;
szWeek.Empty();
m_week.GetWindowText(szWeek);
CString szWeather;
szWeather.Empty();
m_weather.GetWindowText(szWeather);
szText.Empty();
m_edit.GetWindowText(szText);
hFile.Seek(NULL,CFile::current);
hFile.Write(szText,szText.GetLength());
hFile.Close();
if(IDYES==MessageBox("保存成功,开始新的日记?","提示",MB_YESNO|MB_ICONQUESTION))
{
m_edit.SetWindowText("");
m_edit.SetFocus();
m_szFileName.Empty();
}
}
}
void CWorldDialog::OnButtonOpen()
{
// TODO: Add your control notification handler code here
CSuperWordsApp *pApp = (CSuperWordsApp *)AfxGetApp();
CString szBuffer;
szBuffer.Empty();
CString szFileName;
szFileName.Empty();
// int iReturn;
TCHAR szTempBuffer[MAX_PATH]={0};
TCHAR szCurDir[MAX_PATH]={0};
memset(szTempBuffer,0,sizeof(szTempBuffer));
if(::GetModuleFileName(NULL,szTempBuffer,MAX_PATH)!=0)
{
pApp->GetCurDir(szTempBuffer,szCurDir);
}
lstrcat(szCurDir,"日记");
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
hFind = ::FindFirstFile(szCurDir, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
::CreateDirectory(szCurDir,NULL);
}
else
FindClose(hFind);
lstrcat(szCurDir,"\\");
::SetCurrentDirectory(szCurDir);
OPENFILENAME of;
TCHAR szFileName1 [MAX_PATH] = {0};
const LPTSTR pszOpenFilter = TEXT ("日记文件 (*.dat)\0*.dat\0\0");
INT rc;
HANDLE hFile;
memset (&of, 0, sizeof (of));
of.lStructSize = sizeof (of);
of.hwndOwner = NULL;
of.lpstrFile = szFileName1;
of.nMaxFile = sizeof(szFileName1);
of.lpstrFilter = pszOpenFilter;
of.Flags = 0;
rc = GetOpenFileName (&of);
hFile = CreateFile(szFileName1,
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING ,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (INVALID_HANDLE_VALUE==hFile)
{
return;
}
m_edit.SetWindowText("");
m_szFileName.Empty();
m_szFileName = szFileName1;
DWORD dwBytes;
DWORD dwFileSize = GetFileSize(hFile,NULL);
TCHAR *szText;
szText = new TCHAR[dwFileSize];
ReadFile (hFile, szText, dwFileSize, (LPDWORD)&dwBytes, 0);
m_edit.SetWindowText(szText);
CloseHandle(hFile);
delete szText;
}
BOOL CWorldDialog::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
//button
m_btnNew.SubclassDlgItem(IDC_BUTTON_NEW, this);
m_btnNew.SetIcon(IDI_ICON16);
m_btnSave.SubclassDlgItem(IDC_BUTTON_SAVE, this);
m_btnSave.SetIcon(IDI_ICON11);
m_btnOpen.SubclassDlgItem(IDC_BUTTON_OPEN, this);
m_btnOpen.SetIcon(IDI_ICON12);
//WEEK
m_week.AddString("星期日");
m_week.AddString("星期一");
m_week.AddString("星期二");
m_week.AddString("星期三");
m_week.AddString("星期四");
m_week.AddString("星期五");
m_week.AddString("星期六");
SYSTEMTIME sysTime;
memset(&sysTime, 0, sizeof(sysTime));
m_date.GetTime(&sysTime);
m_week.SetCurSel(sysTime.wDayOfWeek);
//weather
m_weather.AddString("晴");
m_weather.AddString("阴");
m_weather.AddString("雨");
m_weather.AddString("雾");
m_weather.AddString("雪");
m_weather.SetCurSel(0);
m_szFileName.Empty();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
HBRUSH CWorldDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
// TODO: Return a different brush if the default is not desired
return hbr;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -