Click here to Skip to main content
15,867,568 members
Articles / Desktop Programming / MFC
Article

Double Buffered Canvas

Rate me:
Please Sign up or sign in to vote.
3.55/5 (5 votes)
24 Jan 2001 94.5K   2.7K   22   5
Java style canvas component, double buffer and easy mouse support.
  • Download executable file - 424 Kb
  • Download demo project - 25.3 Kb
  • Sample Image - canvas.jpg

    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
    // 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}}
    
    #endif // !defined(AFX_CANVAS_H__9AFA14C2_2DA3_48F1_9CC9_33B220B96494__INCLUDED_)

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
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralResource leak Pin
28-Feb-01 0:52
suss28-Feb-01 0:52 
GeneralMemory Leak..... Pin
18-Feb-01 6:04
suss18-Feb-01 6:04 
GeneralRe: Memory Leak..... Pin
Nicolas Pabion23-Feb-01 11:21
Nicolas Pabion23-Feb-01 11:21 
GeneralRe: Memory Leak..... Pin
AlexMarbus27-Feb-01 7:19
AlexMarbus27-Feb-01 7:19 
void* pSomePointer = new void;

#ifdef _DEBUG
delete pSomePointer;
#endif

just kidding ofcourse. I often use BoundsChecker to detect memory-/resourceleaks and all other kind of errors that I've may forgotten about. It's a great help finding these kind of things.

--
Alex Marbus
GeneralRe: Memory Leak..... Pin
Nicolas Pabion27-Feb-01 8:43
Nicolas Pabion27-Feb-01 8:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.