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

Buffered Canvas for Memory DC with Rotation, Clipping, Transparency

Rate me:
Please Sign up or sign in to vote.
4.77/5 (12 votes)
3 Jun 20031 min read 81.9K   3.2K   54   11
Code for buffering and rotating your drawing context.

Sample Image - demo1.jpg

Introduction

I needed to rotate some drawing that I am doing in a program, but there is no rotate method in the DC for a window. So I wrote a canvas class that could do rotation and solve other problems too!

The buffer canvas class will store all your DC operations and do one copy to the screen.

Advantages

  1. Solves flickering problems
  2. Ability to rotate text, drawings, and images
  3. Clips anything outside of its bounds
  4. Transparency mode for replacing background color with other window contents

Using the code

The class is called CBuffCanvas and has very few function calls to remember:

Constructor

CBuffCanvas(CWnd *pWnd, CRect bounds, COLORREF bkColor, bool isTransparent);

Requires a pointer to the window, the bounding rectangle, a background color, and whether to be transparent

Drawing context

CDC* GetDC();

Returns the DC to the buffered canvas. Use this DC to do all of your drawing operations.

Rotation

void Rotate(int degrees, CPoint origin); void Rotate(int degrees);

Rotates the drawing counter-clockwise from the origin (optional).

Copy to screen

void Draw(CDC* screen);

Takes a pointer to a drawing context and copies the buffered context over to the screen.

CBuffCanvas memCanvas(this, CRect(50,50,350,350), RGB(100,100,100), true);

// Get the DC from the buffer for drawing
CDC * pmemDC = memCanvas.GetDC();

// Do some drawing and text
CPen bluePen(PS_SOLID, 1, RGB(0,0,255));
pmemDC->SelectObject(bluePen);
pmemDC->MoveTo(120,120);
pmemDC->LineTo(120,250);
pmemDC->TextOut(130,150, "Buffer Canvas Demo");

// Rotate the canvas 30 degrees counter-clockwise
memCanvas.Rotate(30);

// Call the draw function to copy the buffered canvas to the screen
memCanvas.Draw(pDC);

Note that there may be performance issues to consider, especially when using the transparent mode. Don't try to draw large areas if you don't want flickering. Also, try doing the rotation in another part of your program than the OnPaint function.

Points of interest

I got the transparent draw down using BitBlt's. Also the bit by bit rotation go by trigonometry back up to par.

Conclusion

This is my first go at it, so I hope that it works for everyone!

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
United States United States
I am currently working for the U.S. Peace Corps in the Kingdom of Tonga. I teach C++ at the Tongan Insitute of Higher Education. In my spare time, I go kayaking and snorkeling. Tonga is a very out of the way, non-tourist island in the South Pacific. If you need to just get away, I highly recommend you come here.
As for programming, I am currently working on MFC applications. I am always interested in more work if you like what I've done. Enjoy!

Kevin

Comments and Discussions

 
GeneralMy vote of 5 Pin
Homero De la Garza15-Jul-12 9:52
Homero De la Garza15-Jul-12 9:52 
GeneralA bit slow, but good work... Pin
Naren Neelamegam22-Dec-08 23:03
Naren Neelamegam22-Dec-08 23:03 
GeneralWhy make it all by yourself.. Pin
Soren_mul6-Jun-03 3:29
Soren_mul6-Jun-03 3:29 
GeneralRe: Why make it all by yourself.. Pin
Kevin Spaeth18-Jun-03 16:54
Kevin Spaeth18-Jun-03 16:54 
Generalmem leak Pin
csc6-Jun-03 2:25
csc6-Jun-03 2:25 
GeneralRe: mem leak Pin
Kevin Spaeth18-Jun-03 16:56
Kevin Spaeth18-Jun-03 16:56 
GeneralBuffered Canvas for Memory DC with Rotation, Clipping, Transparency Pin
shanila6-Jun-03 0:23
shanila6-Jun-03 0:23 
GeneralRe: Buffered Canvas for Memory DC with Rotation, Clipping, Transparency Pin
Kevin Spaeth18-Jun-03 16:56
Kevin Spaeth18-Jun-03 16:56 
Generala bit slowly Pin
billhao5-Jun-03 17:04
billhao5-Jun-03 17:04 
when I hide the window, then show it, the repaint operation is too slowly. I think the problem is the calculation of rotation. An improvement of this part will make it perfectSmile | :)

--------------------------------------
e-mail: billhao@hotmail.com
MSN Messenger: billhao@hotmail.com
http://www.minivoice.com
GeneralRe: a bit slowly Pin
Kevin Spaeth18-Jun-03 16:55
Kevin Spaeth18-Jun-03 16:55 
GeneralRe: a bit slowly Pin
billhao18-Jun-03 19:06
billhao18-Jun-03 19:06 

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.