tags:

views:

48

answers:

2

Given a custom control CCustomWnd which has its own OnPaint method, what's the simplest way that CCustomWnd::OnPaint can render the contents of a CRichEditCtrl, with the same formatting?

To clarify, elsewhere in my dialog/window is a CRichEditCtrl. I have my custom control which does a bunch of custom-drawing, including drawing the contents of the edit control. Currently it doesn't preserve the formatting, now it needs to (not everything, but color/decoration).

The custom control can't be replaced or substantially rewritten. So essentially given a CDC and a CRichEditCtrl, how do I render the formatted text from the latter using the former?

+1  A: 

Rich edit controls do support a couple of messages (EM_FORMATRANGE and EM_DISPLAYBAND) intended primarily for printing. I've never tried it, but offhand I can't think of any real reason they'd require that the DC refer to a printer instead of a window on screen. That being the case, you should be able to send the messages to the existing rich edit control, telling it to render the correct portion of its content to the selected rectangle in your custom control.

Jerry Coffin
Interesting. I'll look into that.
John
+1 - Probably the best (and only) method save writing your own RTF rendering engine.
So it's not possible to loop through fragments of text in a CRichEditCtrl, which was my 1st thought how to achieve this?
John
@John:retrieving fragments is somewhat problematic -- there can be an arbitrary amount of other "stuff" between a format and text that it affects, so maintain formatting, you have to retrieve everything up to the part you care about.
Jerry Coffin
+1  A: 

Can't you make a 'screenshot' (GetDC(), BitBlt() to memory DC) of the rich edit control and display that elsewhere?

Roel
I _assume_ that would work. Anyone? DCs are not an area I've ever worked with in detail.
John
CDC* dc = m_RichEditCtrl->GetDC();CRect r_edit_size;m_RichEditCtrl->GetClientRect(CDC mem_dc;mem_dc.CreateCompatibleDC(dc);CBitmap mem_bitmap;mem_bitmap.CreateCompatibleBitmap(mem_dc, r_edit_size.Width(), r_edit_size.Height());mem_dc.SelectObject(mem_dc.BitBlt(0, 0, r_edit_size.Width(), r_edit_size.Height(), dc, r_edit_size.Width(), r_edit_size.Height(), SRCOPY);CDC* target_ctrl_dc = m_CustomCtrl.GetDC();target_ctrl_dc.BitBlt(0, 0, r_edit_size.Width(), r_edit_size.Height(), mem_dc, r_edit_size.Width(), r_edit_size.Height(), SRCCOPY);
Roel
Something like that, comments don't take code well :/ You'll probably have to clean up the DC's but selecting the old bitmaps back into them (you get a ptr to the old bitmap when doing SelectBitmap())
Roel