www.pudn.com > lhwy_odbc2.zip > lhwyView.cpp, change:2002-10-09,size:8511b


// lhwyView.cpp : implementation of the CLhwyView class 
// 
 
#include "stdafx.h" 
#include "lhwy.h" 
 
#include "lhwySet.h" 
#include "lhwyDoc.h" 
#include "lhwyView.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CLhwyView 
 
IMPLEMENT_DYNCREATE(CLhwyView, CRecordView) 
 
BEGIN_MESSAGE_MAP(CLhwyView, CRecordView) 
	//{{AFX_MSG_MAP(CLhwyView) 
	ON_BN_CLICKED(IDC_ADD, OnAdd) 
	ON_BN_CLICKED(IDC_DELECT, OnDelect) 
	ON_BN_CLICKED(IDC_FIND, OnFind) 
	ON_BN_CLICKED(IDC_EDIT, OnEdit) 
	ON_BN_CLICKED(IDC_ALL, OnAll) 
	ON_WM_CTLCOLOR() 
	ON_NOTIFY(NM_CLICK, IDC_LIST1, OnClickList1) 
	ON_BN_CLICKED(IDC_SORT, OnSort) 
	//}}AFX_MSG_MAP 
	// Standard printing commands 
	ON_COMMAND(ID_FILE_PRINT, CRecordView::OnFilePrint) 
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CRecordView::OnFilePrint) 
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRecordView::OnFilePrintPreview) 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CLhwyView construction/destruction 
 
CLhwyView::CLhwyView() 
	: CRecordView(CLhwyView::IDD) 
{ 
	//{{AFX_DATA_INIT(CLhwyView) 
		// NOTE: the ClassWizard will add member initialization here 
	m_pSet = NULL; 
	//}}AFX_DATA_INIT 
	// TODO: add construction code here 
 
} 
 
CLhwyView::~CLhwyView() 
{ 
} 
 
void CLhwyView::DoDataExchange(CDataExchange* pDX) 
{ 
	CRecordView::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CLhwyView) 
	DDX_Control(pDX, IDC_SORT, m_SortButton); 
	DDX_Control(pDX, IDC_FIND, m_FindButton); 
	DDX_Control(pDX, IDC_EDIT, m_EditButton); 
	DDX_Control(pDX, IDC_DELECT, m_DelectButton); 
	DDX_Control(pDX, IDC_ALL, m_AllButton); 
	DDX_Control(pDX, IDC_ADD, m_AddButton); 
	DDX_Control(pDX, IDC_LIST1, m_ListCtrl); 
	 
	//}}AFX_DATA_MAP 
} 
 
BOOL CLhwyView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
 
	return CRecordView::PreCreateWindow(cs); 
} 
 
void CLhwyView::OnInitialUpdate() 
{ 
	m_pSet = &GetDocument()->m_lhwySet; 
	CRecordView::OnInitialUpdate(); 
	GetParentFrame()->RecalcLayout(); 
	ResizeParentToFit(); 
 
     
//创刷子用来设置界面背景色 
    m_Brush=CreateSolidBrush(RGB(30,180,180)); 
//设表格表题和列的宽度 
 
    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 ,90); 
	} 
	 
//显示所有记录 
	this->Show(); 
 
 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CLhwyView printing 
 
BOOL CLhwyView::OnPreparePrinting(CPrintInfo* pInfo) 
{ 
	// default preparation 
	return DoPreparePrinting(pInfo); 
} 
 
void CLhwyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add extra initialization before printing 
} 
 
void CLhwyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add cleanup after printing 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CLhwyView diagnostics 
 
#ifdef _DEBUG 
void CLhwyView::AssertValid() const 
{ 
	CRecordView::AssertValid(); 
} 
 
void CLhwyView::Dump(CDumpContext& dc) const 
{ 
	CRecordView::Dump(dc); 
} 
 
CLhwyDoc* CLhwyView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CLhwyDoc))); 
	return (CLhwyDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CLhwyView database support 
CRecordset* CLhwyView::OnGetRecordset() 
{ 
	return m_pSet; 
} 
 
 
///////////////////////////////////////////////////////////////////////////// 
// CLhwyView message handlers 
int  CLhwyView::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); 
        s.Format("%d",m_pSet->m_column3); 
	    m_ListCtrl.SetItemText(i,2,s); 
        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 CLhwyView::OnAdd()  
{ 
	// TODO: Add your control notification handler code here 
	 
    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); 
		s.Format("%d",dlg.m_Maths); 
		m_ListCtrl.SetItemText(0,2,s); 
        s.Format("%d",dlg.m_English); 
        m_ListCtrl.SetItemText(0,3,s); 
		} 
	}    
        m_pSet->MoveFirst(); 
	 
 
   } 
 
//删除记录 
void CLhwyView::OnDelect()  
{ 
	// 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 CLhwyView::OnFind()  
{ 
	// TODO: Add your control notification handler code here 
 
	BOOL k=FALSE; 
	CFindDialog dlg; 
	if(dlg.DoModal()==IDOK) 
	{ 
	  m_pSet->MoveFirst(); 
	  m_ListCtrl.DeleteAllItems(); 
	  do{ 
		if(dlg.m_Maths!=m_pSet->m_column3) 
		m_pSet->MoveNext(); 
		else 
			{ 
			 k=TRUE; 
		     CString s; 
			 s.Format("%d",m_pSet->m_column1); 
			 m_ListCtrl.InsertItem(0,s); 
			 m_ListCtrl.SetItemText(0,1,m_pSet->m_column2); 
			 s.Format("%d",m_pSet->m_column3);  
		     m_ListCtrl.SetItemText(0,2,s); 
             s.Format("%d",m_pSet->m_column3); 
             m_ListCtrl.SetItemText(0,3,s);   
			 m_pSet->MoveNext(); 
		}  
 
	}  while(!m_pSet->IsEOF()); 
         
	if(k==FALSE) 
	AfxMessageBox("没有符和条件的记录"); 
	} 
} 
 
//修改记录 
void CLhwyView::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 CLhwyView::OnAll()  
{ 
	// TODO: Add your control notification handler code here 
	m_ListCtrl.DeleteAllItems(); 
	 
	this->Show(); 
} 
 
//排序记录 
void CLhwyView::OnSort()  
{ 
	// TODO: Add your control notification handler code here 
	m_ListCtrl.DeleteAllItems(); 
    m_pSet->m_strSort="[学号]"; 
	m_pSet->Requery(); 
	 
	this->Show(); 
	 
	 
} 
 
//设置界面为草绿色 
HBRUSH CLhwyView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)  
{ 
	return m_Brush; 
} 
 
void CLhwyView::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)  
{ 
	// TODO: Add your control notification handler code here 
	POSITION pos =m_ListCtrl.GetFirstSelectedItemPosition(); 
	if(pos) 
	{    
	 
		int nItem = m_ListCtrl.GetNextSelectedItem(pos); 
		CString s; 
		s.Format("学号:%s 姓名:%s",m_ListCtrl.GetItemText(nItem,0),m_ListCtrl.GetItemText(nItem,1)); 
	 
		GetDlgItem(IDC_STATIC)->SetWindowText(s); 
		 
		} 
	 
	 
	*pResult = 0; 
}