Click to See Complete Forum and Search --> : text above any line


phm
July 13th, 2005, 03:22 PM
Hi

Does anybody know how can I draw a text above any arbitrary line?
I have only line coordinates and its angle and I want to draw the text centered in the middle of the line and above it.

Thanks.

Smasher/Devourer
July 13th, 2005, 04:28 PM
You'll have to be much clearer as to what you're doing. For instance, what are you using to render text? Windows API functions? And when you say that you have the line's "angle," do you mean that you want to draw text at the same slope as the line?

olivthill
July 13th, 2005, 04:51 PM
In order to write some text with an angle, you take advantage of the "nEscapement" argument in the "CreateFont" API (don't use the "nOrientation" argument). Here is an example:

HFONT hfont, hfont_old;
char buf[200]; int angle;

strcpy(buf, "This is a test");

// Draw line with a slope of 45 degrees
MoveToEx(hdc, 10, 20, NULL);
LineTo(hdc, 110, 120);

// Create a font with letters having an angle
angle = 3150; // (360 minus 45 degrees) * 10
hfont = CreateFont(12, 0, angle, 0, FW_NORMAL,
0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_DONTCARE, "Arial");
hfont_old = (HFONT) SelectObject(hdc, hfont);

// Draw Text above the line
SetTextAlign(hdc, TA_CENTER | TA_BOTTOM); // The reference point will be aligned horizontally with the center and the bottom of the bounding rectangle
TextOut(hdc, (110 - 10) / 2 + 10, (120 - 20) / 2 + 20, buf, strlen(buf)); // x and y are chosen to be on the middle of the line

// Remove the font
SelectObject(hdc, hfont_old);
DeleteObject(hfont);

phm
July 14th, 2005, 05:40 PM
Thanks a lot.
It helped me much.