Home > Windows API > How to rotate text using WinAPI?

How to rotate text using WinAPI?

Simple way to rotate a piece of text is to use a font object… Here is a sample which does this…

void CSomeDlg::OnPaint()
{    
  CPaintDC dc(this);   

  // Get existing dlg font    
  CFont NewFont;     
  LOGFONT lgFnt = { 0 };     
  GetFont()->GetLogFont(&lgFnt);   

  // Modify this existing font // 

  // Face name is important, some font's like MS Sans Serif doesn't work
  strcpy( lgFnt.lfFaceName, "Arial" ); 

  // Give rotation as multiples of 10, so rotation will be 90 degrees
  lgFnt.lfOrientation = 900;
  lgFnt.lfEscapement =  900;  

  // Create a font from this log font     
  VERIFY( NewFont.CreateFontIndirect( &lgFnt ));  

  // Save this DC    
  const int DcRestorePoint = dc.SaveDC();      
  dc.SetBkMode( TRANSPARENT );   

  // Set this font to DC     
  dc.SelectObject( &NewFont );     
  dc.TextOut( 100,100, _T( "Nibu babu thomas loves Jesus Christ" ));   

  // Restore dc to previous state    
  dc.RestoreDC( DcRestorePoint );
}// End OnPaint

Not much of work ! 🙂

Advertisements
  1. June 24, 2008 at 3:25 am

    Sorry Rene!

    Hope I am not late replying to you. It’s quite easy to specify an angle, underline, size, bold, italic.

    These are font properties which can be specified while creating a font.

    Take a closer look at my sample function and you will understand.

    As a hint you can replace “900” (i.e. 90 degs * 10 ) with another angle * 10.

    I will generalize given sample for your purpose.

  2. June 16, 2008 at 8:13 pm

    Hello,

    I have a strange question: I am programming in Harbour together with MiniGui (a lib to create window programs). Now I want to print some rotated text in a window. I have a peace of code that does some of the things I want, but not all. It is a C function,I believe and I want to give the angle and the fontname, size, bold, italic, underline to this function. But I don’t know how. Here is the code I have at this moment:\

    HB_FUNC ( ROTATIONTEXT )
    {

    RECT rc;
    int angle;
    HFONT hfnt, hfntPrev;
    LPSTR lpszRotate = “The string to be rotated”;
    HRESULT hr;
    size_t pcch;
    HWND hwnd;
    HDC hdc;

    // Allocate memory for a LOGFONT structure.
    PLOGFONT plf = (PLOGFONT) LocalAlloc(LPTR, sizeof(LOGFONT));

    hdc = GetDC ( (HWND) hb_parnl(1) ) ;

    // Specify a font typeface name and weight.
    strncpy(plf->lfFaceName, “Arial”,6);
    plf->lfWeight = FW_NORMAL;

    // Retrieve the client-rectangle dimensions.
    GetClientRect((HWND) hb_parnl(1), &rc);

    // Set the background mode to transparent for the
    // text-output operation.
    SetBkMode(hdc, TRANSPARENT);

    // Draw the string 36 times, rotating 10 degrees
    // counter-clockwise each time.

    for (angle = 0; angle lfEscapement = angle;
    hfnt = CreateFontIndirect(plf);
    hfntPrev = SelectObject(hdc, hfnt);

    //
    // The StringCchLength call is fitted to the lpszRotate string
    //
    pcch = strlen(lpszRotate);

    TextOut(hdc, rc.right / 2, rc.bottom / 2,
    lpszRotate, pcch);
    SelectObject(hdc, hfntPrev);
    DeleteObject(hfnt);
    }

    // Reset the background mode to its default.
    SetBkMode(hdc, OPAQUE);

    // Free the memory allocated for the LOGFONT structure.
    LocalFree((LOCALHANDLE) plf);

    }

    #pragma ENDDUMP

    Can you help me ajust this function so I can use it the way I wants.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: