question

ts-8004 avatar image
0 Votes"
ts-8004 asked ts-8004 commented

Using Rich Edit for syntax colouring

I am using messages to a Rich Edit box to assign a text colour to individual characters or sequences of characters. Here is (part of) the code.


 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
 {
     HWND reeditbox;
     CHARFORMAT2A recf2;
    
     switch (message)
     {
     case WM_COMMAND:
         {
             int wmId = LOWORD(wParam);
                            
             // Parse the menu selections:
             switch (wmId)
             {
             case IDM_ABOUT:
    
                 reeditbox  = CreateRichEdit(hWnd, 0, 0, 500, 500, 0);
    
                 // --- issue EM_GETCHARFORMAT / EM_SETCHARFORMAT to test changing edit box characteristics
    
                 recf2.cbSize = sizeof(struct CHARFORMAT2A);
                   
                 // --- try Red
    
                 SendMessage(reeditbox, EM_SETSEL, (WPARAM)5, (LPARAM)6);
    
                 SendMessage(reeditbox, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM) &recf2);
    
                 if (recf2.dwEffects & CFE_AUTOCOLOR)
                     recf2.dwEffects -= CFE_AUTOCOLOR;
    
                 recf2.crTextColor = RGB(255, 0, 0);
    
                 recf2.dwEffects |= CFE_UNDERLINE;
                 recf2.dwMask |= CFM_UNDERLINE;
                    
                 SendMessage(reeditbox, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&recf2);


I am trying to find out whether there is an appropriate way of implementing syntax colouring for C using Rich Edit boxes. I think there is no easy way of doing this. You would have to code a solution to read character attributes eg text colour, and write character attriibutes again eg text colour. There is no function built into Windows to deal with syntax colouring.

Of the solutions to syntax colouring, do any of them involve the coding of Rich Edit boxes?

Please tell me if my understanding is correct. Thank you for your answers.

c++
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

It'll be a considerable task. Have a look around for 3'rd party controls that already do it. Maybe this project would do it for you?


0 Votes 0 ·

I think that Rich Edit can be used with certain effort in case of read-only texts. But when the text must be editable, the colourisation needs EM_SETSEL, which moves the caret and scrolls the text. This disturbs the users even if you move the caret back.

It is more appropriate to use RichTextBox control from WPF (in C# or VB).


0 Votes 0 ·

Is WPF Dead 2019?
WPF as a framework is dead because no new development is being done by corporate in WPF as there are many new cross platform desktop app development frameworks available in market (like UNO Platform, Xamarin, Electron).

Both Windows Forms and WPF are old, and Microsoft is pointing developers towards its Universal Windows Platform (UWP) instead. UWP is an evolution of the new application platform introduced in Windows 8 in 2012.

Looks like WPF is pining for the fjords.

Is UWP appropriate to this aim?

0 Votes 0 ·

@ts-8004

As far as I'm concerned, UWP is appropriate to this aim. Whether you want to use uwp control in the win32 application or want to use uwp application directly?

0 Votes 0 ·
Show more comments

0 Answers