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

📄 datagridview.cpp

📁 这是一个分水岭程序
💻 CPP
字号:
// DataGridView.cpp : implementation file
//

#include "stdafx.h"

#include "MyImageDb.h"
#include "DataGridView.h"
#include "myimagedbdoc.h"
#include "MainFrm.h"
#include <AFXOLE.H>

#include "columns.h"
#include "column.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDataGridView

IMPLEMENT_DYNCREATE(CDataGridView, CFormView)

CDataGridView::CDataGridView()
	: CFormView(CDataGridView::IDD), m_bAllowNew(true), m_bAllowEdit(true), m_bAllowDelete(true)
{
	//{{AFX_DATA_INIT(CDataGridView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	mousePressed = FALSE;
	seginfoRS = trainPointsRS = m_pRS = NULL;
}

CDataGridView::~CDataGridView()
{
}

void CDataGridView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDataGridView)
	DDX_Control(pDX, IDC_DATAGRID1, m_ctlDataGrid);
	DDX_Control(pDX, IDC_REGIONCLASSDATAGRID, myRegionclassInfoGrid);
	DDX_Control(pDX, IDC_POINTSINFOGRID, myPointsInfoGrid);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDataGridView, CFormView)
	//{{AFX_MSG_MAP(CDataGridView)
	ON_WM_SIZE()
	ON_WM_CREATE()
	ON_WM_VSCROLL()
	ON_WM_HSCROLL()
	ON_WM_KEYDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDataGridView diagnostics

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

void CDataGridView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CDataGridView message handlers
void CDataGridView::UpdateExistsClassGrid()
//刷新现有分割类表格;
{
    CMainFrame* pMainFrame = reinterpret_cast<CMainFrame*>(AfxGetMainWnd());

	if (pMainFrame->isSegInfoDbOK)
	{
		seginfoRS = pMainFrame->seginfoRs->m_pRecordset;
		myRegionclassInfoGrid.SetAllowDelete(FALSE);
        myRegionclassInfoGrid.SetCaption("分割类信息");
	    myRegionclassInfoGrid.SetRefDataSource(NULL);
	    myRegionclassInfoGrid.SetRefDataSource( (LPUNKNOWN) seginfoRS );
	    myRegionclassInfoGrid.Refresh();
	    
	    myRegionclassInfoGrid.UpdateData(FALSE);	
    }
}


void CDataGridView::UpdateTrainPointsGrid()
//刷新训练点表格;
{
    CMainFrame* pMainFrame = reinterpret_cast<CMainFrame*>(AfxGetMainWnd());
	if (pMainFrame->isTrainPointDbOK)
	{
		trainPointsRS = pMainFrame->trainpointRs->m_pRecordset;
		myPointsInfoGrid.SetAllowDelete(FALSE);
        myPointsInfoGrid.SetCaption("训练点信息");
	    myPointsInfoGrid.SetRefDataSource(NULL);
	    myPointsInfoGrid.SetRefDataSource( (LPUNKNOWN) trainPointsRS );
	    myPointsInfoGrid.Refresh();
	    
	    myRegionclassInfoGrid.UpdateData(FALSE);	
    }
}
void CDataGridView::UpdateGridDetails(const CString& sTableName)
{
    CMainFrame* pMainFrame = reinterpret_cast<CMainFrame*>(AfxGetMainWnd());
	if (pMainFrame->isDbOK)
	{
		m_pRS = pMainFrame->myRs->m_pRecordset;
		m_ctlDataGrid.SetAllowDelete(FALSE);
        m_ctlDataGrid.SetCaption("图像表");
	    m_ctlDataGrid.SetRefDataSource(NULL);
	    m_ctlDataGrid.SetRefDataSource( (LPUNKNOWN) m_pRS );
	    m_ctlDataGrid.Refresh();
	    
	    m_ctlDataGrid.UpdateData(FALSE);	
    }

	if (pMainFrame->isSegInfoDbOK)
	{
		seginfoRS = pMainFrame->seginfoRs->m_pRecordset;
		myRegionclassInfoGrid.SetAllowDelete(FALSE);
        myRegionclassInfoGrid.SetCaption("分割类信息");
	    myRegionclassInfoGrid.SetRefDataSource(NULL);
	    myRegionclassInfoGrid.SetRefDataSource( (LPUNKNOWN) seginfoRS );
	    myRegionclassInfoGrid.Refresh();
	    
	    myRegionclassInfoGrid.UpdateData(FALSE);	
    }

	if (pMainFrame->isTrainPointDbOK)
	{
		trainPointsRS = pMainFrame->trainpointRs->m_pRecordset;
		myPointsInfoGrid.SetAllowDelete(FALSE);
        myPointsInfoGrid.SetCaption("训练点信息");
	    myPointsInfoGrid.SetRefDataSource(NULL);
	    myPointsInfoGrid.SetRefDataSource( (LPUNKNOWN) trainPointsRS );
	    myPointsInfoGrid.Refresh();
	    
	    myRegionclassInfoGrid.UpdateData(FALSE);	
    }
}

void CDataGridView::OnSize(UINT nType, int cx, int cy) 
{
	CFormView::OnSize(nType, cx, cy);
	
    if (m_ctlDataGrid.m_hWnd != NULL)
	{
		m_ctlDataGrid.MoveWindow(0, 0, cx/2, cy);
		myRegionclassInfoGrid.MoveWindow(cx/2, 0, cx/2, cy/2);
		myPointsInfoGrid.MoveWindow(cx/2, cy/2, cx/2, cy/2);
	}        
}

CString CDataGridView::GetErrorDescription(_com_error& e)
{
    _bstr_t bstrSource(e.Source());
    _bstr_t bstrDescription(e.Description());
    _TCHAR szTemp[1024];

    CString strInfo ;
    wsprintf(szTemp, _T("Message : %s\n"), e.ErrorMessage());
    strInfo = szTemp;
    wsprintf(szTemp, _T("Code : 0x%08lx\n"), e.Error());
    strInfo += szTemp;
    wsprintf(szTemp, _T("Source : %s\n"), bstrSource.length() ? (LPCTSTR)bstrSource : _T("null"));
    strInfo += szTemp;
    wsprintf(szTemp, _T("Description : %s\n"), bstrDescription.length() ? (LPCTSTR)bstrDescription : _T("null"));
    strInfo += szTemp;

    return strInfo;
}

void CDataGridView::OnInitialUpdate() 
{
	CFormView::OnInitialUpdate();
	CMainFrame *pFrame;
	pFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd;             
	pFrame->pDataGridView = this;
	if (pFrame->isDbOK)
	{
		m_pRS = pFrame->myRs->m_pRecordset;
	}
	
    m_ctlDataGrid.SetAllowUpdate(m_bAllowEdit);
    m_ctlDataGrid.SetAllowAddNew(m_bAllowNew);	
    m_ctlDataGrid.SetAllowDelete(FALSE);
	m_ctlDataGrid.SetRefDataSource(m_pRS);
}



BEGIN_EVENTSINK_MAP(CDataGridView, CFormView)
    //{{AFX_EVENTSINK_MAP(CDataGridView)
	ON_EVENT(CDataGridView, IDC_DATAGRID1, 221 /* SelChange */, OnSelChangeDatagrid1, VTS_PI2)
	ON_EVENT(CDataGridView, IDC_DATAGRID1, -605 /* MouseDown */, OnMouseDownDatagrid1, VTS_I2 VTS_I2 VTS_I4 VTS_I4)
	ON_EVENT(CDataGridView, IDC_DATAGRID1, -601 /* DblClick */, OnDblClickDatagrid1, VTS_NONE)
	ON_EVENT(CDataGridView, IDC_DATAGRID1, 202 /* AfterDelete */, OnAfterDeleteDatagrid1, VTS_NONE)
	ON_EVENT(CDataGridView, IDC_DATAGRID1, 207 /* BeforeDelete */, OnBeforeDeleteDatagrid1, VTS_PI2)
	ON_EVENT(CDataGridView, IDC_DATAGRID1, -602 /* KeyDown */, OnKeyDownDatagrid1, VTS_PI2 VTS_I2)
	ON_EVENT(CDataGridView, IDC_POINTSINFOGRID, -601 /* DblClick */, OnDblClickPointsinfogrid, VTS_NONE)
	ON_EVENT(CDataGridView, IDC_POINTSINFOGRID, -602 /* KeyDown */, OnKeyDownPointsinfogrid, VTS_PI2 VTS_I2)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CDataGridView::OnSelChangeDatagrid1(short FAR* Cancel) 
{
	selRow = m_ctlDataGrid.GetRow();
	
	CMainFrame* pFrame = (CMainFrame*) AfxGetApp()->GetMainWnd();
	pFrame->selRow = selRow;
}

DROPEFFECT CDataGridView::OnDragEnter(COleDataObject* pDataObject, DWORD dwKeyState, CPoint point) 
{
	// TODO: Add your specialized code here and/or call the base class

	return CFormView::OnDragEnter(pDataObject, dwKeyState, point);
}

void CDataGridView::OnDragLeave() 
{
	// TODO: Add your specialized code here and/or call the base class

	CFormView::OnDragLeave();
}

DROPEFFECT CDataGridView::OnDragOver(COleDataObject* pDataObject, DWORD dwKeyState, CPoint point) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	return CFormView::OnDragOver(pDataObject, dwKeyState, point);
}

BOOL CDataGridView::OnDrop(COleDataObject* pDataObject, DROPEFFECT dropEffect, CPoint point) 
{

	return CFormView::OnDrop(pDataObject, dropEffect, point);
}

void CDataGridView::OnMouseDownDatagrid1(short Button, short Shift, long X, long Y) 
{
/*
	COleDropSource mydropsource;
    COleDataSource myoledata;
	//myoledata.DoDragDrop(DROPEFFECT_MOVE, NULL, NULL);
	myoledata.DoDragDrop(DROPEFFECT_MOVE, NULL, &mydropsource);
*/
}

int CDataGridView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CFormView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	return 0;
}

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

void CDataGridView::OnDblClickPointsinfogrid() 
{
	CMainFrame* pFrame = (CMainFrame*) AfxGetApp()->GetMainWnd();

	//LONG templ = myPointsInfoGrid.GetRow();
	CColumns tempcols = (CColumns) myPointsInfoGrid.GetColumns();
	LONG templ = tempcols.GetCount();
	CColumn tempcol = tempcols.GetItem(COleVariant((long)2));
	CString iname = tempcol.GetText();//所在图像的名字;
	tempcol = tempcols.GetItem(COleVariant((long)0));
	CString ptidstr = tempcol.GetText();//点在表中的ID;
	LONG ptid = strtol(ptidstr, NULL, 10);
	tempcol = tempcols.GetItem(COleVariant((long)1));
	CString rgclass = tempcol.GetText();//点所属的类;
	tempcol = tempcols.GetItem(COleVariant((long)3));
	CString pxstr = tempcol.GetText();//点在图像中的位置X;
	INT px = strtol(pxstr, NULL, 10);
	tempcol = tempcols.GetItem(COleVariant((long)4));
	CString pystr = tempcol.GetText();//点在图像中的位置X;
	INT py = strtol(pystr, NULL, 10);

	//更新mainfrm中的图像名字, 重设当前选择点,并在VIEW中重画图像;
	pFrame->selName = iname;
	pFrame->pImageView->SetCurSelTrainPoint(ptid, iname, 
		px, py, rgclass);
	GetDocument()->RefreshView();//刷新文档类中的图像数据;
	//重新在图像表中定位到该图像;
	//重新在现存类表中定位到该类;

}

void CDataGridView::OnDblClickDatagrid1() 
{
	CMainFrame* pFrame = (CMainFrame*) AfxGetApp()->GetMainWnd();

	selRow = m_ctlDataGrid.GetRow();
	CColumns tempcols = (CColumns) m_ctlDataGrid.GetColumns();
	LONG templ = tempcols.GetCount();
	CColumn tempcol = tempcols.GetItem(COleVariant((long)0));
	CString tempstr = tempcol.GetText();
	pFrame->selID = tempstr;
	tempcol = tempcols.GetItem(COleVariant((long)1));
	tempstr = tempcol.GetText();
	pFrame->selName = tempstr;
	pFrame->selRow = selRow;

	//清除当前选择训练点;
	pFrame->pImageView->ClearCurTrainPt();

	GetDocument()->RefreshView();//刷新文档类中的图像数据;

	pFrame->pImageView->Invalidate(FALSE);//刷新图像视图;
}


void CDataGridView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
/*
	CClientDC dc(this);
	HRGN* curgn = new HRGN;
	dc.GetWindow()->GetWindowRgn(*curgn);
	m_ctlDataGrid.SetWindowRgn(*curgn, 1);
*/
	CView::OnVScroll(nSBCode, nPos, pScrollBar);
}

void CDataGridView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
/*
	CMainFrame* pFrame = (CMainFrame*) AfxGetApp()->GetMainWnd();
	UpdateGridDetails( pFrame->tableName );
*/
	//CFormView::OnHScroll(nSBCode, nPos, pScrollBar);
	CView::OnHScroll(nSBCode, nPos, pScrollBar);
}

void CDataGridView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	CFormView::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CDataGridView::OnAfterDeleteDatagrid1() 
{
/*
	CMainFrame* pFrame = (CMainFrame*) AfxGetApp()->GetMainWnd();
	pFrame->RefreshTableID(deleteID);//重排表内ID,使其唯一;
*/
}

void CDataGridView::OnBeforeDeleteDatagrid1(short FAR* Cancel) 
{
}

void CDataGridView::OnKeyDownDatagrid1(short FAR* KeyCode, short Shift) 
{
    SHORT  downkey = *KeyCode;
	if (downkey==46)
	{
		//“DEL”键,删除当前记录;
		CColumns tempcols = (CColumns) m_ctlDataGrid.GetColumns();
		CColumn tempcol = tempcols.GetItem(COleVariant((long)0));
		CString tempstr = tempcol.GetText();
		deleteID = strtol(tempstr, NULL, 10);
		CMainFrame* pFrame = (CMainFrame*) AfxGetApp()->GetMainWnd();
		pFrame->RefreshTableID(deleteID);
	}

}


void CDataGridView::OnKeyDownPointsinfogrid(short FAR* KeyCode, short Shift) 
{
    SHORT  downkey = *KeyCode;
	if (downkey==46)
	{
		//“DEL”键,删除当前记录;
		CColumns tempcols = (CColumns) myPointsInfoGrid.GetColumns();
		CColumn tempcol = tempcols.GetItem(COleVariant((long)0));
		CString idstr = tempcol.GetText();
		deleteID = strtol(idstr, NULL, 10);
		tempcol = tempcols.GetItem(COleVariant((long)1));
		CString regionclass = tempcol.GetText();
		CMainFrame* pFrame = (CMainFrame*) AfxGetApp()->GetMainWnd();
		pFrame->RefreshPtTableID(deleteID, regionclass);
	}	
}

⌨️ 快捷键说明

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