Click here to Skip to main content
15,891,033 members
Articles / Desktop Programming / MFC

Simple Text Rotation

Rate me:
Please Sign up or sign in to vote.
4.27/5 (15 votes)
26 Sep 2000 137.6K   27   14
A simple function to rotate text around its center point within a rectangle

Introduction

I am working on a kind of CAD-Software and needed a function that was able to rotate text around its center inside a rectangle. Because Windows only provides text rotation around the left-bottom corner of the specified text, I had a problem. I was searching through websites for this piece of code, but I didn't find anything. So I tried it on my own, and this is the code I am using in my program.

First, create a CFont with the angle of rotation specified in nEscapement. Let your DC select this font and call the following function:

C++
#include <cmath>
 
// pDC : pointer to your device-context
// str : the text
// rect: the rectangle
// nOptions: can be a combination of ETO_CLIPPED and ETO_OPAQUE
// (see documentation of ExtTextOut for more details)
void DrawRotatedText(CDC* pDC, const CString str, CRect rect, 
                     double angle, UINT nOptions = 0)
{
   // convert angle to radian
   double pi = 3.141592654;
   double radian = pi * 2 / 360 * angle;
 
   // get the center of a not-rotated text
   CSize TextSize = pDC->GetTextExtent(str);
   CPoint center;
   center.x = TextSize.cx / 2;
   center.y = TextSize.cy / 2;
 
   // now calculate the center of the rotated text
   CPoint rcenter;
   rcenter.x = long(cos(radian) * center.x - sin(radian) * center.y);
   rcenter.y = long(sin(radian) * center.x + cos(radian) * center.y);
 
   // finally draw the text and move it to the center of the rectangle
   pDC->SetTextAlign(TA_BASELINE);
   pDC->SetBkMode(TRANSPARENT);
   pDC->ExtTextOut(rect.left + rect.Width() / 2 - rcenter.x, 
                   rect.top + rect.Height() / 2 + rcenter.y,
                   nOptions, rect, str, NULL);
}

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
Software Developer (Senior) iucon GmbH
Germany Germany
My name is Stefan and I am working at iucon GmbH (http://www.iucon.de), a german software development company.
Our main focus is the development of online shops including the ERP backend.
We also develop custom software solutions for our customers and are able to support other companies and developers in using the following technologies, because we use them every day Wink | ;-)

- .NET Framework 2.0
- .NET Framework 3.0
- .NET Framework 3.5
- Windows Workflow Foundation
- C#
- ASP.NET
- ASP.NET AJAX
- SQL Server 2005 T-SQL
- ADO .NET
- Reporting Services
- Integration Services
- XML Webservices
- Database Design
- Payment systems

You can find information about our ERP software iuBIZ on http://www.iubiz.de which is totally written in .NET by using most of the cool new technologies like Workflow Foundation, Reporting Services, ClickOnce-Deployment. Feel free to take a look at the website and see Microsoft's new technologies in action Wink | ;-)

Comments and Discussions

 
QuestionExecuting same code in c# ? Pin
Rizwan Ahmed But2-Apr-07 21:20
Rizwan Ahmed But2-Apr-07 21:20 
GeneralRestoring previous alignment Pin
agi20dla10-Mar-07 8:17
agi20dla10-Mar-07 8:17 
Questionhow to Fit text into the pattern Pin
ngoctt_hut8-Jan-07 14:48
ngoctt_hut8-Jan-07 14:48 
Questiontext doesn't rotate? Pin
thready13-Oct-06 4:25
thready13-Oct-06 4:25 
AnswerRe: text doesn't rotate? Pin
FioreProject19-Jul-22 5:00
FioreProject19-Jul-22 5:00 
GeneralGDI Text Rotation Pin
anshul_makkar15-Jan-05 0:24
anshul_makkar15-Jan-05 0:24 
GeneralThanks Pin
sanskypotov21-Sep-04 4:38
sanskypotov21-Sep-04 4:38 
GeneralText Control Rotation Pin
rawatvs26-Nov-03 5:27
rawatvs26-Nov-03 5:27 
GeneralDoesn't work on Windows 95/98 Pin
Paul Vickery6-Feb-03 2:47
professionalPaul Vickery6-Feb-03 2:47 
GeneralRe: Doesn't work on Windows 95/98 Pin
Paul Vickery6-Feb-03 3:50
professionalPaul Vickery6-Feb-03 3:50 
GeneralRe: Doesn't work on Windows 95/98 Pin
Ed Deighton10-Dec-03 6:30
Ed Deighton10-Dec-03 6:30 
GeneralRe: Doesn't work on Windows 95/98 Pin
Paul Vickery10-Dec-03 22:43
professionalPaul Vickery10-Dec-03 22:43 
GeneralHere is the SDK version: Pin
nilaysoft18-Jan-03 18:06
nilaysoft18-Jan-03 18:06 
void DrawRotatedText(HDC hdc, char *str, LPRECT rect,
                     double angle, UINT nOptions = 0)
{
   // convert angle to radian
   double pi = 3.141592654;
   double radian = pi * 2 / 360 * angle;

   // get the center of a not-rotated text
   SIZE TextSize;;
   GetTextExtentPoint32(hdc, str, strlen(str), &TextSize);

   POINT center;
   center.x = TextSize.cx / 2;
   center.y = TextSize.cy / 2;

   // now calculate the center of the rotated text
   POINT rcenter;
   rcenter.x = long(cos(radian) * center.x - sin(radian) * center.y);
   rcenter.y = long(sin(radian) * center.x + cos(radian) * center.y);

   // finally draw the text and move it to the center of the rectangle
   SetTextAlign(hdc, TA_BASELINE);
   SetBkMode(hdc, TRANSPARENT);
   ExtTextOut(hdc, rect->left + (rect->right - rect->left) / 2 - rcenter.x,
                   rect->top + (rect->bottom - rect->top) / 2 + rcenter.y,
                   nOptions, rect, str, strlen(str), NULL);
}

GeneralAlignment TA_BOTTOM vs. TA_BASELINE PinPopular
Yury Goltsman27-Sep-00 23:37
Yury Goltsman27-Sep-00 23:37 

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.