2

I am using the windows GDI API ExtTextOut function to draw text like this:

ExtTextOut(hDC, 2000, 2000, 0, &stRect, PrintText, TextOutLen, aiCharCellDistances);

I am trying to rotate the text, and I do rotate the text. But when I fill the rectangle with colors, I found that the rectangle didn't rotate with the text.

Is there any way to rotate the rectangle with the text? Or is there a better way to do this?

P.S.: My goal is to draw text in the rectangle (like text area) and can rotate it in any angle, and set background color, border line, line break, align right, etc.

Thanks!

nate
  • 1,771
  • 12
  • 17
Chao Chan
  • 23
  • 1
  • 5
  • Do you have to use GDI? it is a very old API with serious limitations. If you can use something more modern, Direct2D is very powerful, if you need to support older version of windows GDI+ has been available for some time. – nate Aug 04 '16 at 03:32
  • Actually I am now learning Direct2D , I just hope there is a way can answer my question using GDI . Thank you for your advice ! – Chao Chan Aug 04 '16 at 03:45
  • I will preface this comment by saying that I know practically nothing about GDI, I was just looking around :-) However: seemingly related (although not a duplicate): [Rotate Text by 90 degrees with GDI+](http://stackoverflow.com/a/3584749/4520911). At the same time, this is also partially unrelated since there are [differences between GDI and GDI+](http://stackoverflow.com/a/4551237/4520911). I think it would be helpful for you to include the code you used here: _"But when I fill the rectangle with colors, I found that the rectangle didn't rotate with the text."_ – iRove Aug 04 '16 at 04:33
  • @iRove Hans's answer there is actually written in GDI, not GDI+. :-) – Cody Gray - on strike Aug 04 '16 at 12:51
  • @CodyGray Well, there is a reason I accurately said _"I know practically nothing about GDI."_ Good thing I have an excuse now. ;-) – iRove Aug 04 '16 at 16:22
  • @nate, how did you get to the conclusion GDI is being used here? and what's GDI? please, edit the question and include some context, as Chao Chan didn't. – Luis Colorado Aug 05 '16 at 07:04
  • @LuisColorado I recognized the `ExtTextOut` API. – nate Aug 05 '16 at 13:54

1 Answers1

7

It's not 100% clear what you want, but I think you want to draw some text and rectangle rotated at the same angle? If so, it's probably easiest to use SetWorldTransform to do the job.

Here's some code doing it with MFC:

double factor = (2.0f * 3.1416f)/360.0f;
double rot = 45.0f * factor;

// Create a matrix for the transform we want (read the docs for details)
XFORM xfm = { 0.0f };
xfm.eM11 = (float)cos(rot);
xfm.eM12 = (float)sin(rot);
xfm.eM21 = (float)-sin(rot);
xfm.eM22 = (float)cos(rot);

pDC->SetGraphicsMode(GM_ADVANCED);
pDC->SetWorldTransform(&xfm);    // Tell Windows to use that transform matrix

pDC->SetBkMode(TRANSPARENT);
CRect rect{ 290, 190, 450, 230 };
CBrush red;
red.CreateSolidBrush(RGB(255, 0, 0));

pDC->FillRect(rect, &red); // Draw a red rectangle behind the text

pDC->TextOut(300, 200, L"This is a string"); // And draw the text at the same angle

For the most part, doing this without MFC just means changing pDC->foo(args) to foo(dc, args).

The result looks like this:

enter image description here

Note that in this case, you do not need to specify rotation (at all--either lfRotation or lfEscapement) for the font you use. You just draw like it was normal text, and the world transform handles all the rotation.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • The result is exactly what I want , Thank you for you answer , I'm now trying to use every kind of solution and choose the best one , Thanks again . – Chao Chan Aug 04 '16 at 06:20
  • I have been quite happy with Direct2D, though it requires at least windows 7. – nate Aug 05 '16 at 13:57