CRichEditCtrl OnUpdate(): how to know the start and end positions when a paste is received?

334 Views Asked by At

I'm using a CRichEditCtrl to edit a computer language, and on every change to it I'm calling SetSelectionCharFormat on the current line of text (as reported by LineFromChar(-1)) to highlight the syntax. (EG: comments in green, section headings in a bigger font, compilation errors in red, etc.) Note this language doesn't have multi-line features such as a C comment where typing /* on one line makes following lines part of a comment too; for any given character change I only need to change the color of the current line.

It all looks like its working fine.

However there are some weird issues. One is, when multiple lines of text is selected from somewhere else, and pasted. My OnUpdate() is called but is naively assuming that the only line that potentially needs re-formatting is the one returned by LineFromChar(). That suffices when the user is typing character by character, but it means that after receiving a multi-line paste, the program only reformats the last line of the pasted text. How can it know where the start of the insert was?

1

There are 1 best solutions below

1
On

OnUpdate is called inside the Paste operation.

It should be possible to subclass the RTF control and to intercept the WM_PASTE message. If WM_PASTE is not used internally it might be possible to use EM_PASTESPECIAL. If even tis message isn't sent, you have to interecept the Ctrl+V that causes the paste Operation.

Than you can determine the starting position of the paste operation.

Spy++ might be helpful to determine the message flow in the RTF control.