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

📄 databaseview.cpp

📁 陈建春<用VC++开发GIS系统>源码.适合于GIS学习者,以及学习图形图象编程的朋友.
💻 CPP
字号:
// DatabaseView.cpp : implementation file
//

#include "stdafx.h"
#include "draw.h"
#include "drawdoc.h"
#include "selectdatadlg.h"
#include "DatabaseView.h"
#include "data1dlg.h"
#include "sortdlg.h"
#include "linkdlg.h"
#include "filterdlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDatabaseView

IMPLEMENT_DYNCREATE(CDatabaseView, CScrollView)

CDatabaseView::CDatabaseView()
{
	nIndex=-1;
	sizeTotal.cx=sizeTotal.cy=300;
	sizePage.cx=sizePage.cy=300;
	sizeLine.cx=sizeLine.cy=10;
	nSelectData=0;
}

CDatabaseView::~CDatabaseView()
{
}


BEGIN_MESSAGE_MAP(CDatabaseView, CScrollView)
	//{{AFX_MSG_MAP(CDatabaseView)
	ON_WM_SIZE()
	ON_WM_LBUTTONDOWN()
	ON_COMMAND(ID_RECORD_ADD, OnRecordAdd)
	ON_COMMAND(ID_RECORD_MODIFY, OnRecordModify)
	ON_COMMAND(ID_RECORD_FILTER, OnRecordFilter)
	ON_COMMAND(ID_RECORD_SORT, OnRecordSort)
	ON_COMMAND(ID_RECORD_DELETE, OnRecordDelete)
	ON_COMMAND(ID_RECORD_LINK, OnRecordLink)
	ON_COMMAND(ID_RECORD_DELETE_ALL, OnRecordDeleteAll)
	ON_COMMAND(ID_FILE_SELECT, OnFileSelect)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDatabaseView drawing

void CDatabaseView::OnInitialUpdate()
{
	
	CDrawDoc* pDoc = GetDocument();
	m_pSet1=&pDoc->m_data1;
	m_pSet=pDoc->GetDataSet(nSelectData);
	m_pSet=&pDoc->m_data1;
	//	m_pSet2=(CRecordSet *)&pDoc->m_data1;
	m_pLinkSet1=&pDoc->m_linkdata1;
	Init1();
	Init2();
	CScrollView::OnInitialUpdate();
	SetScrollSizes(MM_TEXT, sizeTotal,sizePage,sizeLine);
	//CSize sizeTotal;
}

void CDatabaseView::Init1()
{
	TEXTMETRIC tm;
	CDC* dc=GetDC();
	dc->GetTextMetrics(&tm);
	ReleaseDC(dc);
	nCharHeight=tm.tmHeight+2;
	nCharWidth=tm.tmAveCharWidth+2;
	if(m_pSet->IsOpen())
		m_pSet->Close();
	m_pSet->Open();
	nField=m_pSet->GetODBCFieldCount();
	CODBCFieldInfo fieldinfo1;
	CODBCFieldInfo& fieldinfo=fieldinfo1;
	for(int i=0;i<nField;i++)
	{
		m_pSet->GetODBCFieldInfo(i,fieldinfo);
		nFieldWide[i]=(short)fieldinfo1.m_nPrecision;
	}
	nScreenWide=0;
	nFieldStart[0]=0;
	for(i=0;i<nField;i++)
	{
		nScreenWide=nScreenWide+nFieldWide[i]*nCharWidth;
		nFieldStart[i+1]=nFieldStart[i]+nFieldWide[i]*nCharWidth;
	}
	sizeTotal.cx=nScreenWide;
	sizeLine.cx=nCharWidth*5;
}

void CDatabaseView::Init2()
{
	if(m_pSet->IsOpen())
		m_pSet->Close();
	m_pSet->Open();
	if(m_pSet->IsBOF())
		nRecord=0;
	else
	{
		m_pSet->MoveFirst();
		nRecord=0;
		while(!m_pSet->IsEOF())
		{
			m_pSet->MoveNext();
			nRecord++;
		}
	}
	nScreenHeight=nRecord*nCharHeight;
	if(nScreenHeight==0)
		nScreenHeight=20;
	sizeTotal.cy=nScreenHeight;
	sizeLine.cy=nCharHeight+2;
	if(nRecord>0)
	{
		if(nIndex<1||nIndex>nRecord)
			nIndex=1;
	}
	else 
		nIndex=-1;
//	SetScrollSizes(MM_TEXT, sizeTotal,sizePage,sizeLine);
}



void CDatabaseView::OnDraw(CDC* pDC)
{
	CRect r1;
	CDrawDoc* pDoc = GetDocument();
	CPen pen1(0,1,RGB(255,0,0));
//	CPen* pOldPen=ht.SelectObject(&pen1);
	pDC->SetTextColor(RGB(255,0,0));
	CString c1;
	CString& c2=c1;
	pDC->SetMapMode(1);	
	m_pSet->MoveFirst();
	for(int i=0;i<nRecord;i++)
	{
		for(int j=0;j<nField;j++)
		{
			m_pSet->GetFieldValue(j,c2);
			pDC->TextOut(nFieldStart[j],i*nCharHeight,c1);
		}
		m_pSet->MoveNext();
	}
	if(nIndex>0)
	{
		GetRect(nIndex,&r1);
		pDC->InvertRect(&r1);
	}
	//	pDC->SelectObject(pOldPen);
}

/////////////////////////////////////////////////////////////////////////////
// CDatabaseView diagnostics

#ifdef _DEBUG
void CDatabaseView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CDatabaseView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

CDrawDoc* CDatabaseView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDrawDoc)));
	return (CDrawDoc*)m_pDocument;
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CDatabaseView message handlers

void CDatabaseView::OnSize(UINT nType, int cx, int cy) 
{
	CScrollView::OnSize(nType, cx, cy);
	// TODO: Add your message handler code here
	sizePage.cx=cx;
	sizePage.cy=cy;
	SetScrollSizes(MM_TEXT, sizeTotal,sizePage,sizeLine);
}

void CDatabaseView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CRect r1;
	CClientDC ht(this);
	OnPrepareDC(&ht);
	ht.DPtoLP(&point);
	int nIndex1=point.y/nCharHeight+1;
	if(nIndex1!=nIndex&&nIndex1>0&&nIndex1<=nRecord)
	{
		if(nIndex>0)
		{
			GetRect(nIndex,&r1);
			ht.InvertRect(&r1);
		}
		GetRect(nIndex1,&r1);
		ht.InvertRect(&r1);
		nIndex=nIndex1;
	}
	CScrollView::OnLButtonDown(nFlags, point);
}


void CDatabaseView::GetRect(int nRow, CRect *r1)
{
	r1->left=0;
	r1->right=nFieldStart[nField-1]+nFieldWide[nField-1]*nCharWidth;
	r1->top=(nRow-1)*nCharHeight;
	r1->bottom=r1->top+nCharHeight;
}

void CDatabaseView::OnRecordAdd() 
{
	CDrawDoc* pDoc=GetDocument();
	if(m_pSet->IsOpen())
		m_pSet->Close();
	int ID=pDoc->DataInfo[nSelectData].only_ID;
	if(ID==1)
	{
		CData1Dlg dlg(m_pSet1,1,GetOnlyID());
		if(dlg.DoModal()==IDOK)
		{
			Init2();
			SetScrollSizes(MM_TEXT, sizeTotal,sizePage,sizeLine);
			UpdateWindow();
			Invalidate();
		}
	}
	else if(ID==2)
	{
		CData1Dlg dlg(m_pSet1,1,GetOnlyID());
		if(dlg.DoModal()==IDOK)
		{
			Init2();
			SetScrollSizes(MM_TEXT, sizeTotal,sizePage,sizeLine);
			UpdateWindow();
			Invalidate();
		}
	}
}


int CDatabaseView::GetOnlyID()
{
	int *Index1,id;
	Index1= GetDocument()->m_Index;
	CString m_str1=m_pSet->m_strFilter;
	m_pSet->m_strFilter="";
	if(m_pSet->IsOpen())
		m_pSet->Close();
	m_pSet->Open();
	if(m_pSet->IsEOF())
		return 0;
	for(int i=0;i<20000;i++)
		Index1[i]=0;
	CString c1;
	CString& c2=c1;
	i=0;
	m_pSet->GetFieldValue(i,c2);
	while(!m_pSet->IsEOF())
	{
		Index1[atoi(c1)]=1;
		m_pSet->MoveNext();
	}
	for(i=0;i<20000;i++)
	{
		if(Index1[i]==0)
		{
			id=i;
			break;
		}
	}
	m_pSet->Close();
	m_pSet->m_strFilter=m_str1;
	m_pSet->Open();
	return id;
}


void CDatabaseView::OnRecordModify() 
{
	CDrawDoc* pDoc=GetDocument();
	if(nIndex<=0&&nIndex>nRecord)
		return;
	CString m_str1=m_pSet->m_strFilter;
	CString m_str2=m_pSet->m_strSort;
	if(m_pSet->IsOpen())
		m_pSet->Close();
	int ID=pDoc->DataInfo[nSelectData].only_ID;
	if(ID==1)
	{
		if(m_pSet1->IsOpen())
			m_pSet1->Close();
		m_pSet1->m_strFilter=m_str1;
		m_pSet1->m_strSort=m_str2;
		m_pSet1->Open();
		m_pSet1->MoveFirst();
		m_pSet1->Move(nIndex-1);
		CData1Dlg dlg(m_pSet1,0,0);
		if(dlg.DoModal())
		{
			Init2();
			SetScrollSizes(MM_TEXT, sizeTotal,sizePage,sizeLine);
			Invalidate();
		}
	}
	else if(ID==2)
	{
/*		if(m_pSet2->IsOpen())
			m_pSet2->Close();
		m_pSet2->m_strFilter=m_str1;
		m_pSet2->m_strSort=m_str2;
		m_pSet2->Open();
		m_pSet2->MoveFirst();
		m_pSet2->Move(nIndex-1);
		CData2Dlg dlg(m_pSet2,0,0);*/
		CData1Dlg dlg(m_pSet1,0,0);

		if(dlg.DoModal())
		{
			Init2();
			SetScrollSizes(MM_TEXT, sizeTotal,sizePage,sizeLine);
			Invalidate();
		}
	}
}

void CDatabaseView::OnRecordFilter() 
{
	CFilterDlg dlg(m_pSet);
	if(dlg.DoModal()==IDOK)
	{
		m_pSet->m_strFilter=dlg.m_strFilter;
		Init2();
		SetScrollSizes(MM_TEXT, sizeTotal,sizePage,sizeLine);
		Invalidate();
	}
}

void CDatabaseView::OnRecordSort() 
{
	CSortDlg pDg1(m_pSet);
	if(pDg1.DoModal()==IDOK)
	{
		if(m_pSet->IsOpen())
			m_pSet->Close();
		m_pSet->m_strSort=pDg1.m_strSort;
		m_pSet->Open();
		Invalidate();
	}
}

void CDatabaseView::OnRecordDelete() 
{
	if(nIndex<1||nIndex>nRecord)
		return;
	if(!m_pSet->IsOpen())
		m_pSet->Open();
	m_pSet->MoveFirst();
	m_pSet->Move(nIndex-1);
	m_pSet->Delete();
	CPoint p1;
	p1=GetScrollPosition();
	sizeTotal.cy-=nCharHeight;
	if(nIndex==nRecord)
		nIndex--;
	Init2();
	SetScrollSizes(MM_TEXT, sizeTotal,sizePage,sizeLine);
	Invalidate();
	ScrollToPosition(p1);	
}

void CDatabaseView::OnRecordLink() 
{
	CDrawDoc* pDoc=GetDocument();
	m_pSet1->MoveFirst();
	m_pSet1->Move(nIndex-1);
	CString m_str;
	m_str="(database_id=";
	short lb=pDoc->GetDataID(nSelectData);
	if(lb<0)
		return;
	char p1[9];
	sprintf(p1,"%d",lb);
	m_str+=p1;
	m_str+=") AND (record_id=";
	int ID=m_pSet->GetID();
	sprintf(p1,"%d",ID);
	m_str+=p1;
	m_str+=")";
	m_pLinkSet1->m_strFilter=m_str;
	CLinkDlg pDlg1(GetDocument(),m_pLinkSet1,1,lb,ID);
	pDlg1.DoModal();
}

void CDatabaseView::OnRecordDeleteAll() 
{
	if(!m_pSet->IsOpen())
		m_pSet->Open();
	for(int i=nRecord-1;i>=0;i--)
	{
		m_pSet->MoveFirst();
		m_pSet->Move(i);
		m_pSet->Delete();
	}
	nIndex=-1;
	Init2();
	SetScrollSizes(MM_TEXT, sizeTotal,sizePage,sizeLine);
	Invalidate();
}

void CDatabaseView::OnFileSelect() 
{
	CDrawDoc* pDoc = GetDocument();
	CSelectDataDlg pDlg1(pDoc,nSelectData);
	if(pDlg1.DoModal()==IDOK)
	{
		int nn=pDlg1.m_Index;
		if(nn!=nSelectData)
		{
			if(m_pSet->IsOpen())
				m_pSet->Close();
			nSelectData=nn;
			m_pSet=pDoc->GetDataSet(nSelectData);
			m_pSet->Open();
			nIndex=-1;
			Init1();
			Init2();
			SetScrollSizes(MM_TEXT, sizeTotal,sizePage,sizeLine);
			Invalidate();
		}
	}
}

⌨️ 快捷键说明

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