?? 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 + -