Discussion:
Rotating text in a CDC
(too old to reply)
Alvaro Palma
2004-09-30 17:05:17 UTC
Permalink
Is it possible to rotate text in a simple way inside a CDC object?
I added the next code in OnPaint for a CWnd object, but the text
still appears "normal". I tried setting the lfEscapament and lfOrientation
to 1, 9, 90, etc (both simultaneously), but nothing worked.

void CMyWnd::OnPaint()
{
CPaintDC dc(this); // device context for painting
....
CFont *pOldFont,*pCurFont,*pNewFont;
pNewFont = new CFont();

// Load the present font from the DC
pCurFont = dc.GetCurrentFont();
// Just change the orientation
LOGFONT logFont;
pCurFont->GetLogFont(&logFont);
logFont.lfEscapement = 90; // Rotate it 90 degrees
logFont.lfOrientation = 90;

// Create a new font using the current font properties, except the changed
orientation
BOOL b = pNewFont->CreateFontIndirect(&logFont);

// Put this new font
pOldFont = dc.SelectObject(pNewFont);
CString szText = "Text Rotated in 90 degrees";
dc.TextOut(100,100,szText);

// Restore the original font
dc.SelectObject(pOldFont);
delete pNewFont;
}

Thanks a lot for your attention
Leonardo Lima
2004-09-30 18:21:03 UTC
Permalink
You have two problems,
the Rotate angle must be times 10,
and change the font, because with the font default ( "System"), nothing
happens.

LOGFONT logFont;
pCurFont->GetLogFont(&logFont);
logFont.lfEscapement = 900; // Rotate it 90 degrees
logFont.lfOrientation = 900;
::lstrcpy ( logFont.lfFaceName, _T ("Arial"));

// Create a new font using the current font properties, except the changed
//orientation
BOOL b = pNewFont->CreateFontIndirect(&logFont);

// Put this new font
pOldFont = dc.SelectObject(pNewFont);
CString szText = "Text Rotated in 90 degrees";
dc.TextOut(100,100,szText);

Another example:

void CMainWindow::OnPaint ()
{
CRect rect;
GetClientRect (&rect);

CPaintDC dc (this);
dc.SetViewportOrg (rect.Width () / 2, rect.Height () / 2);
dc.SetBkMode (TRANSPARENT);

for (int i=0; i<3600; i+=150) {
LOGFONT lf;
::ZeroMemory (&lf, sizeof (lf));
lf.lfHeight = 160;
lf.lfWeight = FW_BOLD;
lf.lfEscapement = i;
lf.lfOrientation = i;
::lstrcpy (lf.lfFaceName, _T ("Arial"));

CFont font;
font.CreatePointFontIndirect (&lf);

CFont* pOldFont = dc.SelectObject (&font);
dc.TextOut (0, 0, CString (_T (" Hello, MFC")));
dc.SelectObject (pOldFont);
}
}
Post by Alvaro Palma
Is it possible to rotate text in a simple way inside a CDC object?
I added the next code in OnPaint for a CWnd object, but the text
still appears "normal". I tried setting the lfEscapament and lfOrientation
to 1, 9, 90, etc (both simultaneously), but nothing worked.
void CMyWnd::OnPaint()
{
CPaintDC dc(this); // device context for painting
.....
CFont *pOldFont,*pCurFont,*pNewFont;
pNewFont = new CFont();
// Load the present font from the DC
pCurFont = dc.GetCurrentFont();
// Just change the orientation
LOGFONT logFont;
pCurFont->GetLogFont(&logFont);
logFont.lfEscapement = 90; // Rotate it 90 degrees
logFont.lfOrientation = 90;
// Create a new font using the current font properties, except the changed
orientation
BOOL b = pNewFont->CreateFontIndirect(&logFont);
// Put this new font
pOldFont = dc.SelectObject(pNewFont);
CString szText = "Text Rotated in 90 degrees";
dc.TextOut(100,100,szText);
// Restore the original font
dc.SelectObject(pOldFont);
delete pNewFont;
}
Thanks a lot for your attention
Continue reading on narkive:
Loading...