CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2003
    Posts
    938

    FillSolidRect & DrawText

    Hey.

    I recently ran into a problem trying to do some custom drawing in a view. (CScrollView derived). I use FillSolidRect to fill a rectangle, and then DrawText to render some text in OnDraw. The problem is that all of the text is set on the background that i set using FillSolidRect as opposed only to the bit that i defined as.
    Here is some code:
    Code:
    pDC->FillSolidRect(	m_lbtnDown.x,
    						m_lbtnDown.y,
    						m_lbtnUp.x - m_lbtnDown.x,
    						m_lbtnUp.y - m_lbtnDown.y,
    						m_crTestA);
    ...
    CPoint ScrolledPos = GetScrollPosition();
    	
    	CRect rectClient;
    	GetClientRect(&rectClient);
    	
    	pOldFont	= pDC->SelectObject(m_pFont);
    	nHeight		= MeasureFontHeight(m_pFont, pDC);
    ...
     pDC->DrawText(strRender, -1, &ScrollRect, DT_TOP|DT_NOPREFIX|DT_SINGLELINE);
    ...
    pDC->SelectObject(pOldFont);
    Any ideas why?

    Thx in advance.

  2. #2
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: FillSolidRect & DrawText

    well it is hard to figure out what you are talking about, but I think you
    should look up

    CDC::IntersectClipRect
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  3. #3
    Join Date
    Aug 2003
    Posts
    938

    Re: FillSolidRect & DrawText

    Ah, thx i will be looking into that.

    Basically my problem is as follows (sry for crappy explanation b4, it was 4 am ).

    I need to draw a rectangle at some random x, y. I can draw that rectangle fine. Now i need to draw text overtop of it. When i start drawing text (using DrawText function), i use a loop that will effectively fill the whole screen from top to bottom. This also works fine by itself. Now when i try to do both, the rectangle and the text, for some reason the rectangle gets extended over all of the text. This is really weird, but i think it's something with me not clearing some buffer after i use FillSolidRect, and the DrawText function automatically uses that background for the text?

    Is there something of that sort implemented within DrawText/FillSoliRect?

    [EDIT]

    I did some more debugging, and yep, it's something about the buffer. Basically if i reset the BgColor of the text after i draw the rectangle, the text will overwrite the rectangle with the color specified completely.

    Is there a way to draw layers one after another, or will i have to calcualte the text offset that i want to be highlighted and then change the background for that piece of text?

    Thx in advance.
    Last edited by Quell; June 15th, 2008 at 11:50 AM.

  4. #4
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: FillSolidRect & DrawText

    Not sure I fully understand what you want, but I think you can use the same rect that you use for FillSolidRect and create a rectangular region (CreateRectRgn). Then, select that into your DC as a ClipRegion (SelectClipRgn). Now when you draw text (or anything else), your drawing will only occur within that region.

    Be sure to select the NULL region to re-enable drawing in the entire DC.

    Hope that helps.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  5. #5
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: FillSolidRect & DrawText

    Set the background mode to TRANSPARENT before drawing the text

    Code:
    	pDC->SetBkMode(TRANSPARENT);
    	pDC->DrawText(strRender, -1, &ScrollRect, DT_TOP|DT_NOPREFIX|DT_SINGLELINE);
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  6. #6
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

    Re: FillSolidRect & DrawText

    Quote Originally Posted by Quell
    Hey.

    I recently ran into a problem trying to do some custom drawing in a view. (CScrollView derived). I use FillSolidRect to fill a rectangle, and then DrawText to render some text in OnDraw. The problem is that all of the text is set on the background that i set using FillSolidRect as opposed only to the bit that i defined as.

    Any ideas why?

    Thx in advance.
    Yes, that is the normal behaviour.

    After using FillSolidRect you have to call CDC::SetBkColor to (re-)set the background color for text outputting funtions. Another solution is to set the bk mode to TRANSPARENT (CDC:SetBkMode(TRANSPARENT) ) since you filled the background alredy ...

    Hope this was useful.

    With regards
    Programartist
    Ingo Bochmann
    Last edited by ProgramArtist; June 16th, 2008 at 02:08 AM. Reason: Edit: Someone has been faster ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured