Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 494107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:28:01+00:00 2026-05-13T05:28:01+00:00

I’ve got a little text drawing puzzle under Win32. I’m trying to draw some

  • 0

I’ve got a little text drawing puzzle under Win32. I’m trying to draw some instructions for users of my application at the top of the window.

Please refer to the following window (I’ve changed the background color on the text so you can see the boundaries)

Demonstration
(source: billy-oneal.com)

I’m currently using DrawTextEx to draw the text to my window, but the problem is that it does not fill the entire RECTangle that I give it. Not drawing that area is just fine, until the window resizes:

Demonstration after resize
(source: billy-oneal.com)

When the text is re wrapped due to the window sizing, because DrawTextEx doesn’t clear it’s background, these artifacts are leftover.

I tried using FillRect to fill in the area behind the text drawing call, which does eliminate the visual artifacts, but then causes the text to flicker constantly, as it is completely erased and then completely redrawn to the display.

Any ideas on how one might get the area not containing text to be drawn with the background color?

EDIT: I’d like to avoid having to double buffer the form if at app possible.

EDIT2: I solved the problem by only redrawing the text when I detect that the wrapping changes during a resize.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:28:01+00:00Added an answer on May 13, 2026 at 5:28 am

    Well since nobody seems to know what to do about it, I implemented it this way:

    std::vector<std::wstring> wrapString(HDC hDC, const std::wstring& text, const RECT& targetRect, HFONT font)
    {
        std::vector<std::wstring> result;
        RECT targetRectangle;
        CopyRect(&targetRectangle, &targetRect);
    
        //Calculate the width of the bounding rectangle.
        int maxWidth = targetRectangle.right - targetRectangle.left;
    
        //Draw the lines one at a time
        std::wstring currentLine;
        for(std::wstring::const_iterator it = text.begin(); it != text.end(); currentLine.push_back(*it), it++)
        {
            if(*it == L'\r' || *it == L'\n')
            { //Hard return
                while(it != text.end() && (*it == L'\r' || *it == L'\n')) it++;
                result.push_back(currentLine);
                currentLine.clear();
            }
            else
            { //Check for soft return
                SIZE sizeStruct;
                GetTextExtentPoint32(hDC, currentLine.c_str(), static_cast<int>(currentLine.length()), &sizeStruct);
                if (sizeStruct.cx > maxWidth)
                {
                    std::wstring::size_type lineLength = currentLine.find_last_of(L' ');
                    if (lineLength == currentLine.npos)
                    { //Word is longer than a line.
                        for(;it != text.end() && !iswspace(*it);it++) currentLine.push_back(*it);
                    }
                    else
                    { //Clip word to line.
                        //Backtrack our scan of the source text.
                        it -= currentLine.length() - lineLength - 1;
                        //Remove the clipped word
                        currentLine.erase(lineLength);
                    }
                    result.push_back(currentLine);
                    currentLine.clear();
                }
            }
        }
        //Last remaining text.
        result.push_back(currentLine);
        return result;
    }
    
    void DrawInstructionsWithFilledBackground(HDC hDC, const std::wstring& text, RECT& targetRectangle, HFONT font, COLORREF backgroundColor)
    {
        //Set up our background color.
        int dcIdx = SaveDC(hDC);
        HBRUSH backgroundBrush = CreateSolidBrush(backgroundColor);
        SelectObject(hDC, backgroundBrush);
        SelectObject(hDC, font);
        SetBkColor(hDC, backgroundColor);
    
        std::vector<std::wstring> lines(wrapString(hDC, text, targetRectangle, font));
        for(std::vector<std::wstring>::const_iterator it = lines.begin(); it!=lines.end(); it++)
        {
            RECT backgroundRect = targetRectangle;
            DrawText(hDC, const_cast<LPWSTR>(it->c_str()), static_cast<int>(it->length()), &backgroundRect, DT_CALCRECT | DT_NOCLIP | DT_SINGLELINE);
            backgroundRect.left = backgroundRect.right;
            backgroundRect.right = targetRectangle.right;
            if (backgroundRect.right >= backgroundRect.left)
            FillRect(hDC, &backgroundRect, backgroundBrush);
            ExtTextOut(hDC, targetRectangle.left, targetRectangle.top, ETO_OPAQUE, NULL, it->c_str(), static_cast<UINT>(it->length()), NULL);
            targetRectangle.top += backgroundRect.bottom - backgroundRect.top;
        }
        instructionsWrap = lines;
        //Restore the DC to it's former glory.
        RestoreDC(hDC, dcIdx);
        DeleteObject(backgroundBrush);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.