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
- Solves flickering problems
- Ability to rotate text, drawings, and images
- Clips anything outside of its bounds
- 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);
CDC * pmemDC = memCanvas.GetDC();
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");
memCanvas.Rotate(30);
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!