Thread: changing color of certain words in an edit box?

  1. #1
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905

    changing color of certain words in an edit box?

    how would i change the color of certain words in an edit box? I mean in the same way that visual studio does it.......like when you type an if or true or false or pretty much every single keyword

    I want to be able to make an editor that will make certain words turn blue if they are valid commands and comments turn green (for my scripting language)

    -thanks in advance

  2. #2
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    You could try something like this if you're using the MFC, but very easy to convert to Win 32. This will set the IDC_EDIT1 textbox text red.
    Code:
    LRESULT CEditboxDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
    {
    	// TODO: Add your specialized code here and/or call the base class
    	static COLORREF TextCol = RGB(255,0,0);	// RED
    	LRESULT RC = FALSE;
    	
    	switch (message)
    	{
    		case WM_CTLCOLOREDIT:
    			if (IsWindow((HWND)lParam))
    			{
    				if (IDC_EDIT1 == ::GetDlgCtrlID((HWND)lParam))
    				{
    					RC = CDialog::WindowProc(message, wParam, lParam);
    					SetTextColor((HDC)wParam, TextCol);
    					return 	RC;
    				}
    			}
    			break;
    	}
      	return CDialog::WindowProc(message, wParam, lParam);
    }
    Last edited by Scarlet7; 04-27-2003 at 04:24 PM.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Look up the richedit control.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    but, what i need is to be able to highlight ONE specific word, and be able to do it quickly.........

  5. #5
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    so, does anyone here know how to do this? if you've ever used visual studio, you should know what I'm talking about. For example: this is what some c++ code would be like:

    Code:
    #include  <iostream.h>
    
    int  main()
    {
    	int  var1;
    	return  1;
    }
    and what I want to do, is if someone types in some OpenScript code, it'll look like so:

    Code:
    BEGIN;
    	Reset;
    	Move(0,0,-100);
    END;

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Again, look up the rich edit control.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>so, does anyone here know how to do this?<<

    answer->XSquared: Look up the richedit control.

    Other than that, you will have to design your own control and handle the syntax highlighting and the basic edting functionality yourself. Or, you could subclass an edit control, rely on its default editing capability, but still handle the syntax highlighting yourself.

    To see an example of a full blown syntax highlighting control take a look at Scintilla; it's used by a number of 'free' ide's/editors including their own SciTe and (probably) devcpp.

    So the simplest/easiest answer to your question is: use someone else's control or, if you must build one yourself, start very simply with something like a richedit control, as XSquared as already stated.

    edit: Beaten by XSquared - and rightly so!

  8. #8
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    ah, i see, sorry bout that, didn't see the above

    -edit-
    that scintalla thing is pretty sweet, it'll probably be a while before I can get to that stage of Win32 programming. lol

  9. #9
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    well, i've spent about the past 3 hours staring at msdn, and I haven't learned a thing yet

    i can't figure out how to use an edit box in my program

    what i'm doing is i did the tutorials from http://winprog.org/tutorial/ and i did the multiple document interface, changing and adding a few things. but the thing is, i can't get it to create an edit box. I don't know HOW to create one, because all msdn says is, "hey, we've got edit boxes, whoohooo! but we're not telling you how to use 'em, we'll just tease you with all the stuff it CAN do! muahahahaha"

    so, yah, msdn is evil sometimes. lol

    help would be appreciated, like some sort of example or tutorial somewhere that is semi-decent......

    -thanks in advance

    -edit-
    oh yeah, here's all the code that is really of importance here:

    Code:
    BOOL CALLBACK EditProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	switch(msg)
    	{
    	default:
    		return FALSE;
    	}
    	return TRUE;
    }
    
    LRESULT CALLBACK MDIChildWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	switch(msg)
    	{
    		case WM_CREATE:
    		{
    			HFONT hfDefault;
    			HWND hEdit;
    
    			RichEdit test;
    			hEdit=CreateDialog(hInMain, MAKEINTRESOURCE(IDD_EDITBOX), MainWindow, EditProc); // here's where i'm creating the richedit box, but it crashes whenever i try and open it
    			ShowWindow(hEdit, numcmd);
    			UpdateWindow(hEdit);
    
    			if(hEdit==NULL)
    				MessageBox(hwnd, "Could not create edit box.", "Error", MB_OK | MB_ICONERROR);
    
    			hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
    			SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
    		}
    		break;
    // and more from here, not putting it all, lol
    Last edited by jverkoey; 04-29-2003 at 08:46 PM.

  10. #10
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    After 30 seconds of searching MSDN:
    http://msdn.microsoft.com/library/de...itcontrols.asp
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  11. #11
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    ok, fjlajfds, here's the whole project:


    and don't flame me for global variables/variable names or whatever, i'm trying to see if i can even do this program

  12. #12
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Originally posted by XSquared
    After 30 seconds of searching MSDN:
    http://msdn.microsoft.com/library/de...itcontrols.asp
    eeeuhhhbgf *sputter*

    well, i guess msdn hates me

    cuz i searched "richedit" and all it came up with was this page:

    http://msdn.microsoft.com/library/de...richedit_m.asp

    so, thanks msdn, lol

  13. #13
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    ok, it appears that i can't create any dialogs whatsoever in my program for some reason now

    if you hit the about button, it just does nothing, but i KNOW that i'm creating the dialog box

    i'm really getting frustrated at this, considering i've spent about 5 hours trying to figure this out

    i need someone who can actually help me, and not just give me links, i've passed the link stage, none of 'em are helping me

  14. #14
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>but i KNOW that i'm creating the dialog box

    The edit dialog you are trying to create inside the MDI child dlg is failing. Try creating the edit dlg in response to a NEW msg not the MDI childs WM_CREATE.

    Use GetLastError() to give you a clue as to what is happening.

    PS
    I would use a HDC and intecept WM_KEYDOWN msgs. Then I could paint what ever I wanted on screen, whatever colour I wanted.
    This may be harder to start with but will give you full control.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_HSCROLL in ES_READONLY edit box error
    By Homunculus in forum Windows Programming
    Replies: 4
    Last Post: 02-13-2006, 08:46 AM
  2. Multiline Edit Box Parser
    By The Brain in forum Windows Programming
    Replies: 6
    Last Post: 11-01-2005, 07:15 PM
  3. setting fixed floats in edit box
    By WaterNut in forum Windows Programming
    Replies: 4
    Last Post: 08-13-2004, 09:13 AM
  4. Limit of Edit Box
    By osal in forum Windows Programming
    Replies: 0
    Last Post: 07-15-2004, 11:58 AM
  5. Edit box scroll
    By kybert in forum Windows Programming
    Replies: 3
    Last Post: 10-21-2002, 08:50 PM