The Class is used by simply adding a CStatic Control with the class CGraphCtrl to a dialog.
public: CGraphCtrl Movingpixelplot;
#pragma once #include "afxwin.h" #include "memdc.h" #include </vector><vector> using namespace std; class CGraphCtrl : public CStatic { // Construction public: CGraphCtrl(void); CGraphCtrl(const int x, const int y); // Destruction public: ~CGraphCtrl(void); // Attributes public: // Operations public: // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CGraphCtrl) public: //virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL); protected: virtual BOOL OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult); virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam); //virtual BOOL PreCreateWindow(CREATESTRUCT& cs); //}}AFX_VIRTUAL // Implementation public: int m_PixelNumberX; int m_PixelNumberY; vector</vector><vector <COLORREF> > PlotData; void Resize(const unsigned short width, const unsigned short height); void FullResize(const CRect newRect); void SetSize(const unsigned short width, const unsigned short height); void SetPlotDataSize(const int x, const int y); CRect GetSize(); CRect GetPlotDataSize(); void SetPlotData(vector</vector><vector <COLORREF> > InputArray); void UpdatePlot(); void PaintPixel(CMemDC* pDC, const int x, const int y, COLORREF c); void PlotToDC(CMemDC* pDC); // Generated message map functions protected: //{{AFX_MSG(CGraphCtrl) afx_msg BOOL OnEraseBkgnd(CDC* pDC); afx_msg void OnPaint(); //}}AFX_MSG DECLARE_MESSAGE_MAP() public: };
#include "stdafx.h" #include "GraphCtrl.h" CGraphCtrl::CGraphCtrl(void) { // Set Number of Datamember to 0 m_PixelNumberX=0; m_PixelNumberY=0; } CGraphCtrl::CGraphCtrl(const int x, const int y) { SetPlotDataSize(x,y); } CGraphCtrl::~CGraphCtrl(void) { } BEGIN_MESSAGE_MAP(CGraphCtrl, CStatic) //{{AFX_MSG_MAP(CGraphCtrl) ON_WM_ERASEBKGND() ON_WM_PAINT() ON_WM_CREATE() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CGraphCtrl message handlers BOOL CGraphCtrl::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) { // TODO: Add your specialized code here and/or call the base class return CStatic::OnNotify(wParam, lParam, pResult); } LRESULT CGraphCtrl::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) { // TODO: Add your specialized code here and/or call the base class return CStatic::WindowProc(message, wParam, lParam); } BOOL CGraphCtrl::OnEraseBkgnd(CDC* pDC) { return FALSE; } void CGraphCtrl::OnPaint() { CPaintDC dc(this); // device context for painting CRect (rc_dest); rc_dest=GetSize(); CRect (rc_source); rc_source=GetPlotDataSize(); CMemDC pDC(&dc,&rc_source, &rc_dest); // Double Buffering PlotToDC(& pDC); } void CGraphCtrl::PlotToDC(CMemDC* pDC) { COLORREF c; if ((m_PixelNumberX>0) && (m_PixelNumberY > 0)) { for (int ix=0; ix < m_PixelNumberX; ix++) { for (int iy=0; iy < m_PixelNumberY; iy++) { c=PlotData[ix][iy]; pDC->SetPixel(ix,iy,c); } } } } void CGraphCtrl::Resize(const unsigned short width, const unsigned short height) { SetWindowPos(NULL, 0, 0, width, height, SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER); } void CGraphCtrl::FullResize(const CRect newRect) { SetWindowPos(NULL, newRect.left, newRect.top, newRect.right, newRect.bottom, SWP_NOACTIVATE | SWP_NOZORDER); } void CGraphCtrl::SetSize(const unsigned short width, const unsigned short height) { Resize(width-1, height-1); } CRect CGraphCtrl::GetSize() { CRect rc; GetClientRect(rc); return rc; } CRect CGraphCtrl::GetPlotDataSize() { CRect rc; rc.left=0; rc.top=0; rc.right=m_PixelNumberX; rc.bottom=m_PixelNumberY; return rc; } void CGraphCtrl::SetPlotDataSize(const int x, const int y) { m_PixelNumberX = x; m_PixelNumberY = y; if ((x>=0) && (y>=0)) { PlotData.resize(m_PixelNumberX); for(int x = 0; x < m_PixelNumberX; x++) PlotData[x].resize(m_PixelNumberY); } } void CGraphCtrl::SetPlotData(vector<vector<COLORREF> > InputArray) { if ((m_PixelNumberX>0) && (m_PixelNumberY > 0)) { PlotData = InputArray; } } void CGraphCtrl::UpdatePlot() { Invalidate(); UpdateWindow(); }