Thread: Drawing on a RichEdit?

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    13

    Drawing on a RichEdit?

    i'm 'drawing' a colored rectangle on a subclassed Richedit control using InvertRect, but everytime the region gets covered by another control it reverts to the background color.

    this is the code i'm using:

    Code:
    void PaintSelection()
    {
    		
    		// hdcMem is memory device context that i'm blitting from 
    		// to avoid flicker.
    		// hDC is the device context of the RichEdit
    		// rectClient is the rectangular region of the RichEdit
    		// rectPrev is the previously highlighted region
    		// rectCurr is the currently highlighted region
    		BitBlt(hdcMem, 0, 0, rectClient.right - rectClient.left,
    			rectClient.bottom - rectClient.top, hDC, 0, 0, SRCCOPY);
    		InvertRect(hdcMem, &rectPrev);
    		InvertRect(hdcMem, &rectCurr);
    
    		BitBlt(hDC, 0, 0, rectClient.right - rectClient.left,
    			rectClient.bottom - rectClient.top, hdcMem, 0, 0, SRCCOPY);
    		rectPrev = rectCurr;
    }
    // WM_PAINT handler
    .....
    case WM_PAINT:
    			PAINTSTRUCT ps;
    			// This calls the Default Handler to paint the screen
    			::CallWindowProc(m_oldProc, this->m_hWnd, uMsg, wParam, lParam);
    			
    			// Now it calls PaintSelection
    			BeginPaint(m_hWnd, &ps);
    			PaintSelection();
    			EndPaint(m_hWnd, &ps);
    			
    			return false;
    .....

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Perhaps you need to intercept WM_ERASEBKGND as well?

    gg

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    13
    I think I've found the problem. InvertRect doesn't use a Brush to color the rect, so it seems like the output only gets reflected on the monitor. If I use FillRect with an alternating Black and White brush like this:
    Code:
    FillRect(hdcMem, &rectPrev, (HBRUSH)GetStockObject(WHITE_BRUSH));
    FillRect(hdcMem, &rectCurr, (HBRUSH)GetStockObject(BLACK_BRUSH));
    then Windows remembers the colors of the rectangular regions. The only problem is that everything (text) in the rectangle gets colored black or white, which is not what I want.

    So I guess I need to find out how to invert the colors of a rectangular region using a brush or how to capture the text inside a RECT and invert its color.
    Last edited by Sea Monster; 12-30-2003 at 02:39 AM.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    161
    to invert colors you could use SRCINVERT constant with BitBlt.

    Hope that helps!

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    13
    Originally posted by Benzakhar
    to invert colors you could use SRCINVERT constant with BitBlt.

    Hope that helps!
    Actually InvertRect calls BitBlt with those arguments anyway so it basically boils down to the same thing. I did however find out what the problem was and how to fix it.

    Because BitBlt doesn't use a Brush to paint the screen, Windows doesn't really 'know' about the color change, so every time WM_PAINT gets called, it reverts back to the color of the Brush associated with the Device Context. IOW, I should only invert the rect ONCE when I call the function from WM_PAINT because windows already set it back to it's original color.

    I fixed it by adding a Boolean parameter to the PaintSelection function which specifies whether or not it was being called from WM_PAINT. If it was, then there is no need to invert the previous rect because the default handler (::CallWindowProc(m_oldProc.....) set it back already.

    Thanks for the help anway.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Haha, it's usually something simple....

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Slow drawing code
    By tjpanda in forum Windows Programming
    Replies: 5
    Last Post: 05-09-2008, 05:09 PM
  2. Line Drawing Algorithm
    By Axpen in forum Game Programming
    Replies: 15
    Last Post: 08-01-2005, 06:30 PM
  3. RichEdit need help!!!
    By lucas4ce in forum Windows Programming
    Replies: 1
    Last Post: 08-21-2004, 02:27 PM
  4. Problems with my richedit...
    By tyouk in forum Windows Programming
    Replies: 2
    Last Post: 11-02-2003, 04:57 AM
  5. RichEdit Problem
    By dirkduck in forum Windows Programming
    Replies: 0
    Last Post: 07-24-2003, 05:50 PM