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

📄 abcview.cpp

📁 金山词霸界面的数据库管
💻 CPP
字号:
// AbcView.cpp : implementation of the CAbcView class
//

#include "stdafx.h"
#include "Abc.h"

#include "AbcDoc.h"
#include "AbcView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAbcView

IMPLEMENT_DYNCREATE(CAbcView, CListView)

BEGIN_MESSAGE_MAP(CAbcView, CListView)
	//{{AFX_MSG_MAP(CAbcView)
	ON_NOTIFY_REFLECT(LVN_COLUMNCLICK, OnColumnclick)
	ON_COMMAND(ID_SET_FONT, OnSetFont)
	ON_COMMAND(ID_RECORD_ADD, OnRecordAdd)
	ON_COMMAND(ID_RECORD_SEARCH, OnRecordSearch)
	ON_COMMAND(ID_RECORD_SHOWALL, OnRecordShowall)
	ON_UPDATE_COMMAND_UI(ID_RECORD_SHOWALL, OnUpdateRecordShowall)
	ON_COMMAND(ID_RECORD_DEL, OnRecordDel)
	ON_COMMAND(ID_RECORD_EDIT, OnRecordEdit)
	ON_UPDATE_COMMAND_UI(ID_RECORD_DEL, OnUpdateRecordDel)
	ON_UPDATE_COMMAND_UI(ID_RECORD_EDIT, OnUpdateRecordEdit)
	ON_COMMAND(ID_EXPORTDATA, OnExportData)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CAbcView construction/destruction

CAbcView::CAbcView()
{
	// TODO: add construction code here

}

CAbcView::~CAbcView()
{
}

BOOL CAbcView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CListView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CAbcView drawing

void CAbcView::OnDraw(CDC* pDC)
{
	CAbcDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

void CAbcView::OnInitialUpdate()
{
	CListView::OnInitialUpdate();


	// TODO: You may populate your ListView with items by directly accessing
	//  its list control through a call to GetListCtrl().
	CListCtrl &ctl=GetListCtrl();
	ctl.ModifyStyle(0,LVS_REPORT|LVS_SINGLESEL);
	ctl.SetExtendedStyle(LVS_EX_FLATSB|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
	
	
	m_pSet=&GetDocument()->m_dataSet;
	if(m_pSet->IsOpen())
		m_pSet->Close();
	m_pSet->Open();
    GetDocument()->SetTitle(m_pSet->m_pCfgInfo->szDaoFilePath);	
	m_pFont=new CFont;
	m_pFont->CreateFontIndirect(&(m_pSet->m_pCfgInfo->lfViewFont));
	ctl.SetFont(m_pFont);
	ctl.SetTextColor(m_pSet->m_pCfgInfo->crFontColor);	
	
	CString str;
	str.LoadString(IDS_SERIAL);
	ctl.InsertColumn(0,str);
	
	CDaoFieldInfo fieldInfo;
	for(short i=0;i<m_pSet->m_nFields;i++)
	{
	    m_pSet->GetFieldInfo(i,fieldInfo);
		ctl.InsertColumn(i+1,fieldInfo.m_strName);
	}
	
	m_bIsAsc=TRUE;
	m_bIsShowAll=TRUE;
	ShowTableData();
}

/////////////////////////////////////////////////////////////////////////////
// CAbcView printing

BOOL CAbcView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CAbcView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CAbcView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CAbcView diagnostics

#ifdef _DEBUG
void CAbcView::AssertValid() const
{
	CListView::AssertValid();
}

void CAbcView::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}

CAbcDoc* CAbcView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAbcDoc)));
	return (CAbcDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CAbcView message handlers

void CAbcView::ShowTableData()
{
	CListCtrl &ctl=GetListCtrl();
	ctl.DeleteAllItems();

	m_pSet->MoveFirst();
	int i=0;
	int nMaxWidthColumn0=0,nMaxWidthColumn1=0,nMaxWidthColumn2=0;
	while(!m_pSet->IsEOF())
	{
		LV_ITEM lvItem;
		lvItem.mask = LVIF_TEXT;
		lvItem.iItem = i;
		
		lvItem.iSubItem=0;
		CString str;
		str.Format("%d",i+1); //序号列
		lvItem.pszText=(LPTSTR)(LPCTSTR)str;
		if(ctl.GetStringWidth(lvItem.pszText)>nMaxWidthColumn0)
			nMaxWidthColumn0=ctl.GetStringWidth(lvItem.pszText);
		ctl.InsertItem(&lvItem);
	
		lvItem.iSubItem = 1;
		lvItem.pszText=(LPTSTR)(LPCTSTR)(m_pSet->m_column1);
		if(ctl.GetStringWidth(lvItem.pszText)>nMaxWidthColumn1)
			nMaxWidthColumn1=ctl.GetStringWidth(lvItem.pszText);
		ctl.SetItem(&lvItem);
		
		lvItem.iSubItem=2;
		lvItem.pszText=(LPTSTR)(LPCTSTR)(m_pSet->m_column2);
		if(ctl.GetStringWidth(lvItem.pszText)>nMaxWidthColumn2)
			nMaxWidthColumn2=ctl.GetStringWidth(lvItem.pszText);
		ctl.SetItem(&lvItem);
		i++;
		m_pSet->MoveNext();
	}
	ctl.SetColumnWidth(0,nMaxWidthColumn0+30);
	ctl.SetColumnWidth(1,nMaxWidthColumn1+30);
    ctl.SetColumnWidth(2,nMaxWidthColumn2+30);
    m_pSet->MoveFirst();

}

void CAbcView::Sort(BOOL isAsc)
{
	CDaoFieldInfo fieldInfo;
	m_pSet->GetFieldInfo(0,fieldInfo);
	if(isAsc)
	{
		m_pSet->m_strSort=fieldInfo.m_strName+" ASC";
		m_bIsAsc=TRUE;
	}
	else
	{
		m_pSet->m_strSort=fieldInfo.m_strName+" DESC";
		m_bIsAsc=FALSE;
	}
	m_pSet->Requery();	

}

void CAbcView::OnColumnclick(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	if(pNMListView->iSubItem==1)
	{
		Sort(!m_bIsAsc);
		ShowTableData();
	}

	*pResult = 0;
}

void CAbcView::OnSetFont() 
{
	// TODO: Add your command handler code here
    CListCtrl &ctl=GetListCtrl();
	
	LOGFONT lf;
	GetFont()->GetLogFont(&lf);
    CFontDialog fdlg(&lf);
	fdlg.m_cf.rgbColors=ctl.GetTextColor();
	if(fdlg.DoModal()==IDOK)
	{
			m_pFont=new CFont;
			m_pFont->CreateFontIndirect(fdlg.m_cf.lpLogFont);
			SetFont(m_pFont);
			m_pSet->m_pCfgInfo->lfViewFont=*(fdlg.m_cf.lpLogFont);
		
			ctl.SetTextColor(fdlg.m_cf.rgbColors);
			m_pSet->m_pCfgInfo->lfViewFont=*(fdlg.m_cf.lpLogFont);
		
			m_pSet->UpdateCfgInfo();
		
	}
	
}

void CAbcView::OnRecordAdd() 
{
	// TODO: Add your command handler code here
	CRecordAddDlg addDlg;
	if(addDlg.DoModal()==IDOK)
	{
		if(addDlg.m_strEnglish.IsEmpty())
			return;
		addDlg.m_strEnglish.TrimLeft();
		addDlg.m_strEnglish.TrimRight();
		addDlg.m_strChinese.TrimLeft();
		addDlg.m_strChinese.TrimRight();
		m_pSet->AddNew();
		m_pSet->m_column1=addDlg.m_strEnglish;
		m_pSet->m_column2=addDlg.m_strChinese;
		m_pSet->Update();
		Sort(m_bIsAsc);
		ShowTableData();
	}
	
}

void CAbcView::OnRecordSearch() 
{
	// TODO: Add your command handler code here
	CRecordSearchDlg searchDlg;
	if(searchDlg.DoModal()==IDOK)
	{
		if(searchDlg.m_strEnglish.IsEmpty())
			return;
        ASSERT(m_pSet->CanRestart());
		searchDlg.m_strEnglish.MakeLower();
		m_bIsShowAll=FALSE;
		CDaoFieldInfo fieldInfo;
	    m_pSet->GetFieldInfo(0,fieldInfo);
		m_pSet->m_strFilter=fieldInfo.m_strName+" = "+"'"+searchDlg.m_strEnglish+"'";
	    m_pSet->Requery();
		ShowTableData();
	
	}
	
	
}
//显示数据库中的全部数据
void CAbcView::OnRecordShowall() 
{
	// TODO: Add your command handler code here
	if(m_bIsShowAll)
		return;
	m_pSet->m_strFilter.Empty();
	m_pSet->Requery();
	ShowTableData();
	m_bIsShowAll=TRUE;
}

void CAbcView::OnUpdateRecordShowall(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(m_bIsShowAll)
		pCmdUI->Enable(FALSE);
	else
		pCmdUI->Enable();

}

void CAbcView::OnRecordDel() 
{
	// TODO: Add your command handler code here
	CListCtrl &ctl=GetListCtrl();
	POSITION pos=ctl.GetFirstSelectedItemPosition();
	if(pos==NULL)
		return;
	int nItem=ctl.GetNextSelectedItem(pos);
	m_pSet->Move(nItem);
	CString str1,str2;
	str1.LoadString(IDS_WARN_1);
	str2.LoadString(IDS_WARN_2);
	str1=str1+m_pSet->m_column1+": "+m_pSet->m_column2+str2;
	
	if(AfxMessageBox(str1,MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
	{
		m_pSet->Delete();
		m_pSet->MoveFirst();
		ShowTableData();
	}
}

void CAbcView::OnRecordEdit() 
{
	// TODO: Add your command handler code here
	CListCtrl &ctl=GetListCtrl();
	POSITION pos=ctl.GetFirstSelectedItemPosition();
	if(pos==NULL)
		return;
	int nItem=ctl.GetNextSelectedItem(pos);
	m_pSet->Move(nItem);
	CRecordEditDlg editDlg;
	editDlg.m_strEnglish=m_pSet->m_column1;
	editDlg.m_strChinese=m_pSet->m_column2;

	if(editDlg.DoModal()==IDOK)
	{
		m_pSet->Edit();
		m_pSet->m_column1=editDlg.m_strEnglish;
		m_pSet->m_column2=editDlg.m_strChinese;
		m_pSet->Update();
		ShowTableData();
	}
}

void CAbcView::OnUpdateRecordDel(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	CListCtrl &ctl=GetListCtrl();
	POSITION pos=ctl.GetFirstSelectedItemPosition();
	if(pos==NULL)
		pCmdUI->Enable(FALSE);
	else
		pCmdUI->Enable();
}

void CAbcView::OnUpdateRecordEdit(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	CListCtrl &ctl=GetListCtrl();
	POSITION pos=ctl.GetFirstSelectedItemPosition();
	if(pos==NULL)
		pCmdUI->Enable(FALSE);
	else
		pCmdUI->Enable();
}





void CAbcView::OnExportData() 
{
	// TODO: Add your command handler code here
	
	CString strFilter;
	strFilter.LoadString(IDS_SAVE_FILEFILTER);
    CString strExt;
	strExt.LoadString(IDS_SAVE_DEFAULTEXT);
	CFileDialog flDlg(FALSE,strExt,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,strFilter);
	CString  strTitle;
	strTitle.LoadString(IDS_SAVE_FILEDIALOG_TITLE);
	flDlg.m_ofn.lpstrTitle=strTitle;

	if(flDlg.DoModal()==IDOK)
	{
		CString strFilePath=flDlg.GetPathName();
		CStdioFile file;
		if(!file.Open(strFilePath,CFile::modeCreate|CFile::modeReadWrite))
			return;
		long pos=m_pSet->GetAbsolutePosition();
		long count=m_pSet->GetRecordCount();
	    if(count>0)
		{
			m_pSet->MoveFirst();
			CDaoFieldInfo fieldInfo;
			m_pSet->GetFieldInfo(0,fieldInfo);
			long length=fieldInfo.m_lSize;
			while(!m_pSet->IsEOF())
			{
				CString str1,str2;
				str1=m_pSet->m_column1;
				str2=m_pSet->m_column2;
				str1.TrimLeft(); 
				str2.TrimLeft(); str2.TrimRight();

				int n=str1.GetLength();
				if(n<length)
					for(long j=0;j<=length-n;j++)
						str1+=" ";
				
				CString strWrite;
				strWrite=str1+str2+"\n";
				file.WriteString(strWrite);
				m_pSet->MoveNext();
			}
			m_pSet->SetAbsolutePosition(pos);
			file.Close();
		}
	}
		

}

⌨️ 快捷键说明

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