www.gusucode.com > VC++建筑横梁数据管理系统源代码源码程序 > VC++建筑横梁数据管理系统源代码源码程序/code/BeamDataManagerView.cpp

    // BeamDataManagerView.cpp : implementation of the CBeamDataManagerView class
// Download by http://www.NewXing.com

#include "stdafx.h"
#include "BeamDataManager.h"

#include "BeamDataManagerDoc.h"
#include "BeamDataManagerView.h"

#include "DataExchangeFunDefine.h"
#include "BrushChildTableFunction.h"

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

/////////////////////////////////////////////////////////////////////////////
// CBeamDataManagerView

IMPLEMENT_DYNCREATE(CBeamDataManagerView, CFormView)

BEGIN_MESSAGE_MAP(CBeamDataManagerView, CFormView)
	//{{AFX_MSG_MAP(CBeamDataManagerView)
	ON_NOTIFY(NM_CLICK, IDC_BEAMDATALIST, OnClickBeamdatalist)
	ON_NOTIFY(NM_DBLCLK, IDC_BEAMDATALIST, OnDblclkBeamdatalist)
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBeamDataManagerView construction/destruction

CBeamDataManagerView::CBeamDataManagerView()
	: CFormView(CBeamDataManagerView::IDD)
{
	// TODO: add construction code here
	m_TableName="\0";
}

CBeamDataManagerView::~CBeamDataManagerView()
{
}

void CBeamDataManagerView::DoDataExchange(CDataExchange* pDX) 
{
	// TODO: Add your specialized code here and/or call the base class
	DDX_Control(pDX, IDC_BEAMDATALIST, m_basedatalist);

	CFormView::DoDataExchange(pDX);
}

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

	return CFormView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CBeamDataManagerView drawing

void CBeamDataManagerView::OnDraw(CDC* pDC)
{
	CBeamDataManagerDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	
	pDC->TextOut(20,2,m_TableName);	
}

/////////////////////////////////////////////////////////////////////////////
// CBeamDataManagerView printing

BOOL CBeamDataManagerView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CBeamDataManagerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CBeamDataManagerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CBeamDataManagerView diagnostics

#ifdef _DEBUG
void CBeamDataManagerView::AssertValid() const
{
	CFormView::AssertValid();
}

void CBeamDataManagerView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CBeamDataManagerView message handlers

void CBeamDataManagerView::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	// TODO: Add your message handler code here
	RECT rect;
	GetClientRect(&rect);
	m_basedatalist.SetWindowPos(&wndTop,0,20,
								rect.right-rect.left,
								rect.bottom-rect.top-20,
								SWP_SHOWWINDOW); 

	OnDraw(&dc);	
	// Do not call CFormView::OnPaint() for painting messages
}

void CBeamDataManagerView::OnInitialUpdate() 
{
	CFormView::OnInitialUpdate();
	// TODO: Add your specialized code here and/or call the base class
	DrawList();
	ReadtoList();
}

void CBeamDataManagerView::DrawList()
{
	RECT rect;
	GetClientRect(&rect);
	m_basedatalist.SetBkColor(RGB(180,180,180));
	m_basedatalist.SetTextBkColor(RGB(100,140,140));
	m_basedatalist.SetTextColor(RGB(250,250,250));

	DWORD dwStyle;
	dwStyle = m_basedatalist.GetStyle();
	dwStyle |= LVS_EX_GRIDLINES |LVS_EX_FULLROWSELECT|LVS_SHOWSELALWAYS ;
	m_basedatalist.SetExtendedStyle(dwStyle);
	m_basedatalist.GetClientRect(&rect);
	LV_COLUMN lvc;
	lvc.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH |LVCF_FMT;
	lvc.fmt=LVCFMT_CENTER;

	if(m_TableName=="-"||m_TableName=="\0"){
		m_TableName="\0";
		lvc.pszText="\0";
		lvc.cx=50;
		m_basedatalist.DeleteColumn(0);
		m_basedatalist.InsertColumn(0,&lvc);
		return;
	}

	CString sql="SELECT * FROM "+m_TableName;
	CBeamDataManagerApp *ptheApp=((CBeamDataManagerApp*)AfxGetApp());
	try
	{
		m_pRecordset.CreateInstance("ADODB.Recordset");	
		m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)ptheApp->m_pConnection,true),
											adOpenStatic,adLockOptimistic,adCmdText);
		m_fieldscount=m_pRecordset->GetFields()->GetCount();
		m_listcolumnscount=m_fieldscount;
		FieldPtr ptr;
		CString fieldname;
		for(long i=0;i<m_fieldscount;i++){
			ptr=m_pRecordset->GetFields()->GetItem(i);
			fieldname= (LPCTSTR)(_bstr_t)ptr->Name;
			if(fieldname=="ChildTableViewLocation") m_listcolumnscount--;
			if(fieldname=="ChildTableName")	m_listcolumnscount--;
			if(fieldname=="DataExchangeFunction") m_listcolumnscount--;
			if(fieldname=="BrushChildTableFunction") m_listcolumnscount--;
			//检索是否有以上三个字段,以扣去不画到网格中
		}

		for(i=0;i<m_listcolumnscount;i++){
			ptr=m_pRecordset->GetFields()->GetItem(i);
			lvc.iSubItem = i;
			fieldname= (LPCTSTR)(_bstr_t)ptr->Name;
			lvc.pszText=fieldname.GetBuffer(fieldname.GetLength());  
			lvc.cx = fieldname.GetLength()*11;
			m_basedatalist.DeleteColumn(i);
			m_basedatalist.InsertColumn(i,&lvc);
		}
	}
	catch(_com_error e)///捕捉异常
	{
		CString temp;
		temp.Format("连接数据库错误信息:%s",e.ErrorMessage());
		AfxMessageBox(temp);
		return;
	}
	if(m_pRecordset->State)
		m_pRecordset->Close();
	m_pRecordset.Release();
}

void CBeamDataManagerView::ReadtoList()
{
	//删除所有list中的数据。
	m_basedatalist.DeleteAllItems();

	if(m_TableName=="-"||m_TableName=="\0")
		return;

	CString sql="SELECT * FROM "+m_TableName;
	FieldPtr ptr;
	CString fieldname;
    LPSTR fieldText;
	int numline=0;
	try
	{
		CBeamDataManagerApp *ptheApp=((CBeamDataManagerApp*)AfxGetApp());
		m_pRecordset.CreateInstance("ADODB.Recordset");
		m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)ptheApp->m_pConnection,true),
							adOpenStatic,adLockOptimistic,adCmdText);
		if(m_pRecordset->adoEOF)
		{
			AfxMessageBox("数据库表中没有记录信息!");
		}
		else
		{
			LV_ITEM lvitem;
			lvitem.mask=LVIF_TEXT ;
			lvitem.pszText="";
			lvitem.iSubItem=0;
			while(!m_pRecordset->adoEOF)
			{
				lvitem.iItem=numline;
				m_basedatalist.InsertItem(&lvitem);
				//读出数据写入到list中
				for(long i=0;i<m_listcolumnscount;i++){
					ptr=m_pRecordset->GetFields()->GetItem(i);
					fieldname= (LPCTSTR)(_bstr_t)ptr->Name;
					fieldText=fieldname.GetBuffer(fieldname.GetLength()); 
					m_basedatalist.SetItemText(numline,i,(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect(fieldText));
				}
				numline++;
				m_pRecordset->MoveNext();
			}
		}
	}
	catch(_com_error e)///捕捉异常
	{
		CString temp;
		temp.Format("连接数据库错误信息:%s",e.ErrorMessage());
		AfxMessageBox(temp);
		return;
	}
	if(m_pRecordset->State)
		m_pRecordset->Close();
	m_pRecordset.Release();
}

void CBeamDataManagerView::OnClickBeamdatalist(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	POSITION pos = m_basedatalist.GetFirstSelectedItemPosition();
	//如果选中一行
	if(pos)
	{
		CString sql="SELECT * FROM "+m_TableName;
		try
		{
			CBeamDataManagerApp *ptheApp=((CBeamDataManagerApp*)AfxGetApp());
			m_pRecordset.CreateInstance("ADODB.Recordset");
			m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)ptheApp->m_pConnection,true),
								adOpenStatic,adLockOptimistic,adCmdText);
			if(m_pRecordset->adoEOF)
			{
				AfxMessageBox("数据库表中没有记录信息!");
			}
			else
			{
				int nItem = m_basedatalist.GetNextSelectedItem(pos);
				for(long i=0;i<nItem;i++)
					m_pRecordset->MoveNext();
				FieldPtr ptr;
				CString  fieldname;
				for(i=0;i<m_fieldscount;i++){
					ptr=m_pRecordset->GetFields()->GetItem(i);
					fieldname= (LPCTSTR)(_bstr_t)ptr->Name;
					if(fieldname=="BrushChildTableFunction") break;//检索是否有BrushChildTableFunction字段
				}
				if(fieldname!="BrushChildTableFunction") return;

				CString str1=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("BrushChildTableFunction");
				CString str2;
				BrushChildTableFunction BrushChildTable;
				for(i=0;i<dim(_BrushChildTableMapEntries);i++){
					str2.Format("%s",_BrushChildTableMapEntries[i].functionname);
					if(str2==str1)
						(BrushChildTable.*(_BrushChildTableMapEntries[i].pfn))(m_pRecordset,m_basedatalist);
				}

				for(i=0;i<m_fieldscount;i++){
					ptr=m_pRecordset->GetFields()->GetItem(i);
					fieldname= (LPCTSTR)(_bstr_t)ptr->Name;
					if(fieldname=="ChildTableViewLocation")	break;//检索是否有ChildTableViewLocation字段
				}
				if(fieldname!="ChildTableViewLocation")	return;
				CString childviewlocation=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("ChildTableViewLocation");
				//子表要显示的视口位置		

				for(i=0;i<m_fieldscount;i++){
					ptr=m_pRecordset->GetFields()->GetItem(i);
					fieldname= (LPCTSTR)(_bstr_t)ptr->Name;
					if(fieldname=="ChildTableName")	break;//检索是否有ChildTableName字段
				}
				if(fieldname!="ChildTableName") return;
				CString TableName=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("ChildTableName");
				//子表名
				if(m_pRecordset->State)
					m_pRecordset->Close();//必须要,因为当下句如果是删除自身的话,就须先关闭该记录再删除自身视窗
                 //如果子表要显示的视口与本表是同一视口会碰到这一问题
				CWnd *pMainWnd=AfxGetApp()->GetMainWnd();
				pMainWnd->SendMessage(WM_USER+50,
									  (WPARAM)childviewlocation.GetBuffer(childviewlocation.GetLength()),
									  (LPARAM)TableName.GetBuffer(TableName.GetLength()));
//该句作为该控件单击事件的统一行为,不管是击于哪一条记录,都是根据是否有子表记录及视口记录信息单纯地
//显示子表,但是对于子表的内容无法修改调整,对于子表的内容与本记录值某参数有相关的子表内容,无法实现
//对其的调整修改,因为由于不同的记录,信息内涵属性不同,与对应的子表的数据关系不同,无法在此处编写统一
//的代码实现不同的记录对不同的子表的数据关联修改特性
//除非在这里用一个统一的类对象,通过虚函数机制自动实现不同的关联性,就是要编类库机制,还是要用类的机制
//用映射机制也可以
			}
		}
		catch(_com_error e)///捕捉异常
		{
			CString temp;
			temp.Format("连接数据库错误信息:%s",e.ErrorMessage());
			AfxMessageBox(temp);
			return;
		}
		if(m_pRecordset->State)
			m_pRecordset->Close();
		m_pRecordset.Release();
	}
	
	*pResult = 0;
}

void CBeamDataManagerView::OnDblclkBeamdatalist(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	POSITION pos = m_basedatalist.GetFirstSelectedItemPosition();
	//如果选中一行
	if(pos)
	{
		CString sql="SELECT * FROM "+m_TableName;
		try
		{
			CBeamDataManagerApp *ptheApp=((CBeamDataManagerApp*)AfxGetApp());
			m_pRecordset.CreateInstance("ADODB.Recordset");
			m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)ptheApp->m_pConnection,true),
								adOpenStatic,adLockOptimistic,adCmdText);
			if(m_pRecordset->adoEOF)
			{
				AfxMessageBox("数据库表中没有记录信息!");
			}
			else
			{
				int nItem = m_basedatalist.GetNextSelectedItem(pos);
				for(int i=0;i<nItem;i++)
					m_pRecordset->MoveNext();
//				FieldPtr ptr=m_pRecordset->GetFields()->GetItem(m_fieldscount-(long)3);
//				CString  fieldname= (LPCTSTR)(_bstr_t)ptr->Name;
//				LPSTR fieldText=fieldname.GetBuffer(fieldname.GetLength()); 
//				CString str1=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect(fieldText);
				FieldPtr ptr;
				CString  fieldname;
				for(i=0;i<(int)m_fieldscount;i++){
					ptr=m_pRecordset->GetFields()->GetItem((long)i);
					fieldname= (LPCTSTR)(_bstr_t)ptr->Name;
					if(fieldname=="DataExchangeFunction") break;//检索是否有DataExchangeFunction字段
				}
				if(fieldname!="DataExchangeFunction") return;

				CString str1=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("DataExchangeFunction");
				CString str2;
				DataExchangeFunction dataExchange;
				for(i=0;i<dim(_structExchangeMapEntries);i++){
					str2.Format("%s",_structExchangeMapEntries[i].functionname);
					if(str2==str1)
						(dataExchange.*(_structExchangeMapEntries[i].pfn))(m_pRecordset,m_basedatalist);
				}
			}
		}
		catch(_com_error e)///捕捉异常
		{
			CString temp;
			temp.Format("连接数据库错误信息:%s",e.ErrorMessage());
			AfxMessageBox(temp);
			return;
		}
		if(m_pRecordset->State)
			m_pRecordset->Close();
		m_pRecordset.Release();
	}

	*pResult = 0;
}

BOOL CBeamDataManagerView::OnScroll(UINT nScrollCode, UINT nPos, BOOL bDoScroll) 
{
	// TODO: Add your specialized code here and/or call the base class

	return CFormView::OnScroll(nScrollCode, nPos, bDoScroll);
}
//
//void CBeamDataManagerView::OnDestroy() 
//{
//	CFormView::OnDestroy();
//
//	CWnd *pMainWnd=AfxGetApp()->GetMainWnd();
//	pMainWnd->SendMessage(WM_USER+50,
//						  (WPARAM)m_childviewlocation.GetBuffer(m_childviewlocation.GetLength()),
//						  (LPARAM)"-");
//
//	// TODO: Add your message handler code here
//}