CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2005
    Posts
    44

    text above any line

    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.

  2. #2
    Join Date
    May 2005
    Location
    United States
    Posts
    526

    Re: text above any line

    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?

  3. #3
    Join Date
    Jun 2005
    Posts
    1,255

    Re: text above any line

    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:
    Code:
    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);

  4. #4
    Join Date
    Jul 2005
    Posts
    44

    Thumbs up Re: text above any line

    Thanks a lot.
    It helped me much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured