CDC::DrawText

Maria 21 Reputation points
2023-05-29T15:29:31.8766667+00:00

Hi. I use DrawText(str, &rect, DT_CENTER | DT_CALCRECT | DT_WORDBREAK) to find rectangle. Why is the size of rectangle depending on scroll and zoom factor? What should I do to avoid this dependence?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,631 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jeanine Zhang-MSFT 9,431 Reputation points Microsoft Vendor
    2023-05-30T06:38:35.0766667+00:00

    Hello,

    Welcome to Microsoft Q&A!

    If there are multiple lines of text, DrawText returns the height of the formatted, however it'll modify the width of the rectangle to bound the last line of text.

    According to the Doc: DrawText function

    DT_CALCRECT: Determines the width and height of the rectangle. If there are multiple lines of text, DrawText uses the width of the rectangle pointed to by the lpRect parameter and extends the base of the rectangle to bound the last line of text. If the largest word is wider than the rectangle, the width is expanded. If the text is less than the width of the rectangle, the width is reduced. If there is only one line of text, DrawText modifies the right side of the rectangle so that it bounds the last character in the line. In either case, DrawText returns the height of the formatted text but does not draw the text.

    If you want to force the text to fit in a fixed rectangle, you cannot use DT_CALCRECT. However, if you do not use DT_CALCRECT, the last lines will be clipped if necessary.

    Thank you.

    Jeanine


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful