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

📄 dao_example2view.cpp

📁 Visual C++数据库编程源代码 《Visual C++程序员成长攻略》一书的附带源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// DAO_EXAMPLE2View.cpp : implementation of the CDAO_EXAMPLE2View class
//

#include "stdafx.h"
#include "DAO_EXAMPLE2.h"

#include "DAO_EXAMPLE2Doc.h"
#include "DAO_EXAMPLE2View.h"

//"加入自定义代码"
#include "SearchRecord.h"
#include "ModifyRecord.h"
#include "AddRecord.h"
#include "AddUser.h"
#include "DeleteUser.h"
#include "ManageAffirm.h"
#include "PasswordModify.h"
#include "UserManage.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDAO_EXAMPLE2View

IMPLEMENT_DYNCREATE(CDAO_EXAMPLE2View, CListView)

BEGIN_MESSAGE_MAP(CDAO_EXAMPLE2View, CListView)
	//{{AFX_MSG_MAP(CDAO_EXAMPLE2View)
	ON_COMMAND(ID_ADD_RECORD, OnAddRecord)
	ON_COMMAND(ID_DELETE_RECORD, OnDeleteRecord)
	ON_UPDATE_COMMAND_UI(ID_DELETE_RECORD, OnUpdateDeleteRecord)
	ON_COMMAND(ID_MODIFY_RECORD, OnModifyRecord)
	ON_UPDATE_COMMAND_UI(ID_MODIFY_RECORD, OnUpdateModifyRecord)
	ON_COMMAND(ID_SEARCH_RECORD, OnSearchRecord)
	ON_COMMAND(ID_ADD_USER, OnAddUser)
	ON_COMMAND(ID_DELETE_USER, OnDeleteUser)
	ON_COMMAND(ID_PASSWORD_MODIFY, OnPasswordModify)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CDAO_EXAMPLE2View construction/destruction

CDAO_EXAMPLE2View::CDAO_EXAMPLE2View()
{
	// TODO: add construction code here
	m_flag2 = false;
}

CDAO_EXAMPLE2View::~CDAO_EXAMPLE2View()
{
}

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

	return CListView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CDAO_EXAMPLE2View drawing

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

void CDAO_EXAMPLE2View::OnInitialUpdate()
{
	CListView::OnInitialUpdate();
	//"加入自定义代码"
	CListCtrl &example=GetListCtrl();
	example.ModifyStyle(0,LVS_REPORT|LVS_SINGLESEL);  //设置风格
	example.SetExtendedStyle(LVS_EX_FLATSB|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
	m_exp1=&GetDocument()->m_data1;              //初始化指针
	m_exp2=&GetDocument()->m_data2;
	if(m_exp1->IsOpen())
		m_exp1->Close();    
	m_exp1->Open();                              //打开数据库
	CString str;
	str = "学生编号";
	example.InsertColumn(0,str);                  //插入各列
	example.InsertColumn(1,"学生姓名");
    example.InsertColumn(2,"英语成绩");
	example.InsertColumn(3,"数学成绩");
	example.InsertColumn(4,"语文成绩");
	example.InsertColumn(5,"历史成绩");
	example.InsertColumn(6,"政治成绩");
	example.InsertColumn(7,"平均成绩");
	m_flag2=TRUE;
	ShowStudentInformation();                       //自定义显示函数
}

void CDAO_EXAMPLE2View::ShowStudentInformation()
{
	CListCtrl &example=GetListCtrl();
	example.DeleteAllItems();         //删除所有内容
	m_exp1->MoveFirst();            //指向首条记录
	int i=0,k;
	int ColWidth[10];
	for(k=0;k<10;k++)
		ColWidth[k]=0;
	while(!m_exp1->IsEOF())         //如果到达数据末尾结束循环
	{
		LV_ITEM Item;		Item.mask = LVIF_TEXT;
		Item.iItem = i;		Item.iSubItem=0;
		CString str;	    str.Format("%d",i+1);   //编号
		Item.pszText=(LPTSTR)(LPCTSTR)str;
		if(example.GetStringWidth(Item.pszText)>ColWidth[0])
			ColWidth[0]=example.GetStringWidth(Item.pszText);
		example.InsertItem(&Item);
	    //显示各列具体内容
		Item.iSubItem = 1;    
		Item.pszText=(LPTSTR)(LPCTSTR)(m_exp1->m_name);
		if(example.GetStringWidth(Item.pszText)>ColWidth[1])
			ColWidth[1]=example.GetStringWidth(Item.pszText);
		example.SetItem(&Item);

		Item.iSubItem=2;
		Item.pszText=(LPTSTR)(LPCTSTR)(m_exp1->m_englishscore);
		if(example.GetStringWidth(Item.pszText)>ColWidth[2])
			ColWidth[2]=example.GetStringWidth(Item.pszText);
		example.SetItem(&Item);
		
		Item.iSubItem=3;
		Item.pszText=(LPTSTR)(LPCTSTR)(m_exp1->m_mathscore);
		if(example.GetStringWidth(Item.pszText)>ColWidth[3])
			ColWidth[3]=example.GetStringWidth(Item.pszText);
		example.SetItem(&Item);

		Item.iSubItem=4;
		Item.pszText=(LPTSTR)(LPCTSTR)(m_exp1->m_chinesescore);
		if(example.GetStringWidth(Item.pszText)>ColWidth[4])
			ColWidth[4]=example.GetStringWidth(Item.pszText);
		example.SetItem(&Item);

		Item.iSubItem=5;
		Item.pszText=(LPTSTR)(LPCTSTR)(m_exp1->m_historyscore);
		if(example.GetStringWidth(Item.pszText)>ColWidth[5])
			ColWidth[5]=example.GetStringWidth(Item.pszText);
		example.SetItem(&Item);

        Item.iSubItem=6;
		Item.pszText=(LPTSTR)(LPCTSTR)(m_exp1->m_politicalscore);
		if(example.GetStringWidth(Item.pszText)>ColWidth[6])
			ColWidth[6]=example.GetStringWidth(Item.pszText);
		example.SetItem(&Item);

		Item.iSubItem=7;
		Item.pszText=(LPTSTR)(LPCTSTR)(m_exp1->m_averagescore);
		if(example.GetStringWidth(Item.pszText)>ColWidth[7])
			ColWidth[7]=example.GetStringWidth(Item.pszText);
		example.SetItem(&Item);
		i++;
		m_exp1->MoveNext();           //指向下条记录
	}
	//设置各列宽度
	for(int j=0;j<8;j++)
		example.SetColumnWidth(j,ColWidth[j]+85);
    m_exp1->MoveFirst();              //再指向首条记录
}

/////////////////////////////////////////////////////////////////////////////
// CDAO_EXAMPLE2View printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CDAO_EXAMPLE2View diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CDAO_EXAMPLE2View message handlers

void CDAO_EXAMPLE2View::OnAddRecord() 
{
	// "加入自定义代码"
	if(UserAffirm()==0)
		return;       //用户资格确认
	else
	{
        CAddRecord Dlg;
        if(Dlg.DoModal()==IDOK)
        {
        	CString name;
        	name=Dlg.m_name;
        	if(Dlg.m_name.IsEmpty())
	        {
	        AfxMessageBox("确定添加如上信息?",MB_ICONEXCLAMATION);
	        OnAddRecord();
		    Dlg.m_name=name;
            }
			else
			{
			Dlg.m_name.TrimLeft(" ");      //去除无效空格字符
	        Dlg.m_name.TrimRight(" ");
	        Dlg.m_english_score.TrimLeft(" ");
	        Dlg.m_english_score.TrimRight(" ");
	        Dlg.m_math_score.TrimLeft(" ");
        	Dlg.m_math_score.TrimRight(" ");
        	Dlg.m_chinese_score.TrimLeft(" ");
        	Dlg.m_chinese_score.TrimRight(" ");
        	Dlg.m_history_score.TrimLeft(" ");
        	Dlg.m_history_score.TrimRight(" ");
        	Dlg.m_political_score.TrimLeft(" ");
        	Dlg.m_political_score.TrimRight(" ");
        	Dlg.m_average_score.TrimLeft(" ");
        	Dlg.m_average_score.TrimRight(" ");
			//增加具体数据
	        m_exp1->AddNew();
			m_exp1->m_name=Dlg.m_name;
        	m_exp1->m_englishscore=Dlg.m_english_score;
        	m_exp1->m_mathscore=Dlg.m_math_score;
	        m_exp1->m_chinesescore=Dlg.m_chinese_score;
        	m_exp1->m_historyscore=Dlg.m_history_score;
        	m_exp1->m_politicalscore=Dlg.m_political_score;
	        m_exp1->m_averagescore=Dlg.m_average_score;
        	m_exp1->Update();
        	ShowStudentInformation();     //刷新显示所增加数据
			}
		}
	}
}

void CDAO_EXAMPLE2View::OnDeleteRecord() 
{
	//"加入自定义代码"
	if(UserAffirm()==0)     	return;
	else
	{
        CListCtrl &example=GetListCtrl();
        POSITION position=example.GetFirstSelectedItemPosition();
        if(position==NULL)
		{
	        AfxMessageBox("对不起,您还没有选中记录!",MB_ICONEXCLAMATION);
	        return;
		}
        int m_item=example.GetNextSelectedItem(position);
        m_exp1->Move(m_item);
        CString str1;
		str1 = "您确定要删除该条记录吗?";
        if(AfxMessageBox(str1,MB_YESNO|MB_ICONEXCLAMATION) ==IDYES)
		{
        	m_exp1->Delete();
	        m_exp1->MoveFirst();
	        ShowStudentInformation();
		}
	}
}

void CDAO_EXAMPLE2View::OnUpdateDeleteRecord(CCmdUI* pCmdUI) 
{
	// "加入自定义代码"
	// 点击记录才能进行删除
	CListCtrl &example = GetListCtrl();
	POSITION  position  = example.GetFirstSelectedItemPosition();
	if(position==NULL)
		pCmdUI->Enable(FALSE);
	else        		   pCmdUI->Enable();

⌨️ 快捷键说明

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