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

📄 treedisp.cpp

📁 《面向对象程序设计实用教程》一书的源码
💻 CPP
字号:
// TreeDisp.cpp : implementation file
//

#include "stdafx.h"
#include "cge.h"
#include "TreeDisp.h"

#include "cgeView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CTreeDisp

IMPLEMENT_DYNCREATE(CTreeDisp, CView)

CTreeDisp::CTreeDisp()
{
	Fontl.CreatePointFont(80,"Arial");//Times New Roman
	Fontb.CreatePointFont(90,"Arial");//Times New Roman
	TextHeight = 11;
	ScrlIndex = ScrollNum = 0;
}

CTreeDisp::~CTreeDisp()
{
}

BEGIN_MESSAGE_MAP(CTreeDisp, CView)
	//{{AFX_MSG_MAP(CTreeDisp)
	ON_WM_CREATE()
	ON_WM_LBUTTONDOWN()
	ON_WM_VSCROLL()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTreeDisp drawing
void CTreeDisp::DrawBottom()
{
	CCgeDoc *pDoc = GetDocument();
	BOOL& bShowSubCurve=pDoc->bShowSubCurve;

	CRect rect;
	GetClientRect( &rect );

	CClientDC dc(this);

	int x1,y1,x2,y2;
	rect.top = rect.bottom-cyHScroll;
	x1 = rect.left;
	y1 = rect.top;
	x2 = rect.right;
	y2 = rect.bottom;
	CBrush *BrushOld=(CBrush *)dc.SelectStockObject(LTGRAY_BRUSH);
	CPen Pen(PS_SOLID,1,RGB(128,128,128));
	CPen *PenOld = (CPen *)dc.SelectObject(&Pen);
	dc.Rectangle(x1,y1,x2,y2);
	dc.SelectStockObject(WHITE_BRUSH);
	SelectRect.left = x1+4;
	SelectRect.top  = y1+3;
	SelectRect.right = x1+18;
	SelectRect.bottom = y2-2;
	dc.SelectStockObject(WHITE_PEN);
	dc.MoveTo(x1+1,y2);
	dc.LineTo(x1+1,y1+1);
	dc.LineTo(x2-1,y1+1);
	dc.SelectObject(BrushOld);
	CFont font;
	font.CreatePointFont(10*cyHScroll-75,_T("宋体"));
	CFont *FontOld = (CFont *)dc.SelectObject( &font );
	int OldBKMode = dc.SetBkMode(TRANSPARENT);
	rect.left += 9;
	rect.top += 3;
	dc.DrawText("显示/隐藏子曲线",&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
	dc.SelectObject(&Pen);
	dc.Rectangle( SelectRect );
	if(bShowSubCurve)
	{
		dc.SelectStockObject(BLACK_PEN);
		dc.MoveTo(SelectRect.left,SelectRect.top);
		dc.LineTo(SelectRect.right,SelectRect.bottom);
		dc.MoveTo(SelectRect.left,SelectRect.bottom);
		dc.LineTo(SelectRect.right,SelectRect.top);
	}
	dc.SelectObject( &FontOld );
	dc.SetBkMode(OldBKMode);
	dc.SelectObject(PenOld);
}

void CTreeDisp::OnDraw(CDC* pDC)
{
	CCgeDoc* pDoc = GetDocument();
	if(pDoc->bReadError) return;
	Channel* m_pChannel = pDoc->m_pChannel;
	if(m_pChannel==NULL) return;

	DrawBottom();

	WORD CurveHeigh = pDoc->HalfCurveHeigh*2;
	WORD NumPerView = pDoc->m_NumPerView;
	WORD m_ChannelNum = pDoc->m_ChannelNum;
	WORD m_ViewChannel = pDoc->m_ViewChannel;
//	WORD m_ViewChannelMax = pDoc->m_ViewChannelMax;
	ChannelNumber *m_pChannelIndex = pDoc->m_pChannelIndex;
	long **PSamplData = pDoc->PSamplData;
	int nWhichCurve = pDoc->nWhichCurve;
	int BKMode = pDC->SetBkMode(TRANSPARENT);
	BOOL& bShowSubCurve = pDoc->bShowSubCurve;
	CMDIFrameWnd *pFrame=(CMDIFrameWnd *)AfxGetApp()->m_pMainWnd;
	CChildFrame *pChild=(CChildFrame *)pFrame->GetActiveFrame();
	CCgeView *pCgeView=(CCgeView *)pChild->m_wndSplitter.GetPane(0,1);
	unsigned long& LCursor = pDoc->LCursor;
	unsigned long& RCursor = pDoc->RCursor;
	int x,y;
	CString strTemp;
	CRect rect;
	GetClientRect( &rect );
	int kl = ScrlIndex;
	WORD wIndex = m_ViewChannel;
	BOOL bSub = FALSE;
	WORD wChannel = m_pChannelIndex[wIndex].No;
	int nSubIndex;
	while(kl && bShowSubCurve)
	{
		if(m_pChannelIndex[wIndex].bShow)
		{
			kl--;
			nSubIndex = m_pChannelIndex[wIndex].nNumb;
			x = nSubIndex;
			if(nSubIndex > 0)
			{
				bSub = TRUE;
				while(kl && x) { kl--; x--; }
				if(kl == 0 && x > 0)
				{
					bSub = TRUE;
					nSubIndex = nSubIndex-x;
					wChannel=m_pChannelIndex[wIndex].pSub[nSubIndex];
					goto draw;
				}
			}
		}
		bSub = FALSE;
		wIndex ++;
		while(wIndex <= m_ChannelNum &&
			m_pChannelIndex[wIndex].bShow==0) wIndex++;
		wChannel = m_pChannelIndex[wIndex].No;
	}
draw:
	if(!bSub) nSubIndex = -1;
	WORD wIndexOld = wIndex;
	while(wIndex <= m_ChannelNum && kl < NumPerView)
	{
		if(bSub)
		{
			wIndex = pWgl_32View->GetCurveNumberIndex(wChannel);
			x = 12;
		}
		else
			x = 2;
		double dmin,dmax;
		y = kl*CurveHeigh;
		if(!bSub && nWhichCurve == wChannel)
		{
			CPen RectPen(PS_SOLID, 2, RGB(0,255,255));
			CPen *OldRectPen=(CPen *)pDC->SelectObject(&RectPen);
			CBrush *OldBrush=(CBrush *)pDC->SelectStockObject(BLACK_BRUSH);
			CRect r( rect );
			r.top = y;
			r.bottom = y+CurveHeigh;
			pDC->Rectangle( &r );
			pDC->SelectObject(OldBrush);
			pDC->SelectObject(OldRectPen);
		}
		COLORREF GOldRGB=pDC->SetTextColor(m_pChannelIndex[wIndex].Color);
		CFont *OldFont=(CFont *)pDC->SelectObject(CFont::FromHandle(Fontb));
		Channel* pChannel = &m_pChannel[wChannel];
		if(pChannel->VAD > -1)
			strTemp.Format("A%02d, %s",
			pChannel->ChannelNum, pChannel->ChannelName);
		else
			strTemp.Format("D%02d, %s",
			pChannel->ChannelNum-pDoc->m_AnaNum,pChannel->ChannelName);
		UINT TextAlign = pDC->SetTextAlign(TA_LEFT);
		y += 1;
		pDC->TextOut( x, y, strTemp );
		y += TextHeight;
		pDC->SelectObject(CFont::FromHandle(Fontl));
		if(pChannel->VAD > -1)
		{
			//dmin = pChannel->min*pChannel->a+pChannel->b;
			//dmax = pChannel->max*pChannel->a+pChannel->b;
			//strTemp.Format("%10.4f%s",dmax,pChannel->Unit);
			strTemp.Format("%s",pChannel->Unit);
			strTemp.TrimLeft();
			int nx = rect.right-2;
			pDC->SetTextAlign(TA_RIGHT);
			pDC->TextOut( nx, y, strTemp );
			//strTemp.Format("%10.4f%s",dmin,pChannel->Unit);
			//pDC->TextOut( x, y+TextHeight, strTemp );
		}
		y += TextHeight*2/3;
		if(pChannel->VAD > -1)
		{
			dmin = PSamplData[wChannel][LCursor]*pChannel->a+pChannel->b;
			dmax = PSamplData[wChannel][RCursor]*pChannel->a+pChannel->b;
			pDoc->GetDisplayValue(wChannel,dmin,dmax);
			strTemp.Format("%10.4f, %-10.4f",dmin,dmax);
		}
		else
		{
			dmin = PSamplData[wChannel][LCursor];
			dmax = PSamplData[wChannel][RCursor];
			if(dmin == 999999 || dmax == 999999)
				strTemp = _T(" ");
			else
				strTemp.Format("%3.0f, %-3.0f",dmin,dmax);
		}
		strTemp.TrimLeft();
		pDC->SetTextAlign(TA_LEFT);
		pDC->TextOut( x, y, strTemp );
		if(bShowSubCurve && nSubIndex<m_pChannelIndex[wIndexOld].nNumb-1)
		{
			bSub = TRUE;
			nSubIndex ++;
			wChannel=m_pChannelIndex[wIndexOld].pSub[nSubIndex];
			wIndex = pWgl_32View->GetCurveNumberIndex(wChannel);
			kl ++;
			continue;
		}
		pDC->SetTextColor( GOldRGB );
		pDC->SelectObject( OldFont );
		pDC->SetTextAlign(TextAlign);
		wIndex = wIndexOld;
		wIndex++;
		while(wIndex<m_ChannelNum && m_pChannelIndex[wIndex].bShow==0)
			wIndex++;
		wChannel=m_pChannelIndex[wIndex].No;
		wIndexOld = wIndex;
		nSubIndex = -1;
		bSub = FALSE;
		kl++;
	}
	pDC->SetBkMode(BKMode);
}

/////////////////////////////////////////////////////////////////////////////
// CTreeDisp diagnostics

#ifdef _DEBUG
void CTreeDisp::AssertValid() const
{
	CView::AssertValid();
}

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

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

/////////////////////////////////////////////////////////////////////////////
// CTreeDisp message handlers

BOOL CTreeDisp::PreCreateWindow(CREATESTRUCT& cs) 
{
	cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW, 
		AfxGetApp()->LoadStandardCursor(IDC_ARROW), (HBRUSH)GetStockObject(BLACK_BRUSH));
	return CView::PreCreateWindow(cs);
}

BOOL CTreeDisp::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
	dwStyle = dwStyle | WS_VSCROLL;
	return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}

int CTreeDisp::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	cyHScroll = GetSystemMetrics(SM_CYHSCROLL);
	return 0;
}

void CTreeDisp::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) 
{
	CSplitterWnd *pWnd = (CSplitterWnd *)GetParent();
	pWnd->SetActivePane(0,1);
}

BOOL CTreeDisp::GetSubCurveIndex( CPoint point, int& Num, int& NumS )
{
	CWgl_32Doc *pDoc = GetDocument();
	int CurveHeigh = pDoc->HalfCurveHeigh*2;
	WORD m_ViewChannel = pDoc->m_ViewChannel;
	WORD m_ViewChannelMax = pDoc->m_ViewChannelMax;
	ChannelNumber *m_pChannelIndex = pDoc->m_pChannelIndex;
	int endy = ScrlIndex*CurveHeigh+point.y;
	int y = 0;
	Num = m_ViewChannel;
	NumS = -1;
	while(y < endy || Num <= m_ViewChannelMax)
	{
		if(m_pChannelIndex[Num].bShow)
		{
			if(y >= endy) return TRUE;
			y += CurveHeigh;
			int nNumb = m_pChannelIndex[Num].nNumb;
			NumS = -1;
			while(nNumb)
			{
				nNumb--; NumS ++;
				y += CurveHeigh;
				if(y > endy)
				{
					NumS = m_pChannelIndex[Num].pSub[NumS];
					return FALSE;
				}
			}
		}
		Num++;
	}
	return TRUE;
}

void CTreeDisp::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CWgl_32Doc *pDoc = GetDocument();
	if(pDoc->bReadError) return;
	CRect rect;
	GetClientRect( &rect );
	int cy = rect.bottom-cyHScroll;
	if(pDoc->bDelSubCurve && point.y < cy)
	{
		int Num, NumS;
		pDoc->bDelSubCurve = FALSE;
		if(GetSubCurveIndex(point,Num,NumS)) return;
		if(!pDoc->DeltChannelInform(Num,NumS)) return;
		SetVScroll();
		CMDIFrameWnd *pFrame=(CMDIFrameWnd *)AfxGetApp()->m_pMainWnd;
		CChildFrame *pChild=(CChildFrame *)pFrame->GetActiveFrame();
		CWgl_32View *pWgl_32View = 
			(CWgl_32View *)pChild->m_wndSplitter.GetPane(0,1);
		pWgl_32View->InvalidateRect( NULL );
		return;
	}
	if(SelectRect.PtInRect(point))
	{
		BOOL& bShowSubCurve = pDoc->bShowSubCurve;
		CClientDC dc(this);
		if(!bShowSubCurve)
		{
			CPen *OldPen = (CPen *)dc.SelectStockObject(BLACK_PEN);
			dc.MoveTo(SelectRect.left,SelectRect.top);
			dc.LineTo(SelectRect.right,SelectRect.bottom);
			dc.MoveTo(SelectRect.left,SelectRect.bottom);
			dc.LineTo(SelectRect.right,SelectRect.top);
			dc.SelectObject( OldPen );
		}
		else
		{
			CPen Pen(PS_SOLID,1,RGB(128,128,128));
			CPen *OldPen = (CPen *)dc.SelectObject(&Pen);
			CBrush *BrushOld = (CBrush *)dc.SelectStockObject(WHITE_BRUSH);
			dc.Rectangle( SelectRect );
			dc.SelectObject( BrushOld );
			dc.SelectObject( OldPen );
		}
		bShowSubCurve = !bShowSubCurve;
		SetVScroll();
	}
}

int CTreeDisp::GetDisplayNumber()
{
	CWgl_32Doc *pDoc = GetDocument();
	WORD m_ViewChannel = pDoc->m_ViewChannel;
	WORD m_ViewChannelMax = pDoc->m_ViewChannelMax;
	BOOL bShowSubCurve = pDoc->bShowSubCurve;
	ChannelNumber *m_pChannelIndex = pDoc->m_pChannelIndex;
	int m_NumPerView = pDoc->m_NumPerView;
	int num = 0;
	int kl = 0;
	while(kl < m_NumPerView && m_ViewChannel <= m_ViewChannelMax)
	{
		if(m_pChannelIndex[m_ViewChannel].bShow)
		{
			kl++;
			num++;
			if(bShowSubCurve)
				num += m_pChannelIndex[m_ViewChannel].nNumb;
		}
		m_ViewChannel++;
	}
	return num;
}

void CTreeDisp::SetVScroll()
{
	CWgl_32Doc *pDoc = GetDocument();
	if(pDoc->bReadError) return;
	BOOL bShowSubCurve = pDoc->bShowSubCurve;
	int CurveHeigh = pDoc->HalfCurveHeigh*2;
	int m_NumPerView = pDoc->m_NumPerView;
	CRect rect;
	GetClientRect(&rect);
	SCROLLINFO ScrollInfo;
	ScrollInfo.cbSize = sizeof(ScrollInfo);
	ScrollNum = GetDisplayNumber();
	if(ScrollNum <= m_NumPerView)
		ScrollNum = 0;
	else
		ScrollNum = ScrollNum-m_NumPerView;
	ScrlIndex = 0;
	ScrollInfo.fMask  = SIF_RANGE | SIF_PAGE |SIF_POS;
	ScrollInfo.nMin = 0;
	ScrollInfo.nMax = ScrollNum;
	ScrollInfo.nPos = ScrlIndex;
	ScrollInfo.nPage  = 1;
	SetScrollInfo(SB_VERT, &ScrollInfo, TRUE);
	InvalidateRect( NULL );
}

void CTreeDisp::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	CWgl_32Doc *pDoc = GetDocument();
	if(!pDoc->bShowSubCurve) return;
	int ScrlIndexOld = ScrlIndex;
	switch(nSBCode) 
	{
		case SB_BOTTOM:
			ScrlIndex = 0;
			break;
		case SB_PAGEDOWN:
		case SB_LINEDOWN:
			ScrlIndex ++;
			if(ScrlIndex > ScrollNum)
				ScrlIndex = ScrollNum;
			break;
		case SB_PAGEUP:
		case SB_LINEUP:
			ScrlIndex --;
			if(ScrlIndex < 0)
				ScrlIndex = 0;
			break;
		case SB_TOP:
			ScrlIndex = 0;
			break;
		case SB_THUMBPOSITION:
			break;
	}
	if(ScrlIndexOld != ScrlIndex)
	{
		InvalidateRect( NULL );
		SetScrollPos(SB_VERT, ScrlIndex, TRUE);
	}
}

void CTreeDisp::OnInitialUpdate() 
{
	CView::OnInitialUpdate();	
	SetVScroll();	
}

⌨️ 快捷键说明

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