Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

## text_painter

The text_painter draws a text into his drawing-rectangle so that it best fits. You can pass a maximum and a minimum font height and the text_painter iterates through all the heights, starting with the maximum. If the text fits vertically it will be drawn. If the text doesn't even fit into the drawing-rect with the minimum font height it will nevertheless be drawn.

    namespace win32 { namespace gui {

         class text_painter : public painter_base
         {
         public:
              // info structure needed to load the text_painter
              struct font_info;

              typedef const std::string& param0;
              typedef const font_info& param1;

              // c'tor
              text_painter(const std::string& str, const font_info& info);

              // virtual functions of painter_base
              text_painter* clone() const;
              void draw(HDC hDC, int cx, int cy);

              // own functions
              void text(const std::string& str);
              std::string text() const;
              void info(const font_info& info);
              font_info info() const;
              void draw_text_options(unsigned flags);
              unsigned draw_text_options() const;
              unsigned last_used_text_height() const;

              struct font_info
              {
                   font_info(LOGFONT lf, unsigned min_font_height, unsigned max_font_height, COLORREF cr = RGB(0, 0, 0));

                   LOGFONT lf_;
                   unsigned min_font_height_;
                   unsigned max_font_height_;
                   COLORREF cr_;
              };
         };

    } }  // namespace win32::gui

Note:
  - text_painter::font_info holds the information necessary to draw the text. You can pass a LOGFONT-object describing the font that is used, minimum and maximum font height and a text color
  - `info()` sets or returns the underlying text_painter::font_info
  - `text()` sets or returns the text that is drawn
  - The text_painter draws the text by calling `DrawText()`. `draw_text_options()` allows changing the flags that are passed to this function. For further information take a look into your MSDN
  - `last_used_text_height()` returns the last text height. Be aware: the painter must be drawn first before this function returns a useful result, else it will return 0
  - Tip: If minimum and maximum font height are the same the text is drawn in that height

Example:

    // create colored rectangle behind the text
    COLORREF cr_blue = RGB(0, 0, 150);
    rectangle rc(0, 0, 150, 30);
    unsigned idx_color = add_painter<color_painter> (color_painter::make_brush(cr_blue));
    drawing_rect(idx_color, rc);

    // create the font information
    LOGFONT lf;
    memset(&lf, 0, sizeof(lf));
    lf.lfWeight = FW_NORMAL;
    lf.lfQuality = PROOF_QUALITY;
    strcpy(lf.lfFaceName, "Arial");
    COLORREF cr_white = RGB(255, 255, 255);
    const unsigned max_size = ...;
    const unsigned min_size = ...;

    // add the text_painter and set its drawing_rect to the same as the text_painter's
    std::string text = "hello world!";
    unsigned idx_text = add_painter<text_painter> (text, text_painter::font_info(lf, min_size, max_size, cr_white));
    drawing_rect(idx_text, rc);

The result is as follows

min_size = 0, max_size = 140 (last_used_text_height() returns 51) | min_size = 80, max_size = 140 (last_used_text_height() returns 80)

| by Steven Weiss. You can contact me at steven11@gmx.de.