65.9K
CodeProject is changing. Read more.
Home

Double Buffered Canvas

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.55/5 (5 votes)

Jan 25, 2001

viewsIcon

95115

downloadIcon

2728

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_)