📄 ex_odbcview.cpp
字号:
// Ex_ODBCView.cpp : implementation of the CEx_ODBCView class
//
#include "stdafx.h"
#include "Ex_ODBC.h"
#include "Ex_ODBCSet.h"
#include "Ex_ODBCDoc.h"
#include "Ex_ODBCView.h"
#include "MainFrm.h"
#include "StuDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CEx_ODBCView
IMPLEMENT_DYNCREATE(CEx_ODBCView, CRecordView)
BEGIN_MESSAGE_MAP(CEx_ODBCView, CRecordView)
//{{AFX_MSG_MAP(CEx_ODBCView)
ON_BN_CLICKED(IDC_REC_ADD, OnRecAdd)
ON_BN_CLICKED(IDC_REC_EDIT, OnRecEdit)
ON_BN_CLICKED(IDC_REC_DEL, OnRecDel)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CEx_ODBCView construction/destruction
CEx_ODBCView::CEx_ODBCView()
: CRecordView(CEx_ODBCView::IDD)
{
//{{AFX_DATA_INIT(CEx_ODBCView)
m_pSet = NULL;
//}}AFX_DATA_INIT
// TODO: add construction code here
}
CEx_ODBCView::~CEx_ODBCView()
{
}
void CEx_ODBCView::DoDataExchange(CDataExchange* pDX)
{
CRecordView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEx_ODBCView)
DDX_FieldText(pDX, IDC_PROF_CODE, m_pSet->m_profcode, m_pSet);
DDV_MaxChars(pDX, m_pSet->m_profcode, 6);
DDX_FieldText(pDX, IDC_STU_ID, m_pSet->m_stuid, m_pSet);
DDV_MaxChars(pDX, m_pSet->m_stuid, 6);
DDX_FieldText(pDX, IDC_STU_NAME, m_pSet->m_name, m_pSet);
DDV_MaxChars(pDX, m_pSet->m_name, 8);
DDX_FieldText(pDX, IDC_STU_SEX, m_pSet->m_sex, m_pSet);
DDV_MaxChars(pDX, m_pSet->m_sex, 2);
DDX_Control(pDX, IDC_MSFLEXGRID1, m_MSFGrid);
//}}AFX_DATA_MAP
}
BOOL CEx_ODBCView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CRecordView::PreCreateWindow(cs);
}
void CEx_ODBCView::OnInitialUpdate()
{
m_pSet = &GetDocument()->m_ex_ODBCSet;
CRecordView::OnInitialUpdate();//视图更新并初始化
GetParentFrame()->RecalcLayout();
ResizeParentToFit();//根据视图的尺寸重新调整父窗口的大小
while(!m_pSet->IsEOF())
{ m_pSet->MoveNext();
m_pSet->GetRecordCount();
}
m_pSet->MoveFirst();
m_MSFGrid.SetCols (m_pSet->m_nFields+1);//设置网格的最大列数
m_MSFGrid.SetRows(m_pSet->GetRecordCount()+1);
m_MSFGrid.SetColWidth(-1,1440);
//将所有的网格都设为相同的列宽.-1表示所有的列,列宽单位为一个点的
//1/20(一个点是1/72英寸),也就是说,1440刚好为1英寸
//定义网格的表头
m_MSFGrid.SetRow(0);
m_MSFGrid.SetCol(1); //定位到(0,1)网格
m_MSFGrid.SetText("学号"); //设置其显示内容
m_MSFGrid.SetRow(0); m_MSFGrid.SetCol(2);
m_MSFGrid.SetText("姓名");
m_MSFGrid.SetRow(0); m_MSFGrid.SetCol(3);
m_MSFGrid.SetText("性别");
m_MSFGrid.SetRow(0); m_MSFGrid.SetCol(4);
m_MSFGrid.SetText("专业代号");
int iRow=1;
while(!m_pSet->IsEOF())
{ CString str;
str.Format("记录%d",iRow);
m_MSFGrid.SetRow(iRow); m_MSFGrid.SetCol(0);
m_MSFGrid.SetText(str);
m_MSFGrid.SetRow(iRow); m_MSFGrid.SetCol(1);
m_MSFGrid.SetText(m_pSet->m_stuid);
m_MSFGrid.SetRow(iRow); m_MSFGrid.SetCol(2);
m_MSFGrid.SetText(m_pSet->m_name);
m_MSFGrid.SetRow(iRow); m_MSFGrid.SetCol(3);
m_MSFGrid.SetText(m_pSet->m_sex);
m_MSFGrid.SetRow(iRow); m_MSFGrid.SetCol(4);
m_MSFGrid.SetText(m_pSet->m_profcode);
iRow++;
m_pSet->MoveNext();
}
m_MSFGrid.SetRow(1); m_MSFGrid.SetCol(1);
m_pSet->MoveFirst();
}
/////////////////////////////////////////////////////////////////////////////
// CEx_ODBCView printing
BOOL CEx_ODBCView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CEx_ODBCView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CEx_ODBCView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CEx_ODBCView diagnostics
#ifdef _DEBUG
void CEx_ODBCView::AssertValid() const
{
CRecordView::AssertValid();
}
void CEx_ODBCView::Dump(CDumpContext& dc) const
{
CRecordView::Dump(dc);
}
CEx_ODBCDoc* CEx_ODBCView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEx_ODBCDoc)));
return (CEx_ODBCDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CEx_ODBCView database support
CRecordset* CEx_ODBCView::OnGetRecordset()
{
return m_pSet;
}
BOOL CEx_ODBCView::OnCommand(WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
CString str;
CMainFrame* pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd;
CStatusBar* pStatus = &pFrame->m_wndStatusBar;
if(pStatus)
{ CRecordsetStatus rStatus;
m_pSet->GetStatus(rStatus);
str.Format("当前记录:%d/总记录:%d",1+rStatus.m_lCurrentRecord,
m_pSet->GetRecordCount());
pStatus->SetPaneText(1,str);
}
return CRecordView::OnCommand(wParam, lParam);
}
void CEx_ODBCView::OnRecAdd()
{
// TODO: Add your control notification handler code here
CStuDlg dlg;
if(dlg.DoModal()==IDOK)
{ m_pSet->AddNew();
m_pSet->m_stuid=dlg.m_StuID;
m_pSet->m_name=dlg.m_StuName;
m_pSet->m_sex=dlg.m_StuSex;
m_pSet->m_profcode=dlg.m_ProfCode;
m_pSet->Update();
m_pSet->Requery();
}
}
void CEx_ODBCView::OnRecEdit()
{
// TODO: Add your control notification handler code here
CStuDlg dlg;
dlg.m_StuID=m_pSet->m_stuid;
dlg.m_StuName=m_pSet->m_name;
dlg.m_StuSex=m_pSet->m_sex;
dlg.m_ProfCode=m_pSet->m_profcode;
if(dlg.DoModal()==IDOK)
{ m_pSet->Edit();
m_pSet->m_stuid=dlg.m_StuID;
m_pSet->m_name= dlg.m_StuName;
m_pSet->m_sex=dlg.m_StuSex;
m_pSet->m_profcode=dlg.m_ProfCode;
m_pSet->Update();
UpdateData(FALSE);
}
}
void CEx_ODBCView::OnRecDel()
{
// TODO: Add your control notification handler code here
CRecordsetStatus status;
m_pSet->GetStatus(status);
m_pSet->Delete();
if(status.m_lCurrentRecord==0) m_pSet->MoveNext();
else m_pSet->MoveFirst();
UpdateData(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -