- Download executable file - 424 Kb
- Download demo project - 25.3 Kb
Introduction
I wrote this code a while ago to learn the basics of drawing in windows. I am more a Java guy than an MFC guy, so I wanted a control that was as easy to manipulate as the Java component Canvas.
The control extends CStatic
and handles the mouse events. I guess it is an easy demo for a starter. I also included in the demo files some useful code for bitmap manipulation.
Here is the header file:
#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
class CCanvas : public CStatic
{
public:
CCanvas();
public:
enum BORDER
{
SUNKEN,
LOW,
UP,
HIGH,
FLAT,
FRAME,
LIGHT_FRAME,
THIN_FRAME,
THIN_LIGHT_FRAME,
ETCHED,
ETCHED2,
SIMPLE
};
private:
CDC* m_buffer;
CBitmap* m_bitmap;
bool m_double_buffered;
bool m_auto_focus;
bool m_mouse_in;
bool m_draw_background;
bool m_draw_border;
COLORREF m_back_color;
BORDER m_border_style;
HCURSOR m_cursor;
int m_width, m_height;
protected:
virtual void PreSubclassWindow();
public:
bool IsMouseIn() { return m_mouse_in; }
void SetDoubleBuffer(bool flag) { m_double_buffered = flag; }
bool IsDoubleBuffered() { return m_double_buffered; }
void SetAutoFocus(bool flag) { m_auto_focus = flag; }
bool IsAutoFocus() { return m_auto_focus; }
void SetBackgroundColor(COLORREF back) { m_back_color = back; }
COLORREF GetBackgroundColor() { return m_back_color; }
void DrawBackground(bool flag) { m_draw_background = flag; }
bool DrawBackground() { return m_draw_background; }
void SetBorder(BORDER border_style) { m_border_style = border_style; }
BORDER GetBorder() { return m_border_style; }
void DrawBorder(bool flag) { m_draw_border = flag; }
bool DrawBorder() { return m_draw_border; }
void SetWinCursor(LPCTSTR cursor);
void SetAppCursor(UINT nIDResource);
void SetCursor(HCURSOR hCursor) { m_cursor = hCursor; }
int GetWidth() { return m_width; }
int GetHeight() { return m_height; }
private:
public:
virtual ~CCanvas();
virtual void Paint(CDC* dc) {}
virtual void DrawBorder(CDC* dc);
void Repaint();
virtual void OnMouseExit() {}
public:
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);
void DrawCircle(CDC *dc, int x, int y, int radius);
void DrawCircle(CDC *dc, int x, int y, int radius, COLORREF color);
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:
void Init();
void Clean();
protected:
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);
DECLARE_MESSAGE_MAP()
};
#endif // !defined(AFX_CANVAS_H__9AFA14C2_2DA3_48F1_9CC9_33B220B96494__INCLUDED_)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.