0

I have a simple Win32 API dialog-based application that I've written which contains a rich edit control. The control displays the contents of ANSI-based text files and does some very basic syntax highlighting.

I am using Visual C++ 2010 Express to write the code, and when I compile in Release mode, everything works perfect. However, when I compile in debug mode, the program runs, the syntax highlighting appears to be happening, but the text in the control does not change color.

Any ideas on why this might be happening?

EDIT: This snippet of code was added to show how I am attempting to color the text in the rich edit control.

CHARFORMATA _token; // This variable is actually a member variable.
                    // I just pasted it in the body of the function
                    // so the code would make sense.

// _control is a pointer to a rich edit control object. I created a
// REdit class that adds member variables for a rich edit control.
// The class contains an HWND member variable storing the window
// handle. The method GetHandle() returns the window handle.

void SyntaxHighlighter::ColorSelection(COLORREF color)
{
  CHARFORMATA _token;
  _token.cbSize = sizeof(CHARFORMATA);
  _token.dwMask = CFM_COLOR;
  _token.crTextColor = color;
  SendMessageA(_control->GetHandle(), EM_SETCHARFORMAT,
               (WPARAM)SCF_SELECTION, (LPARAM)&_token);
}

As I mentioned above, when I compile in Release mode, the coloring of the text works as intended. When I compile in Debug mode, the coloring does not happen. I'm wondering if in Debug mode, if certain features of the control don't work?

  • There isn't enough information here to answer your question, but in general when you get different results from debug and release builds, it's usually because your settings are different between the two (other than simply debug vs no debug and optimizing vs not). What have you done to try and debug the problem? – Carey Gregory Mar 05 '12 at 05:08
  • @CareyGregory In response to your question, I posted a snippet of the code that I am using to color a selection. –  Mar 06 '12 at 05:11
  • Since it works in release mode, the code you posted must be functioning correctly and that's not where the problem lies. Most likely the problem lies elsewhere, prior to the call to ColorSelection(). Do the GetHandle() and SendMessage() calls succeed? You should be able to step into the code in the debugger and verify these things. – Carey Gregory Mar 06 '12 at 15:33
  • @CareyGregory Yes, all the functions are working in debug mode as well. The process of calling the ColorSelection() function happens, and the message is sent to color the text in the control, but on the screen it isn't happening. Does that make sense? –  Mar 09 '12 at 15:20

1 Answers1

1

You're setting dwMask to CFM_COLOR, which says that both the crTextColor and dwEffects members are valid, but you're not initializing dwEffects. In release mode it's probably ending up zero but in debug mode some random flag value that causes it not to work. I would recommend doing it this way:

CHARFORMATA _token;
memset(&_token, 0, sizeof(_token));
_token.cbSize = sizeof(CHARFORMATA);
_token.dwMask = CFM_COLOR;
_token.crTextColor = color;
Carey Gregory
  • 6,836
  • 2
  • 26
  • 47
  • That was it! Finally! Thank you very much for the continued replies with regards to this problem. I don't know why the thought of setting dwEffects to 0 didn't enter my mind. (= –  Mar 10 '12 at 01:33
  • Easy to overlook little details like that. I make it a habit to zero structs before initializing them and passing them. I think it's a habit that avoids a lot of bugs. – Carey Gregory Mar 10 '12 at 05:25