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

📄 graphviewctl.cpp

📁 《Visual C++视频/音频开发实用工程案例精选》一书的源代码
💻 CPP
字号:
// GraphViewCtl.cpp : Implementation of the CGraphViewCtrl ActiveX Control class.

#include "stdafx.h"
#include "GraphView.h"
#include "GraphViewCtl.h"


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


IMPLEMENT_DYNCREATE(CGraphViewCtrl, COleControl)


/////////////////////////////////////////////////////////////////////////////
// Message map

BEGIN_MESSAGE_MAP(CGraphViewCtrl, COleControl)
	//{{AFX_MSG_MAP(CGraphViewCtrl)
	// NOTE - ClassWizard will add and remove message map entries
	//    DO NOT EDIT what you see in these blocks of generated code !
	//}}AFX_MSG_MAP
	ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// Dispatch map

BEGIN_DISPATCH_MAP(CGraphViewCtrl, COleControl)
	//{{AFX_DISPATCH_MAP(CGraphViewCtrl)
	DISP_PROPERTY_NOTIFY(CGraphViewCtrl, "ForeColor", m_foreColor, OnForeColorChanged, VT_COLOR)
	DISP_PROPERTY_NOTIFY(CGraphViewCtrl, "LineType", m_lineType, OnLineTypeChanged, VT_I4)
	DISP_PROPERTY_NOTIFY(CGraphViewCtrl, "LineWidth", m_lineWidth, OnLineWidthChanged, VT_I4)
	DISP_PROPERTY_NOTIFY(CGraphViewCtrl, "BackColor", m_backColor, OnBackColorChanged, VT_COLOR)
	DISP_FUNCTION(CGraphViewCtrl, "Clear", Clear, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CGraphViewCtrl, "SetScale", SetScale, VT_EMPTY, VTS_R8 VTS_R8 VTS_R8 VTS_R8)
	DISP_FUNCTION(CGraphViewCtrl, "LineTo", LineTo, VT_EMPTY, VTS_R8 VTS_R8)
	DISP_FUNCTION(CGraphViewCtrl, "MoveTo", MoveTo, VT_EMPTY, VTS_R8 VTS_R8)
	DISP_FUNCTION(CGraphViewCtrl, "Multiline", Multiline, VT_EMPTY, VTS_I4 VTS_PR8 VTS_PR8)
	DISP_FUNCTION(CGraphViewCtrl, "DrawLine", DrawLine, VT_EMPTY, VTS_R8 VTS_R8 VTS_R8 VTS_R8)
	//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()


/////////////////////////////////////////////////////////////////////////////
// Event map

BEGIN_EVENT_MAP(CGraphViewCtrl, COleControl)
	//{{AFX_EVENT_MAP(CGraphViewCtrl)
	// NOTE - ClassWizard will add and remove event map entries
	//    DO NOT EDIT what you see in these blocks of generated code !
	//}}AFX_EVENT_MAP
END_EVENT_MAP()


/////////////////////////////////////////////////////////////////////////////
// Initialize class factory and guid

IMPLEMENT_OLECREATE_EX(CGraphViewCtrl, "GRAPHVIEW.GraphViewCtrl.1",
	0x37573b5e, 0x2a9a, 0x4686, 0x9c, 0xb7, 0x8d, 0x26, 0x11, 0xc4, 0x92, 0x72)


/////////////////////////////////////////////////////////////////////////////
// Type library ID and version

IMPLEMENT_OLETYPELIB(CGraphViewCtrl, _tlid, _wVerMajor, _wVerMinor)


/////////////////////////////////////////////////////////////////////////////
// Interface IDs

const IID BASED_CODE IID_DGraphView =
		{ 0xb2c79461, 0x51b8, 0x4eeb, { 0x8e, 0x89, 0x8b, 0xf8, 0xdc, 0x83, 0xda, 0x60 } };
const IID BASED_CODE IID_DGraphViewEvents =
		{ 0x2e5c7806, 0x20f7, 0x493a, { 0xad, 0x4c, 0xfd, 0xf6, 0xb7, 0xb0, 0x25, 0xd4 } };


/////////////////////////////////////////////////////////////////////////////
// Control type information

static const DWORD BASED_CODE _dwGraphViewOleMisc =
	OLEMISC_ACTIVATEWHENVISIBLE |
	OLEMISC_SETCLIENTSITEFIRST |
	OLEMISC_INSIDEOUT |
	OLEMISC_CANTLINKINSIDE |
	OLEMISC_RECOMPOSEONRESIZE;

IMPLEMENT_OLECTLTYPE(CGraphViewCtrl, IDS_GRAPHVIEW, _dwGraphViewOleMisc)


/////////////////////////////////////////////////////////////////////////////
// CGraphViewCtrl::CGraphViewCtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CGraphViewCtrl

BOOL CGraphViewCtrl::CGraphViewCtrlFactory::UpdateRegistry(BOOL bRegister)
{
	// TODO: Verify that your control follows apartment-model threading rules.
	// Refer to MFC TechNote 64 for more information.
	// If your control does not conform to the apartment-model rules, then
	// you must modify the code below, changing the 6th parameter from
	// afxRegApartmentThreading to 0.

	if (bRegister)
		return AfxOleRegisterControlClass(
			AfxGetInstanceHandle(),
			m_clsid,
			m_lpszProgID,
			IDS_GRAPHVIEW,
			IDB_GRAPHVIEW,
			afxRegApartmentThreading,
			_dwGraphViewOleMisc,
			_tlid,
			_wVerMajor,
			_wVerMinor);
	else
		return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}


/////////////////////////////////////////////////////////////////////////////
// CGraphViewCtrl::CGraphViewCtrl - Constructor

CGraphViewCtrl::CGraphViewCtrl()
{
	InitializeIIDs(&IID_DGraphView, &IID_DGraphViewEvents);

	//Default properties
	m_backColor		= RGB(255,255,255);
	m_foreColor		= RGB(  0,  0,  0);
	m_lineType		= PS_SOLID;
	m_lineWidth		= 1;

	//Instanciate drawing tools
	ptrPen		= new CPen(m_lineType, m_lineWidth, m_foreColor);
	ptrBrush	= new CBrush(m_backColor);
	penpos		= (0,0);
}


/////////////////////////////////////////////////////////////////////////////
// CGraphViewCtrl::~CGraphViewCtrl - Destructor

CGraphViewCtrl::~CGraphViewCtrl()
{
	delete ptrPen;
	delete ptrBrush;
}


/////////////////////////////////////////////////////////////////////////////
// CGraphViewCtrl::OnDraw - Drawing function

void CGraphViewCtrl::OnDraw(CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
	CRect bufRect = rcBounds;

	//Drawing rect
	pdc->FillRect(rcBounds, ptrBrush);
	pdc->DrawEdge(bufRect, EDGE_BUMP, BF_RECT);
}


/////////////////////////////////////////////////////////////////////////////
// CGraphViewCtrl::DoPropExchange - Persistence support

void CGraphViewCtrl::DoPropExchange(CPropExchange* pPX)
{
	ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
	COleControl::DoPropExchange(pPX);

	// TODO: Call PX_ functions for each persistent custom property.

}


/////////////////////////////////////////////////////////////////////////////
// CGraphViewCtrl::OnResetState - Reset control to default state

void CGraphViewCtrl::OnResetState()
{
	COleControl::OnResetState();  // Resets defaults found in DoPropExchange

	// TODO: Reset any other control state here.
}


//--- PUBLIC METHODS ---------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------
void CGraphViewCtrl::Clear() 
{
	CDC *pdc;
	CRect rcBounds;

	pdc = GetDC();
	pdc->GetClipBox(&rcBounds);

	//Drawing rect
	pdc->FillRect(rcBounds, ptrBrush);
	pdc->DrawEdge(rcBounds, EDGE_BUMP, BF_RECT);

	ReleaseDC(pdc);
}

//----------------------------------------------------------------------------------------------------------
void CGraphViewCtrl::SetScale(double x1, double y1, double x2, double y2) 
{
	CDC *pdc=GetDC();
	CRect rcBounds;
	GetClientRect(&rcBounds);

	x1world = x1;
	y1world = y1;
	x2world = x2;
	y2world = y2;
	dxworld = x2 - x1;
	dyworld = y2 - y1;

	dxwin = rcBounds.Width();
	dywin = rcBounds.Height();

	ReleaseDC(pdc);
}

//----------------------------------------------------------------------------------------------------------
void CGraphViewCtrl::DrawLine(double x1, double y1, double x2, double y2) 
{
	CDC		*pdc = GetDC();
	CPen	*oldPen = pdc->SelectObject(ptrPen);
	CRect rcBounds;
	GetClientRect(&rcBounds);

	CPoint p1,p2;

	p1 = WorldToWindows(x1, y1);
	p2 = WorldToWindows(x2, y2);

	pdc->MoveTo(p1);
	pdc->LineTo(p2);

	pdc->SelectObject(oldPen);
	penpos = p2;

	pdc->DrawEdge(rcBounds, EDGE_BUMP, BF_RECT);
	
	ReleaseDC(pdc);
}

//----------------------------------------------------------------------------------------------------------
void CGraphViewCtrl::Multiline(long segments, double FAR* xi, double FAR* yi) 
{
	CDC *pdc=GetDC();
	CPen *oldPen=pdc->SelectObject(ptrPen);
	CPoint p;

	p = WorldToWindows(xi[0], yi[0]);
	pdc->MoveTo(p);

	for (int i=1; i<segments; i++)
	{
		p = WorldToWindows(xi[i], yi[i]);
		pdc->LineTo(p);
	}
	pdc->SelectObject(oldPen);
	penpos = p;

	ReleaseDC(pdc);
}

//----------------------------------------------------------------------------------------------------------
void CGraphViewCtrl::MoveTo(double x, double y) 
{
	CPoint p = WorldToWindows(x, y);

	penpos = p;
}

//----------------------------------------------------------------------------------------------------------
void CGraphViewCtrl::LineTo(double x, double y)
{
	CDC *pdc		= GetDC();
	CPen *oldPen	= pdc->SelectObject(ptrPen);
	CPoint p		= WorldToWindows(x, y);

	pdc->MoveTo(penpos);
	pdc->LineTo(p);
	penpos=p;
	pdc->SelectObject(oldPen);

	ReleaseDC(pdc);
}

//--- PRIVATE METHODS --------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------
CPoint CGraphViewCtrl::WorldToWindows(double x, double y)
{
	CPoint bufPoint;

	bufPoint.x = (int)((x - x1world) * (dxwin / dxworld));
	bufPoint.y = (int)((y2world - y) * (dywin / dyworld));

	return bufPoint;
}

//--- PROPERTIES -------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------
void CGraphViewCtrl::OnBackColorChanged() 
{
	CDC *pdc;
	CRect rcBounds;

	delete ptrBrush;
	ptrBrush = new CBrush(m_backColor);

	pdc = GetDC();
	pdc->GetClipBox(&rcBounds);
	
	//Drawing rect
	pdc->FillRect(rcBounds, ptrBrush);
	pdc->DrawEdge(rcBounds, EDGE_BUMP, BF_RECT);
	ReleaseDC(pdc);

	SetModifiedFlag();
}

//----------------------------------------------------------------------------------------------------------
void CGraphViewCtrl::OnForeColorChanged() 
{
	delete ptrPen;
	ptrPen = new CPen(m_lineType, m_lineWidth, m_foreColor);

	SetModifiedFlag();
}

//----------------------------------------------------------------------------------------------------------
void CGraphViewCtrl::OnLineTypeChanged() 
{
	delete ptrPen;
	ptrPen = new CPen(m_lineType, m_lineWidth, m_foreColor);

	SetModifiedFlag();
}

//----------------------------------------------------------------------------------------------------------
void CGraphViewCtrl::OnLineWidthChanged() 
{
	delete ptrPen;
	ptrPen = new CPen(m_lineType, m_lineWidth, m_foreColor);

	SetModifiedFlag();
}

⌨️ 快捷键说明

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