bool getTextExtentPoint(TUChar* pszText, int nLen, TSize& tSize) { bool bRet = false; do { CC_BREAK_IF(! pszText); if (tSize.Width() > 0 && tSize.Height() > 0) { bRet = true; break; } int nLineCount = 0; int nWinWidth = 0; stdTUString FullString = pszText; if (tSize.Width() == 0) { // not specified the width,calculate the width and line count stdTUString strLeft = FullString; stdTUString strLine; while (strLeft.length() > 0) { int nPos = strLeft.find('\n'); // get one line text if (nPos != stdTUString::npos) { strLine = strLeft.substr(0, nPos); strLeft = strLeft.substr(nPos + 1); } else { strLine = strLeft; strLeft.erase(); } // calculate the width of current line and update the window width int nTempWidth = m_hFont.CharsWidth(strLine.c_str(), strLine.length() + 1); if (nTempWidth >= nWinWidth) { nWinWidth = nTempWidth; } // update the line count ++nLineCount; } } else { // have specified the window width,calculate the line count nWinWidth = tSize.Width(); stdTUString strLeft = FullString; int nCurPos = 0; do { nCurPos = m_hFont.WordWrap(strLeft.c_str(), nWinWidth); strLeft = strLeft.substr(nCurPos); ++nLineCount; } while (strLeft.length() > 0); } // calculate the window height. tSize.SetHeight(nLineCount * m_hFont.LineHeight()); tSize.SetWidth(nWinWidth); bRet = true; } while (0); return bRet; }
int drawText(TUChar* pszText, int nLen, TSize& tSize, UInt32 style) { int nRet = 0; do { CC_BREAK_IF(! pszText); if (tSize.Width() <= 0 || tSize.Height() <= 0) { CC_BREAK_IF(! getTextExtentPoint(pszText, nLen, tSize)); } CC_BREAK_IF(tSize.Width() <= 0 || tSize.Height() <= 0); CC_BREAK_IF(! prepareBitmap(tSize.Width(), tSize.Height())); Int32 nWidth = tSize.Width(); Int32 nHeight = tSize.Height(); // create memory window if (m_pMemWnd) { TRectangle rcMemWnd(0, 0, 0, 0); m_pMemWnd->GetClientBounds(&rcMemWnd); if (rcMemWnd.Width() < nWidth || rcMemWnd.Height() < nHeight) { m_pMemWnd->CloseWindow(); m_pMemWnd = NULL; } } do { // if memory window is already break CC_BREAK_IF(m_pMemWnd); TApplication* pApp = TApplication::GetCurrentApplication(); CC_BREAK_IF(! pApp || ! (m_pMemWnd = new TWindow(pApp))); Coord nCurrentWidth = pApp->GetScreenWidth(); Coord nCurrentHeight = pApp->GetScreenHeight(); Coord nMemWndW = (nWidth >= nCurrentWidth) ? nWidth : nCurrentWidth; Coord nMemWndH = (nHeight >= nCurrentHeight) ? nHeight : nCurrentHeight; CC_BREAK_IF(m_pMemWnd->CreateMemWindow(nMemWndW, nMemWndH,screenAlphaFormat)); delete m_pMemWnd; m_pMemWnd = NULL; } while (0); CC_BREAK_IF(! m_pMemWnd); // create DC TDC dc(m_pMemWnd); // draw text m_pMemWnd->GetMemWindowTBitmapPtr()->Fill32(RGBA(0, 0, 0, 0), 0, 0, nWidth, nHeight); TRectangle rect(0, 0, nWidth, nHeight); dc.DrawTextInRectangleEx(pszText, 0, RGBA(255,255,255,255), RGBA(0,0,0,255), m_hFont, &rect, style); dc.ReadBitmap(m_pBmp, 0, 0); nRet = true; } while (0); return nRet; }