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

📄 smartgraph.h

📁 A source code for gragh
💻 H
字号:
// SmartGraph.h : Declaration of the SmartGraph
#pragma once
#include "resource.h"       // main symbols
#include <atlctl.h>
#include "comutil.h"
#pragma comment(lib,"comsupp.lib")
#include <Math.h>
#include <atlcoll.h>
#include "GraphDataSet.h"
const static double PI = 3.1415926535897932384626433832795;

#define GRAPH_RESIZED	1
#define DATA_CHANGED	2
#define SEL_CHANGED		3
#define GRID_CHANGED	4


// ISmartGraph
[
	object,
	uuid(1EECA078-14D3-427D-AEA5-F9342ABBFC7C),
	dual,
	helpstring("ISmartGraph Interface"),
	pointer_default(unique)
]
__interface ISmartGraph : public IDispatch
{
	[propput, bindable, requestedit, id(DISPID_BACKCOLOR)]
	HRESULT BackColor([in]OLE_COLOR clr);
	[propget, bindable, requestedit, id(DISPID_BACKCOLOR)]
	HRESULT BackColor([out,retval]OLE_COLOR* pclr);
	[propput, bindable, requestedit, id(DISPID_BORDERCOLOR)]
	HRESULT BorderColor([in]OLE_COLOR clr);
	[propget, bindable, requestedit, id(DISPID_BORDERCOLOR)]
	HRESULT BorderColor([out, retval]OLE_COLOR* pclr);
	[propput, bindable, requestedit, id(DISPID_FORECOLOR)]
	HRESULT ForeColor([in]OLE_COLOR clr);
	[propget, bindable, requestedit, id(DISPID_FORECOLOR)]
	HRESULT ForeColor([out,retval]OLE_COLOR* pclr);
	[propget, id(1), helpstring("property Title")] HRESULT Title([out, retval] BSTR* pVal);
	[propput, id(1), helpstring("property Title")] HRESULT Title([in] BSTR newVal);
	[propget, id(2), helpstring("property xLable")] HRESULT xLable([out, retval] BSTR* pVal);
	[propput, id(2), helpstring("property xLable")] HRESULT xLable([in] BSTR newVal);
	[propget, id(3), helpstring("property yLable")] HRESULT yLable([out, retval] BSTR* pVal);
	[propput, id(3), helpstring("property yLable")] HRESULT yLable([in] BSTR newVal);
	[propget, id(4), helpstring("property Columns")] HRESULT Columns([out, retval] LONG* pVal);
	[propput, id(4), helpstring("property Columns")] HRESULT Columns([in] LONG newVal);
	[propget, id(5), helpstring("property Rows")] HRESULT Rows([out, retval] LONG* pVal);
	[propput, id(5), helpstring("property Rows")] HRESULT Rows([in] LONG newVal);
	[propget, id(6), helpstring("property MarginBottom")] HRESULT MarginBottom([out, retval] LONG* pVal);
	[propput, id(6), helpstring("property MarginBottom")] HRESULT MarginBottom([in] LONG newVal);
	[propget, id(7), helpstring("property MarginTop")] HRESULT MarginTop([out, retval] LONG* pVal);
	[propput, id(7), helpstring("property MarginTop")] HRESULT MarginTop([in] LONG newVal);
	[propget, id(8), helpstring("property MarginRight")] HRESULT MarginRight([out, retval] LONG* pVal);
	[propput, id(8), helpstring("property MarginRight")] HRESULT MarginRight([in] LONG newVal);
	[propget, id(9), helpstring("property MarginLeft")] HRESULT MarginLeft([out, retval] LONG* pVal);
	[propput, id(9), helpstring("property MarginLeft")] HRESULT MarginLeft([in] LONG newVal);
	[id( 10), helpstring("method SetData")] HRESULT SetData([in] DOUBLE* xData, [in] DOUBLE* yData, [in] LONG NumOfPoints, [in] VARIANT_BOOL bResample);
	[id(11), helpstring("method SetDataWithDetail")] HRESULT SetData2([in] DOUBLE* xData, [in] DOUBLE* yData, [in] LONG NumOfPoints, 
		[in] DOUBLE xMin, [in] DOUBLE xMax, [in] DOUBLE yMin, [in] DOUBLE yMax, [in] VARIANT_BOOL bResample);
	[id(12), helpstring("method ShowGrid")] HRESULT ShowGrid([in] VARIANT_BOOL bShow);
	[id(13), helpstring("method UpdateGraph")] HRESULT UpdateGraph(void);
	[id(14), helpstring("method SetPlotType")] HRESULT SetPlotType([in] LONG nType);
	[id(15), helpstring("method GetPlotRect")] HRESULT GetPlotRect([out] LONG* left, [out] LONG* top, [out] LONG* right,[out] LONG* bottom);
	[id(16), helpstring("method GetClientRect")] HRESULT GetClientRect([out] LONG* left, [out] LONG* top, [out] LONG* right,[out] LONG* bottom);
	[id(17), helpstring("method ZoomIn")] HRESULT ZoomIn([in] LONG FromSample, [in] LONG ToSample);
	[id(18), helpstring("method ZoomInByPercent")] HRESULT ZoomInByPercent([in] DOUBLE FromPercent, [in] DOUBLE ToPercent);
	[id(19), helpstring("method ZoomOut")] HRESULT ZoomOut(void);
	[id(20), helpstring("Resets graph view to original view"), helpcontext(20)] HRESULT Reset(void);
	[id(21), helpstring("method GetDisplayedRange")] HRESULT GetDisplayedRange([out] LONGLONG* nStartSample, [out] LONGLONG* nEndSample);
	[id(22), helpstring("method SetLegendText")] HRESULT SetLegendText([in] BSTR strText, LONG top, LONG left, LONG bottom, LONG right);
	[id(23), helpstring("method SetParentWnd")] HRESULT SetParentWnd([in] OLE_HANDLE hWnd);
};


// _ISmartGraphEvents
[
	uuid("1F12078D-03B2-4C25-84CA-3191E7DF3FFB"),
	dispinterface,
	helpstring("_ISmartGraphEvents Interface")
]
__interface _ISmartGraphEvents
{
	[id(1), helpstring("method LButtonDown")] HRESULT LButtonDown([in] LONG xPos, LONG yPos, LONGLONG wParam);
	[id(2), helpstring("method LButtonUp")] HRESULT LButtonUp([in] LONG xPos, LONG yPos, LONGLONG wParam);
	[id(3), helpstring("method MouseMove")] HRESULT MouseMove([in] LONG xPos, LONG yPos, LONGLONG wParam);
};

// SmartGraph
[
	coclass,
	threading("apartment"),
	vi_progid("Graph.SmartGraph"),
	progid("Graph.SmartGraph.1"),
	version(1.0),
	uuid("A2919A75-B379-4BBD-8D95-15BEB3BAA140"),
	helpstring("SmartGraph Class"),
	event_source("com"),
	support_error_info(ISmartGraph),
	registration_script("control.rgs")
]
class ATL_NO_VTABLE SmartGraph : 
	public CStockPropImpl<SmartGraph, ISmartGraph>,
	public IPersistStreamInitImpl<SmartGraph>,
	public IOleControlImpl<SmartGraph>,
	public IOleObjectImpl<SmartGraph>,
	public IOleInPlaceActiveObjectImpl<SmartGraph>,
	public IViewObjectExImpl<SmartGraph>,
	public IOleInPlaceObjectWindowlessImpl<SmartGraph>,
	public IPersistStorageImpl<SmartGraph>,
	public ISpecifyPropertyPagesImpl<SmartGraph>,
	public IQuickActivateImpl<SmartGraph>,
	public IDataObjectImpl<SmartGraph>,
	public CComControl<SmartGraph>
{
public:

	SmartGraph()
		: m_nRow(5)
		, m_nCol(5)
	{
	}

DECLARE_OLEMISC_STATUS(OLEMISC_RECOMPOSEONRESIZE | 
	OLEMISC_CANTLINKINSIDE | 
	OLEMISC_INSIDEOUT | 
	OLEMISC_ACTIVATEWHENVISIBLE | 
	OLEMISC_SETCLIENTSITEFIRST
)


BEGIN_PROP_MAP(SmartGraph)
	PROP_DATA_ENTRY("_cx", m_sizeExtent.cx, VT_UI4)
	PROP_DATA_ENTRY("_cy", m_sizeExtent.cy, VT_UI4)
	PROP_ENTRY("BackColor", DISPID_BACKCOLOR, CLSID_StockColorPage)
	PROP_ENTRY("BorderColor", DISPID_BORDERCOLOR, CLSID_StockColorPage)
	PROP_ENTRY("ForeColor", DISPID_FORECOLOR, CLSID_StockColorPage)
	// Example entries
	// PROP_ENTRY("Property Description", dispid, clsid)
	// PROP_PAGE(CLSID_StockColorPage)
END_PROP_MAP()


BEGIN_MSG_MAP(SmartGraph)
	CHAIN_MSG_MAP(CComControl<SmartGraph>)
	MESSAGE_HANDLER(WM_LBUTTONDOWN, OnLButtonDown)
	MESSAGE_HANDLER(WM_MOUSEMOVE, OnMouseMove)
	MESSAGE_HANDLER(WM_LBUTTONUP, OnLButtonUp)
	DEFAULT_REFLECTION_HANDLER()
END_MSG_MAP()
// Handler prototypes:
//  LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
//  LRESULT CommandHandler(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
//  LRESULT NotifyHandler(int idCtrl, LPNMHDR pnmh, BOOL& bHandled);

	__event __interface _ISmartGraphEvents;
// IViewObjectEx
	DECLARE_VIEW_STATUS(VIEWSTATUS_SOLIDBKGND | VIEWSTATUS_OPAQUE)

// ISmartGraph
public:
		HRESULT OnDraw(ATL_DRAWINFO& di)
		{
			static bool bLock = false;
			if(bLock)
				return S_FALSE;
			bLock = true;
			RECT& rc = *(RECT*)di.prcBounds;
			static RECT rcOld = rc;
			if( ( (rc.right - rc.left) != (rcOld.right - rcOld.left) )||
				( (rc.bottom - rc.top) != (rcOld.bottom - rcOld.top) )
				)
				m_bDirty = true;
			rcOld.top = rc.top;
			rcOld.bottom = rc.bottom;
			rcOld.right = rc.right;
			rcOld.left = rc.left;

			// Set Clip region to the rectangle specified by di.prcBounds
			HRGN hRgnOld = NULL;
			if (GetClipRgn(di.hdcDraw, hRgnOld) != 1)
				hRgnOld = NULL;
			bool bSelectOldRgn = false;

			if(m_RgnClient)
				DeleteObject(m_RgnClient);
			m_RgnClient = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);

			if (m_RgnClient != NULL)
			{
				bSelectOldRgn = (SelectClipRgn(di.hdcDraw, m_RgnClient) != ERROR);
			}

			m_rcClient = rc;
			m_rcPlot.left = rc.left + m_nLMargin;
			m_rcPlot.top	= rc.top  + m_nTMargin;
			m_rcPlot.bottom	= rc.bottom - m_nBMargin;
			m_rcPlot.right	= rc.right - m_nRMargin;

			UpdateGraph(di.hdcDraw);

			if (bSelectOldRgn)
				SelectClipRgn(di.hdcDraw, hRgnOld);
			bLock = false;
			return S_OK;
		}

	OLE_COLOR m_clrBackColor;
	void OnBackColorChanged()
	{
		ATLTRACE(_T("OnBackColorChanged\n"));
		if(m_brBackColor)
			DeleteObject(m_brBackColor);
		m_brBackColor = CreateSolidBrush(m_clrBackColor);
		UpdateGraph();
	}
	OLE_COLOR m_clrBorderColor;
	void OnBorderColorChanged()
	{
		ATLTRACE(_T("OnBorderColorChanged\n"));
		if(m_brMarginColor)
			DeleteObject(m_brMarginColor);
		if(m_penBorder)
			DeleteObject(m_penBorder);
		m_brMarginColor = CreateSolidBrush(m_clrBorderColor);
		m_penBorder = CreatePen(PS_SOLID,1,m_clrBorderColor);
		UpdateGraph();
	}
	OLE_COLOR m_clrForeColor;
	void OnForeColorChanged()
	{
		ATLTRACE(_T("OnForeColorChanged\n"));
		if(m_penGraph)
			DeleteObject(m_penGraph);
		m_penGraph = CreatePen(PS_SOLID,1,m_clrForeColor);
		UpdateGraph();
	}

	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
		m_hWndParent = NULL;
		m_RgnClient = NULL;
		m_nDataLength = 0;
		m_rcPlot.left = 0;
		m_rcPlot.right = 512;
		m_rcLegend.left = m_rcLegend.right = 0;
		m_pxData = m_pyData = 0;
		m_dFactor = 1.0;
		m_bResample = true;
		m_nPlotType = 0;//Normal
		m_bGrid = true;
		//m_nReason = 1;
		m_bDirty = true;
		//m_clrBackColor = RGB(175,200,255);
		m_clrBackColor = RGB(0,0,0);
		m_clrBorderColor = RGB(200,200,200);
		m_clrSelection =  RGB(200,0,0);
		m_clrForeColor = RGB(0,255,0);
		m_nSelStart = m_nSelEnd = 0;
		m_strTitle = "Smart Graph";
		m_strxLable = "X";
		m_stryLable = "Y";
		m_strLegend = "";
		m_nBMargin = 30;
		m_nLMargin = 50;
		m_nTMargin = 30;
		m_nRMargin = 20;
		int nLen = 360*8;
		m_brMarginColor = CreateSolidBrush(m_clrBorderColor);
		m_brBackColor = CreateSolidBrush(m_clrBackColor);
		m_brSelectionColor = CreateSolidBrush(m_clrSelection);
		m_penGraph = CreatePen(PS_SOLID,1,m_clrForeColor);
		m_penBorder = CreatePen(PS_SOLID,1,m_clrBorderColor);

		double *xData = new double[nLen];
		double *yData= new double[nLen];
		memset(xData,0,nLen*sizeof(double));
		memset(yData,0,nLen*sizeof(double));
		int x = -1;
		for(int i = -nLen/2; i < nLen/2; i++)
		{
			x++;
			xData[x] = i*PI/180;
			//xData[x] = i;
			if(xData[x] == 0)
				yData[x] = 1;
			else
				yData[x] = sin(xData[x])/xData[x];
			//yData[x] = -xData[x]*xData[x];
		}
		m_cDataSet = new CGraphDataSet<double>;


		SetData(xData,yData,nLen, (VARIANT_BOOL)false);

		//SetData(xData,yData,nLen);
		CreateFonts();
		SAFE_DELETE(xData);
		SAFE_DELETE(yData);
		return S_OK;
	}
	
	void FinalRelease() 
	{
		delete m_cDataSet;
		DeleteObject(m_brBackColor);
		DeleteObject(m_brMarginColor);
		DeleteObject(m_brSelectionColor);
		DeleteObject(m_penBorder);
		DeleteObject(m_penGraph);
		DeleteObject(m_penGrids);
		DeleteObject(m_FontX);
		DeleteObject(m_FontY);
		DeleteObject(m_RgnClient);
	}
	int m_nDataLength;
	double *m_pxData, *m_pyData;
	struct ZoomParam{
		int nFrom,nTo,nLen;//Current Zoom Parameters
	};
	CAtlList<ZoomParam> m_lstZoom;

	double m_dFactor;//Resample Factor
	bool m_bResample;

	HWND m_hWndParent;
	HRGN m_RgnClient;
	int m_nSelStart,m_nSelEnd;//Start and End Of Selection in Graph
	int m_nPlotType;	// Type of plot 0,1,2-->Normal,dot,Bar
	int m_nRow;			// Number of rows in smart graph
	int m_nCol;			// Number of columns in smart graph
	int m_nBMargin;		// Margin for xLable
	int m_nLMargin;		// Margin for yLable
	int m_nTMargin;		// Margin for title
	int m_nRMargin;		// Right Margin
	LPCTSTR m_strTitle;		// Title
	LPCTSTR m_strxLable;	// xLable
	LPCTSTR m_stryLable;	// yLable
	LPCTSTR m_strLegend;	// Text in graph
	RECT m_rcPlot,		// Graph Area
		m_rcClient,		// Graph and all its lables area
		m_rcSelected,	//
		m_rcLegend;		// Legend text area
	HFONT m_FontX, m_FontY;
	HBRUSH m_brBackColor,m_brMarginColor,m_brSelectionColor;
	HPEN m_penGraph,m_penBorder,m_penGrids;
	bool m_bGrid;
	CGraphDataSet<double> *m_cDataSet;
	HBITMAP m_bmTemp;
	HBITMAP m_bmOld;
	HDC m_dcMem;
	bool m_bDirty;
	//int m_nReason;//Reason Of Being Dirty (Graph Resized, Selection Changed, ...)

	OLE_COLOR m_clrSelection;

	void CreateFonts(void);
	void UpdateGraph(HDC dc);
	int DrawGrids(HDC dc);
	int DrawLabes(HDC dc, RECT rc);
	int Zoom(void);
	void SetData(double* xData, double* yData, long NumOfPoints,
		double xMin = LONG_MAX, double xMax = LONG_MIN, 
		double yMin = LONG_MAX, double yMax = LONG_MIN);

	STDMETHOD(get_Columns)(LONG* pVal);
	STDMETHOD(put_Columns)(LONG newVal);
	STDMETHOD(get_Rows)(LONG* pVal);
	STDMETHOD(put_Rows)(LONG newVal);
	STDMETHOD(get_MarginBottom)(LONG* pVal);
	STDMETHOD(put_MarginBottom)(LONG newVal);
	STDMETHOD(get_MarginLeft)(LONG* pVal);
	STDMETHOD(put_MarginLeft)(LONG newVal);
	STDMETHOD(get_Title)(BSTR* pVal);
	STDMETHOD(put_Title)(BSTR newVal);
	STDMETHOD(get_xLable)(BSTR* pVal);
	STDMETHOD(put_xLable)(BSTR newVal);
	STDMETHOD(get_yLable)(BSTR* pVal);
	STDMETHOD(put_yLable)(BSTR newVal);
	STDMETHOD(get_MarginTop)(LONG* pVal);
	STDMETHOD(put_MarginTop)(LONG newVal);
	STDMETHOD(get_MarginRight)(LONG* pVal);
	STDMETHOD(put_MarginRight)(LONG newVal);
	STDMETHOD(SetData)(DOUBLE* xData, DOUBLE* yData, LONG NumOfPoints, VARIANT_BOOL bResample);
	STDMETHOD(SetData2)(DOUBLE* xData, DOUBLE* yData, LONG NumOfPoints, DOUBLE xMin, DOUBLE xMax, DOUBLE yMin, DOUBLE yMax, VARIANT_BOOL bResample);
	STDMETHOD(ShowGrid)(VARIANT_BOOL bShow);
	STDMETHOD(UpdateGraph)(void);
	STDMETHOD(SetPlotType)(LONG nType);
	LRESULT OnLButtonDown(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnMouseMove(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnLButtonUp(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	STDMETHOD(GetPlotRect)(LONG* left, LONG* top, LONG* right, LONG* bottom);
	STDMETHOD(GetClientRect)(LONG* left, LONG* top, LONG* right, LONG* bottom);
	STDMETHOD(ZoomIn)(LONG FromSample, LONG ToSample);
	STDMETHOD(ZoomInByPercent)(DOUBLE FromPercent, DOUBLE ToPercent);
	STDMETHOD(ZoomOut)(void);
	STDMETHOD(Reset)(void);
	STDMETHOD(GetDisplayedRange)(LONGLONG* nStartSample, LONGLONG* nEndSample);
	STDMETHOD(SetLegendText)(BSTR strText, LONG top, LONG left, LONG bottom, LONG right);
	STDMETHOD(SetParentWnd)(OLE_HANDLE hWnd);
};

⌨️ 快捷键说明

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