Click here to Skip to main content
15,897,891 members
Articles / Programming Languages / C#

Canvas implementation for C#

Rate me:
Please Sign up or sign in to vote.
4.43/5 (29 votes)
21 Mar 20044 min read 256.3K   9.6K   118  
Design and implementation of canvas in C#
#if !defined(AFX_CANVAS_H__9AFA14C2_2DA3_48F1_9CC9_33B220B96494__INCLUDED_)
#define AFX_CANVAS_H__9AFA14C2_2DA3_48F1_9CC9_33B220B96494__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// Canvas.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CCanvas window

class CCanvas : public CStatic
{
// Construction
public:
	CCanvas();

// Attributes
public:
	// Border style
	enum BORDER
	{
		SUNKEN,				// Sunken (CLIENT_EDGE)
		LOW,				// Low sunken (STATIC_EDGE)
		UP,					// Up (not too high)
		HIGH,				// High (MODAL_FRAME)
		FLAT,				// Flat (2 pixels large Border DKSHADOW)
		FRAME,				// Raised frame
		LIGHT_FRAME,		// Light raised frame
		THIN_FRAME,			// Thin raised frame
		THIN_LIGHT_FRAME,	// Thin light raised frame
		ETCHED,				// Etched
		ETCHED2,			// Etched 2
		SIMPLE				// Simple rectangle DKSHADOW
	};

private:
	// Buffer
	CDC* m_buffer;
	// Bitmap associated to m_buffer
	CBitmap* m_bitmap;

	// Double buffer / simple buffer
	bool m_double_buffered;
	// Auto focus
	bool m_auto_focus;
	// Mouse In
	bool m_mouse_in;
	// Draw the background
	bool m_draw_background;
	// Draw the border
	bool m_draw_border;

	// Background color
	COLORREF m_back_color;

	// Border Style
	BORDER m_border_style;

	// Cursor
	HCURSOR m_cursor;

	// Width and Height
	int m_width, m_height;

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CCanvas)
	protected:
	virtual void PreSubclassWindow();
	//}}AFX_VIRTUAL

// Operations
public:
	// Return true if the mouse is in the client area
	bool IsMouseIn() { return m_mouse_in; }

	// Set double buffer
	void SetDoubleBuffer(bool flag) { m_double_buffered = flag; } 
	// Check if double buffered
	bool IsDoubleBuffered() { return m_double_buffered; }

	// Set auto focus
	void SetAutoFocus(bool flag) { m_auto_focus = flag; } 
	// Check if auto focus
	bool IsAutoFocus() { return m_auto_focus; }

	// Set the background color
	void SetBackgroundColor(COLORREF back) { m_back_color = back; }
	// Get the background color
	COLORREF GetBackgroundColor() { return m_back_color; }

	// Set background to be displayed
	void DrawBackground(bool flag) { m_draw_background = flag; }
	// Check if the background is displayed
	bool DrawBackground() { return m_draw_background; }

	// Set the border style
	void SetBorder(BORDER border_style) { m_border_style = border_style; }
	// Get the border Style
	BORDER GetBorder() { return m_border_style; }

	// Draw the border
	void DrawBorder(bool flag) { m_draw_border = flag; }
	// Check if the border is displayed
	bool DrawBorder() { return m_draw_border; }

	// Set the cursor (Win32 cursor)
	void SetWinCursor(LPCTSTR cursor);
	// Set application cursor from ID resource
	void SetAppCursor(UINT nIDResource);
	// Set custom cursor
	void SetCursor(HCURSOR hCursor) { m_cursor = hCursor; }

	// Get the width
	int GetWidth() { return m_width; }
	// Get the height
	int GetHeight() { return m_height; }

private:

// Implementation
public:
	virtual ~CCanvas();

	// Main Paint method
	virtual void Paint(CDC* dc) {}
	
	// Draw border
	virtual void DrawBorder(CDC* dc);

	// Repaint
	void Repaint();
	
	// OnMouseExit: Called each time the mouse exit the client area
	virtual void OnMouseExit() {}

// Helpers
public:
	// Draw a rectangle
	void DrawRect(CDC *dc, RECT rect);
	void DrawRect(CDC *dc, int x, int y, int w, int h);
	void DrawRect(CDC *dc, RECT rect, COLORREF color);
	void DrawRect(CDC *dc, int x, int y, int w, int h, COLORREF color);

	// Draw a circle
	void DrawCircle(CDC *dc, int x, int y, int radius);
	void DrawCircle(CDC *dc, int x, int y, int radius, COLORREF color);

	// Draw a line
	void DrawLine(CDC *dc, CPoint& start, CPoint& end);
	void DrawLine(CDC *dc, int xStart, int yStart, int xEnd, int yEnd);
	void DrawLine(CDC *dc, CPoint& start, CPoint& end, COLORREF color);
	void DrawLine(CDC *dc, int xStart, int yStart, int xEnd, int yEnd, COLORREF color);

private:
	// Initialize the buffer
	void Init();

	// Clean the buffer
	void Clean();

protected:
	// Generated message map functions
	//{{AFX_MSG(CCanvas)
	afx_msg UINT OnNcHitTest(CPoint point);
	afx_msg void OnPaint();
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
	afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
	afx_msg void OnTimer(UINT nIDEvent);
	afx_msg BOOL OnEraseBkgnd(CDC* pDC);
	//}}AFX_MSG

	DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_CANVAS_H__9AFA14C2_2DA3_48F1_9CC9_33B220B96494__INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Canada Canada
Baranovsky Eduard has been a software developer for more then 10 years. He has an experence in image processing, computer graphics and distributed systems design.

Comments and Discussions