Senin, 02 Juli 2012

Draw Text Centered

Description
Quote
Graphics code snippet to draw some text centered on a TImage.
Assumes you have already defined GetFontHeight.

Snippet
#include <string>
#include <cassert>
void DrawTextCentered(TImage * const image, const std::string& s)
{
  const int textWidth = image->Canvas->TextWidth(s.c_str());
  const int textHeight = GetFontHeight(image);
  assert(textHeight >= 0);
  const int midX = image->Picture->Graphic->Width  / 2;
  const int midY = image->Picture->Graphic->Height / 2;
  const int x1 = midX - (textWidth  / 2);
  const int y1 = midY - (textHeight / 2);
  const int x2 = midX + (textWidth  / 2);
  const int y2 = midY + (textHeight / 2);
  image->Canvas->TextRect(
        TRect(x1,y1,x2,y2),
        x1,y1,s.c_str());
}

Tidak ada komentar:

Posting Komentar