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

📄 lhwy5323view.cpp

📁 ODBC、Microsoft Access 数据库编程实例
💻 CPP
字号:
// lhwy5323View.cpp : implementation of the CLhwy5323View class
//

#include "stdafx.h"
#include "lhwy5323.h"

#include "lhwy5323Set.h"
#include "lhwy5323Doc.h"
#include "lhwy5323View.h"

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

/////////////////////////////////////////////////////////////////////////////
// CLhwy5323View

IMPLEMENT_DYNCREATE(CLhwy5323View, CRecordView)

BEGIN_MESSAGE_MAP(CLhwy5323View, CRecordView)
	//{{AFX_MSG_MAP(CLhwy5323View)
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	ON_BN_CLICKED(IDC_ALL, OnAll)
	ON_BN_CLICKED(IDC_DELET, OnDelet)
	ON_BN_CLICKED(IDC_EDIT, OnEdit)
	ON_NOTIFY(NM_CLICK, IDC_LIST1, OnClickList1)
	ON_BN_CLICKED(IDC_SORT, OnSort)
	ON_WM_CTLCOLOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLhwy5323View construction/destruction

CLhwy5323View::CLhwy5323View()
	: CRecordView(CLhwy5323View::IDD)
{
	//{{AFX_DATA_INIT(CLhwy5323View)
		// NOTE: the ClassWizard will add member initialization here
	m_pSet = NULL;
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CLhwy5323View::~CLhwy5323View()
{
}

void CLhwy5323View::DoDataExchange(CDataExchange* pDX)
{
	CRecordView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLhwy5323View)
	DDX_Control(pDX, IDC_SORT, m_SortButton);
	DDX_Control(pDX, IDC_LIST1, m_ListCtrl);
	DDX_Control(pDX, IDC_EDIT, m_EditButton);
	DDX_Control(pDX, IDC_DELET, m_DeletButton);
	DDX_Control(pDX, IDC_ALL, m_AllButton);
	DDX_Control(pDX, IDC_ADD, m_AddButton);
	//}}AFX_DATA_MAP
}

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

	return CRecordView::PreCreateWindow(cs);
}

void CLhwy5323View::OnInitialUpdate()
{
	m_pSet = &GetDocument()->m_lhwy5323Set;
	CRecordView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
	//创刷子用来设置界面背景色
    m_Brush=CreateSolidBrush(RGB(30,180,180));
//设表格表题和列的宽度
	m_ListCtrl.SetBkColor(RGB(240,247,233));
	m_ListCtrl.SetTextBkColor(RGB(240,247,233));
    m_ListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
    m_ListCtrl.InsertColumn(0,_T("学号"),LVCFMT_IMAGE|LVCFMT_LEFT);
	m_ListCtrl.InsertColumn(1,_T("姓名"));
	m_ListCtrl.InsertColumn(2,_T("班级"));
	m_ListCtrl.InsertColumn(3,_T("英语"));

	int j;
    for(j=0;j<4;j++)
	{
	  m_ListCtrl.SetColumnWidth(j ,120);
	}
	
//显示所有记录
	this->Show();

}

/////////////////////////////////////////////////////////////////////////////
// CLhwy5323View diagnostics

#ifdef _DEBUG
void CLhwy5323View::AssertValid() const
{
	CRecordView::AssertValid();
}

void CLhwy5323View::Dump(CDumpContext& dc) const
{
	CRecordView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CLhwy5323View database support
CRecordset* CLhwy5323View::OnGetRecordset()
{
	return m_pSet;
}


/////////////////////////////////////////////////////////////////////////////
// CLhwy5323View message handlers
int  CLhwy5323View::Show()
{
	int i=0;
	m_pSet->MoveFirst();
    do
	 {  
		CString s;
        s.Format("%d",m_pSet->m_column1);
        m_ListCtrl.InsertItem(i,s,0);
		m_ListCtrl.SetItemText(i,1,m_pSet->m_column2);
		m_ListCtrl.SetItemText(i,2,m_pSet->m_column3);
        s.Format("%d",m_pSet->m_column4);
        m_ListCtrl.SetItemText(i,3,s);
		i++;
        m_pSet->MoveNext();
		}  while(!m_pSet->IsEOF());
        m_pSet->MoveFirst();
		return i;
}

void CLhwy5323View::OnAdd() 
{
	    CAddDialog dlg;
	if( dlg.DoModal()==IDOK)
	  {  
		if(dlg.m_Xuehao==0)
		{
		 AfxMessageBox("学号不可为零!");
		}
		else
		{
		 m_pSet->AddNew();
	     m_pSet->m_column1=dlg.m_Xuehao;
         m_pSet->m_column2=dlg.m_strName;
	     m_pSet->m_column3=dlg.m_Maths;
		 m_pSet->m_column4=dlg.m_English;
         m_pSet->Update();
	     m_pSet->Requery();
	     m_pSet->MoveLast();

		CString s;
        s.Format("%d",dlg.m_Xuehao);
        m_ListCtrl.InsertItem(0,s,0);
		m_ListCtrl.SetItemText(0,1,dlg.m_strName);
		m_ListCtrl.SetItemText(0,2,dlg.m_Maths);
        s.Format("%d",dlg.m_English);
        m_ListCtrl.SetItemText(0,3,s);
		}
	}   
        m_pSet->MoveFirst();
	

	// TODO: Add your control notification handler code here

}

void CLhwy5323View::OnAll() 
{
	m_ListCtrl.DeleteAllItems();
	
	this->Show();
	// TODO: Add your control notification handler code here
	
}

void CLhwy5323View::OnDelet() 
{
	// TODO: Add your control notification handler code here
	    CDeleteDialog dlg;
	if(dlg.DoModal()==IDOK)
	{
	  if(dlg.m_Password!=1234)
	  AfxMessageBox("你没有权限删除记录!请重输入密码!");
	  else
		{   
		 BOOL b=FALSE;
		 m_pSet->MoveFirst();
		 do
		 {
		  if(dlg.m_Xuehao!=m_pSet->m_column1)
		  m_pSet->MoveNext();
		  else
			{   
			 m_ListCtrl.DeleteAllItems();
			 b=TRUE;
			 m_pSet->Delete();
					
			 m_pSet->Requery();
			 this->Show();
			 m_pSet->MoveFirst();
			 break;
			} 
		}while(!m_pSet->IsEOF());
	          
	if(b==FALSE)
	AfxMessageBox("没有此记录");
	  }
	}
}

void CLhwy5323View::OnEdit() 
{
	// TODO: Add your control notification handler code here
	  CEditDialog dlg;
 
  if(dlg.DoModal()==IDOK)
	{
	  if(dlg.m_Password!=1234)
	  AfxMessageBox("你没有权限更改记录!请重输入密码:");
	  else
		  {   
		   BOOL b=FALSE;
		   m_pSet->MoveFirst();
		   do
			{
			 if(dlg.m_Xuehao!=m_pSet->m_column1)
			 m_pSet->MoveNext();
			 else
				{  	
				m_ListCtrl.DeleteAllItems();
				m_pSet->Edit();
				b=TRUE;
			    m_pSet->m_column1=dlg.m_Xuehao;
				m_pSet->m_column2=dlg.m_strName;
				m_pSet->m_column3=dlg.m_Maths;
				m_pSet->m_column4=dlg.m_English;
				m_pSet->Update();	
				m_pSet->Requery();
				this->Show();
				m_pSet->MoveFirst();
				break;
				} 
			}while(!m_pSet->IsEOF());
	          if(b==FALSE)
			  AfxMessageBox("没有此记录");
		}
	}
}



void CLhwy5323View::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	
	*pResult = 0;
}

void CLhwy5323View::OnSort() 
{
	// TODO: Add your control notification handler code here
	m_ListCtrl.DeleteAllItems();
    m_pSet->m_strSort="[英语]";
	m_pSet->Requery();
	this->Show();	
}

HBRUSH CLhwy5323View::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	return m_Brush;

}

⌨️ 快捷键说明

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