📄 chapter14view.cpp
字号:
// Chapter14View.cpp : implementation of the CChapter14View class
//
#include "stdafx.h"
#include "Chapter14.h"
#include "Chapter14Doc.h"
#include "Chapter14View.h"
#include "FileRW.h"
#include "FleFd.h"
#include "ShellOperation.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define nColumns 20
#define nRows 50
#define STR_LEN 20
/////////////////////////////////////////////////////////////////////////////
// CChapter14View
IMPLEMENT_DYNCREATE(CChapter14View, CListView)
BEGIN_MESSAGE_MAP(CChapter14View, CListView)
//{{AFX_MSG_MAP(CChapter14View)
ON_COMMAND(ID_CFile, OnCFile)
ON_COMMAND(ID_CFileFind, OnCFileFind)
ON_COMMAND(ID_CSdioFile, OnCSdioFile)
ON_COMMAND(ID_shell_operation, Onshelloperation)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CListView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CListView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CListView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChapter14View construction/destruction
CChapter14View::CChapter14View()
{
// TODO: add construction code here
}
CChapter14View::~CChapter14View()
{
}
BOOL CChapter14View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CListView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CChapter14View drawing
void CChapter14View::OnDraw(CDC* pDC)
{
CChapter14Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
void CChapter14View::OnInitialUpdate()
{
// TODO: You may populate your ListView with items by directly accessing
// its list control through a call to GetListCtrl().
CListCtrl *ctl;
//获取列表视所对应的列表控件
ctl=&GetListCtrl();
//设置列表控件的北京颜色
ctl->SetTextBkColor(RGB(192,192,192));
//设置列的题头
ctl->InsertColumn (0, _T("#"), LVCFMT_LEFT, 20);
//插入列头
for (int nColumn = 1; nColumn < nColumns - 1; nColumn++)
{
ctl->InsertColumn (nColumn, CString ((TCHAR)(_T('A') + \
nColumn - 1)), LVCFMT_LEFT, 70);
}
//插入列表记录
for (int i = 0; i < nRows; i++)
{
CString str;
str.Format ("%d", i);
ctl->InsertItem (i, str);
ctl->SetItemData (i, i);
for (nColumn = 1; nColumn < nColumns - 1; nColumn++)
{
str.Format ("Item (%d, %d)", nColumn - 1, i);
ctl->SetItemText (i, nColumn, str);
}
}
//设置列表控件风格
DWORD dwStyle;
dwStyle=::GetWindowLong(ctl->m_hWnd,GWL_STYLE);
dwStyle|=LVS_REPORT|LVS_SHOWSELALWAYS|LVS_EDITLABELS;
::SetWindowLong(ctl->m_hWnd,GWL_STYLE,dwStyle);
dwStyle=ctl->GetExtendedStyle();
dwStyle|= LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT|\
LVS_EX_HEADERDRAGDROP|LVS_EX_TRACKSELECT;
ctl->SetExtendedStyle(dwStyle);
CListView::OnInitialUpdate();
}
/////////////////////////////////////////////////////////////////////////////
// CChapter14View printing
BOOL CChapter14View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CChapter14View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CChapter14View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CChapter14View diagnostics
#ifdef _DEBUG
void CChapter14View::AssertValid() const
{
CListView::AssertValid();
}
void CChapter14View::Dump(CDumpContext& dc) const
{
CListView::Dump(dc);
}
CChapter14Doc* CChapter14View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CChapter14Doc)));
return (CChapter14Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CChapter14View message handlers
void CChapter14View::OnCFile()
{
// TODO: Add your command handler code here
//定义变量
HLOCAL hMem;
char* pbyte;
//提示信息
CString strHint;
CFileRW file;
if(file.DoModal()!=IDOK)
return;
//定义读写文件对象
CFile strFileR,strFileW;
//取得文件名称,只含文件名和最后一个'\'
LPTSTR pstrBack = _tcsrchr(file.m_sourcePath, '\\');
CString strFile,strDesFullPath;
strFile.Format("%s",pstrBack);
//设置写文件的全路径(含文件名)
strDesFullPath=file.m_destinationPath+strFile;
//打开文件
strFileR.Open(file.m_sourcePath,CFile::modeRead|CFile::shareDenyWrite);
//分配内存,将读出的数据放入其中
hMem=LocalAlloc(LHND, strFileR.GetLength()+1);
if (hMem == NULL)
{
//分配内存失败
return;
}
// 锁定内存
pbyte = (char * )LocalLock(hMem);
//初始化新分配的内存,设定初始值为0
memset(pbyte, (BYTE)0, strFileR.GetLength()+1);
//提示信息
strHint.Format("开始读取文件%s",file.m_sourcePath);
AfxMessageBox(strHint);
//读取文件到内存,注意这里是ReadHuge而非Read
strFileR.ReadHuge(pbyte,strFileR.GetLength());
//打开写文件
strFileW.Open(strDesFullPath,CFile::modeCreate|\
CFile::modeWrite|CFile::shareDenyRead);
//写文件,注意这里是WriteHuge而非Write
strFileW.WriteHuge(pbyte,strFileR.GetLength());
//提示信息
strHint.Format("文件写到%s完毕",strDesFullPath);
AfxMessageBox(strHint);
//释放内存
LocalUnlock(pbyte);
LocalFree(hMem);
//关闭文件
strFileR.Close();
strFileW.Close();
}
void CChapter14View::OnCFileFind()
{
// TODO: Add your command handler code here
//声明对话框
CFleFd flfddlg;
if(flfddlg.DoModal()!=IDOK)
return;
}
void CChapter14View::OnCSdioFile()
{
// TODO: Add your command handler code here
CListCtrl *ctl;
//获取列表控件
ctl=&GetListCtrl();
CString strHint;
//打开文件对话框
CFileDialog myFileDlg(FALSE,"txt",_T("CStdioFile写文件"),NULL,
"Text(*.txt)|*.txt|All Files(*.*)|*.*||",NULL);
myFileDlg.m_ofn.lpstrTitle=_T("请选择保存路径:");
//文件名
CString strfilename;
if(myFileDlg.DoModal()!=IDOK)
return;
strfilename=myFileDlg.GetFileName();
CString strSub;
CString str1;
//定义CStdioFile对象
CStdioFile ExportFile;
ExportFile.Open(strfilename,CFile::modeCreate|CFile::modeWrite,NULL);
//访问列表控件成员
int iHdrItemCount;//字段数
int iListItemCount;//记录数
//获取列表头控件
CHeaderCtrl* pHdrCtl=ctl->GetHeaderCtrl();
iHdrItemCount=pHdrCtl->GetItemCount();
iListItemCount=ctl->GetItemCount();
CString strCount;
strCount.Format("共有记录:%d条。",iListItemCount);
CString time;
//GetCurrentTime() 属于类的静态成员函数,因此直接调用
CTime t=CTime::GetCurrentTime();
CString strTime="CStdioFile文件建立于:20%y-%m-%d %H:%M:%S ";
strTime=t.Format(strTime);
strTime+=strCount;
ExportFile.WriteString(strTime+"\n\n");
//提示信息
strHint.Format("开始写文件%s",strfilename);
AfxMessageBox(strHint);
//控件标签文字
LVCOLUMN lvcom;
lvcom.mask = LVCF_TEXT;
lvcom.cchTextMax = _MAX_PATH;
char strCaption[_MAX_PATH];
lvcom.pszText =strCaption;
// lvcom.pszText = new char[_MAX_PATH];
for(int k=0;k<iHdrItemCount;k++)
{
ctl->GetColumn(k,&lvcom);
str1.Format("%s",lvcom.pszText);
strSub+=MakeStr(str1,15);
}
ExportFile.WriteString(strSub+"\n");
strSub="";
int j=ctl->GetItemCount();
int jj=ctl->GetHeaderCtrl()->GetItemCount();
for(int i=0;i<j;i++)
{
for(int k=0;k<jj;k++)
{
str1=ctl->GetItemText(i,k);
strSub+=MakeStr(str1,15);
}
ExportFile.WriteString(strSub+"\n");
//清空更新
strSub="";
}
ExportFile.Close();
//提示信息
strHint.Format("文件写到%s完毕",strfilename);
AfxMessageBox(strHint);
}
CString CChapter14View::MakeStr(CString &str, short size)
{
char buf[STR_LEN];
//将内存全置' '
for(int i=0;i<size;++i)
buf[i]=' ';
CString str1;
str1.Format("%s",buf);
str+=str1;
return str.Left(size);
}
void CChapter14View::Onshelloperation()
{
// TODO: Add your command handler code here
CShellOperation shellOp;
shellOp.DoModal();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -