Click here to Skip to main content
13,045,790 members (48,487 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 24 Jan 2001

Double Buffered Canvas

, 24 Jan 2001
Rate this:
Please Sign up or sign in to vote.
Java style canvas component, double buffer and easy mouse support.
<!-- Link to source file download -->
  • Download executable file - 424 Kb
  • <!-- Link to demo file download -->
  • Download demo project - 25.3 Kb
  • <!-- Article image -->

    Sample Image - canvas.jpg

    <!-- Add the rest of your HTML here -->


    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
    // Attributes
        // 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
        // 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
        virtual void PreSubclassWindow();
    // Operations
        // 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; }
    // Implementation
        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
        // 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);
        // Initialize the buffer
        void Init();
        // Clean the buffer
        void Clean();
        // Generated message map functions
        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);
    #endif // !defined(AFX_CANVAS_H__9AFA14C2_2DA3_48F1_9CC9_33B220B96494__INCLUDED_)


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


About the Author

Nicolas Pabion
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralResource leak Pin
Anonymous28-Feb-01 0:52
memberAnonymous28-Feb-01 0:52 
GeneralMemory Leak..... Pin
Anonymous18-Feb-01 6:04
memberAnonymous18-Feb-01 6:04 
GeneralRe: Memory Leak..... Pin
Nicolas Pabion23-Feb-01 11:21
memberNicolas Pabion23-Feb-01 11:21 
GeneralRe: Memory Leak..... Pin
AlexMarbus27-Feb-01 7:19
memberAlexMarbus27-Feb-01 7:19 
GeneralRe: Memory Leak..... Pin
Nicolas Pabion27-Feb-01 8:43
memberNicolas 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 25 Jan 2001
Article Copyright 2001 by Nicolas Pabion
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid