📄 dbview.cpp
字号:
// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (C) 1992-1998 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.
#include "stdafx.h"
#ifdef AFX_DB_SEG
#pragma code_seg(AFX_DB_SEG)
#endif
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define new DEBUG_NEW
/////////////////////////////////////////////////////////////////////////////
BEGIN_MESSAGE_MAP(CRecordView, CFormView)
//{{AFX_MSG_MAP(CRecordView)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
ON_COMMAND_EX(ID_RECORD_FIRST, OnMove)
ON_UPDATE_COMMAND_UI(ID_RECORD_FIRST, OnUpdateRecordFirst)
ON_COMMAND_EX(ID_RECORD_PREV, OnMove)
ON_UPDATE_COMMAND_UI(ID_RECORD_PREV, OnUpdateRecordPrev)
ON_COMMAND_EX(ID_RECORD_NEXT, OnMove)
ON_UPDATE_COMMAND_UI(ID_RECORD_NEXT, OnUpdateRecordNext)
ON_COMMAND_EX(ID_RECORD_LAST, OnMove)
ON_UPDATE_COMMAND_UI(ID_RECORD_LAST, OnUpdateRecordLast)
END_MESSAGE_MAP()
CRecordView::CRecordView(LPCTSTR lpszTemplateName)
: CFormView(lpszTemplateName)
{
}
CRecordView::CRecordView(UINT nIDTemplate)
: CFormView(nIDTemplate)
{
}
CRecordView::~CRecordView()
{
}
void CRecordView::OnInitialUpdate()
{
CRecordset* pRecordset = OnGetRecordset();
// recordset must be allocated already
ASSERT(pRecordset != NULL);
if (!pRecordset->IsOpen())
{
CWaitCursor wait;
pRecordset->Open();
}
CFormView::OnInitialUpdate();
}
BOOL CRecordView::IsOnFirstRecord()
{
ASSERT_VALID(this);
CRecordsetStatus status;
OnGetRecordset()->GetStatus(status);
return status.m_lCurrentRecord == 0;
}
BOOL CRecordView::IsOnLastRecord()
{
ASSERT_VALID(this);
CRecordset* pRecordset = OnGetRecordset();
CRecordsetStatus status;
pRecordset->GetStatus(status);
if (!status.m_bRecordCountFinal)
return FALSE;
return ((status.m_lCurrentRecord+1 == pRecordset->GetRecordCount()));
}
BOOL CRecordView::OnMove(UINT nIDMoveCommand)
{
CRecordset* pSet = OnGetRecordset();
if (pSet->CanUpdate() && !pSet->IsDeleted())
{
pSet->Edit();
if (!UpdateData())
return TRUE;
pSet->Update();
}
switch (nIDMoveCommand)
{
case ID_RECORD_PREV:
pSet->MovePrev();
if (!pSet->IsBOF())
break;
case ID_RECORD_FIRST:
pSet->MoveFirst();
break;
case ID_RECORD_NEXT:
pSet->MoveNext();
if (!pSet->IsEOF())
break;
if (!pSet->CanScroll())
{
// clear out screen since we're sitting on EOF
pSet->SetFieldNull(NULL);
break;
}
case ID_RECORD_LAST:
pSet->MoveLast();
break;
default:
// Unexpected case value
ASSERT(FALSE);
}
// Show results of move operation
UpdateData(FALSE);
return TRUE;
}
void CRecordView::OnUpdateRecordFirst(CCmdUI* pCmdUI)
{
CRecordset* prs = OnGetRecordset();
// enable if opened, can scroll backwards,
pCmdUI->Enable(prs->IsOpen() && prs->CanScroll() &&
// >= 1 records present and not already on first record
!(prs->IsEOF() && prs->IsBOF()) && !IsOnFirstRecord());
}
void CRecordView::OnUpdateRecordPrev(CCmdUI* pCmdUI)
{
CRecordView::OnUpdateRecordFirst(pCmdUI);
}
void CRecordView::OnUpdateRecordNext(CCmdUI* pCmdUI)
{
CRecordset* prs = OnGetRecordset();
// enable if opened and >= 1 records present
pCmdUI->Enable(prs->IsOpen() && !(prs->IsEOF() && prs->IsBOF())
// and not already on last record
&& !IsOnLastRecord());
}
void CRecordView::OnUpdateRecordLast(CCmdUI* pCmdUI)
{
CRecordset* prs = OnGetRecordset();
// enable if opened, can scroll,
pCmdUI->Enable(prs->IsOpen() && prs->CanScroll() &&
// >= 1 records present and not already on last record
!(prs->IsEOF() && prs->IsBOF()) && !IsOnLastRecord());
}
/////////////////////////////////////////////////////////////////////////////
// DDX Cover functions for use with fields of a recordset
/////////////////////////////////////////////////////////////////////////////
// Simple field formatting to text item
BOOL AFXAPI AfxFieldText(CDataExchange* pDX, int nIDC, void* pv,
CRecordset* pRecordset)
{
ASSERT_VALID(pRecordset);
HWND hWndCtrl = pDX->PrepareEditCtrl(nIDC);
TCHAR szT[2];
if (pDX->m_bSaveAndValidate)
{
::GetWindowText(hWndCtrl, szT, _countof(szT));
if (szT[0] == '\0')
{
if (pRecordset->IsFieldNullable(pv))
{
pRecordset->SetFieldNull(pv);
return TRUE;
}
}
else
pRecordset->SetFieldNull(pv, FALSE);
}
else
{
if (!pRecordset->IsOpen() || pRecordset->IsFieldNull(pv))
{
szT[0] = '\0';
AfxSetWindowText(hWndCtrl, szT);
return TRUE;
}
else if (pRecordset->IsDeleted())
{
CString strDeleted(MAKEINTRESOURCE(AFX_IDS_DELETED));
AfxSetWindowText(hWndCtrl, strDeleted);
return TRUE;
}
}
return FALSE;
}
void AFXAPI DDX_FieldText(CDataExchange* pDX, int nIDC, int& value,
CRecordset* pRecordset)
{
if (!AfxFieldText(pDX, nIDC, &value, pRecordset))
DDX_Text(pDX, nIDC, value);
}
void AFXAPI DDX_FieldText(CDataExchange* pDX, int nIDC, BYTE& value,
CRecordset* pRecordset)
{
if (!AfxFieldText(pDX, nIDC, &value, pRecordset))
DDX_Text(pDX, nIDC, value);
}
void AFXAPI DDX_FieldText(CDataExchange* pDX, int nIDC, UINT& value,
CRecordset* pRecordset)
{
if (!AfxFieldText(pDX, nIDC, &value, pRecordset))
DDX_Text(pDX, nIDC, value);
}
void AFXAPI DDX_FieldText(CDataExchange* pDX, int nIDC, long& value,
CRecordset* pRecordset)
{
if (!AfxFieldText(pDX, nIDC, &value, pRecordset))
DDX_Text(pDX, nIDC, value);
}
void AFXAPI DDX_FieldText(CDataExchange* pDX, int nIDC, DWORD& value,
CRecordset* pRecordset)
{
if (!AfxFieldText(pDX, nIDC, &value, pRecordset))
DDX_Text(pDX, nIDC, value);
}
void AFXAPI DDX_FieldText(CDataExchange* pDX, int nIDC, CString& value,
CRecordset* pRecordset)
{
ASSERT_VALID(pRecordset);
HWND hWndCtrl = pDX->PrepareEditCtrl(nIDC);
if (pDX->m_bSaveAndValidate)
{
// check if length is too long (this is complicated by Windows NT/J)
int nLen = ::GetWindowTextLength(hWndCtrl);
if (nLen > value.GetAllocLength())
{
if (!_afxDBCS)
AfxFailMaxChars(pDX, value.GetAllocLength());
CString strTemp;
::GetWindowText(hWndCtrl, strTemp.GetBuffer(nLen), nLen+1);
strTemp.ReleaseBuffer();
nLen = strTemp.GetLength();
if (nLen > value.GetAllocLength())
AfxFailMaxChars(pDX, value.GetAllocLength());
}
// get known length
::GetWindowText(hWndCtrl, value.GetBuffer(0), nLen+1);
value.ReleaseBuffer();
if (nLen == 0)
{
if (pRecordset->IsFieldNullable(&value))
pRecordset->SetFieldNull(&value, TRUE);
}
else
{
pRecordset->SetFieldNull(&value, FALSE);
}
}
else if (pRecordset->IsDeleted())
{
CString strDeleted(MAKEINTRESOURCE(AFX_IDS_DELETED));
AfxSetWindowText(hWndCtrl, strDeleted);
}
else
{
AfxSetWindowText(hWndCtrl, value);
}
}
void AFXAPI DDX_FieldText(CDataExchange* pDX, int nIDC, LPTSTR pstrValue,
int nMaxLength, CRecordset* pRecordset)
{
ASSERT_VALID(pRecordset);
HWND hWndCtrl = pDX->PrepareEditCtrl(nIDC);
if (pDX->m_bSaveAndValidate)
{
int nLen = ::GetWindowText(hWndCtrl, pstrValue, nMaxLength);
if (nLen == 0)
{
if (pRecordset->IsFieldNullable(pstrValue))
pRecordset->SetFieldNull(pstrValue, TRUE);
}
else
pRecordset->SetFieldNull(pstrValue, FALSE);
}
else if (pRecordset->IsDeleted())
{
CString strDeleted(MAKEINTRESOURCE(AFX_IDS_DELETED));
AfxSetWindowText(hWndCtrl, strDeleted);
}
else
AfxSetWindowText(hWndCtrl, pstrValue);
}
void AFXAPI DDX_FieldLBString(CDataExchange* pDX, int nIDC, CString& value,
CRecordset* pRecordset)
{
ASSERT_VALID(pRecordset);
HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
if (pDX->m_bSaveAndValidate)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -