Thread: Text Colour for Editboxes

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    32

    Post Text Colour for Editboxes

    Okay. I managed to fix my problem with placing text into my editboxes. What I can't figure out now is how do I change the text colour from the grey it is to the black it should be? Any help would be great.
    cerion
    Use the code Luke.

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Taking info from your previous post, if you just want to stop user input, instead of setting the property of your edit box to disabled you could set it to read only. Your program will still be able to write on it (in black text) but the user won't.
    zen

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    32

    Post

    Well that isn't what I want. If you set it to Read-Only then you still are able to click in the editbox. And it can screw up the information placed inside of the editbox. (if you put text inside of it, then later have to add more to it based on user input, but inbetween setting text the user clicks somewhere between the end and beginning you get the new text placed in the center instead of at the end where it should be.)

    Well at least thats what it does on a like program I've seen, and I don't want that happening with mine.
    cerion
    Use the code Luke.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    15
    The color scheme of the CEdit class is defaulted to the color scheme of the parent. You can change the edit box Text color and background color .

    1) You need to first define a HBRUSH object, to represent the RGB color (COLORREF) that you want. This is typically done in the OnCreate of the CWnd derived window class . The parent window or dialog that the edit box resides in. You should also do the CEdit.create in this area too (which creates an edit box).

    2) You need to set up in the parent class the message map for afx_msg HBRUSH OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor ). Dont forget the ON_WM_CTLCOLOR( ) message.

    3) Once that is done, you have to write the MyCwnd::OnCrlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor ) member function. In this member function make sure you make a call to the default of this class CWnd::OnCrlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor ) . Make sure you set up a switch statement or if statement to only change the color of your editbox, otherwise you will change the color on all of your controls (with some exceptions). You want the CTLCOLOR_EDIT.

    4) In the above member function, you want to grab a handle to the editbox you what to change the color to. You can do that two ways. Use the Myeditbox.GetHandle or by using the m_hObject of the CEdit class (Don't forget to typecast).

    5) which you have the typecasted m_hObject of the Myeditbox. You need to pass that, along with the COLORREF for the colors that you want to setbkcolor and settextcolor.

    As you can, it is pretty complex. I left a lot of the little details out. You should be able to look these functions up in the help to get the details you need to fill in the gaps. If you need more detail on any particular part, let me know. Good luck!
    Last edited by DrCodeGuru; 09-29-2001 at 02:56 PM.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    32
    Okay. Thanks for all that detailed help in C++, however, I do not program in C++. Could you perhaps explain it for a C programmer? Or can anyone? I don't use classes, etc. so most of the things you mentioned would be changed in someway for me.
    cerion
    Use the code Luke.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    15
    In order for me to show you how to do it in C, I will need to see your edit box code. So I can tell you where to insert the code you need. Thanks!

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    32
    I'm using MSVC++ 6.0 which means that I used the resource editor. But I think I can figure out where to place the code if you give me an example or something. My program isn't a normal windows program, I am using a dialog box as the main window of my program.

    I have figured out how to change the background colour of the editbox using the following...

    case WM_CTLCOLORSTATIC:
    if( GetDlgCtrlID((HWND)lParam) == EDIT_FILES ) {
    return((LRESULT)GetStockObject(WHITE_BRUSH));
    }
    return(TRUE);

    Is there a Windows message that comes in for text colour as well that I could use to change it white? I couldn't find any such Windows message myself, but then again I could be wrong. (Oh and that code is from the DlgProc())
    cerion
    Use the code Luke.

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    15
    No specific message for text color. There is a broader message for control color as explained in my previous post. You can catch the WM_CTLCOLOR message. You will then have to make sure that message was for the edit box and not for any other control on the Dialog. That is done by testing for CTLCOLOR_EDIT. You can then use the function settextcolor to change the color of the text. You will need to pass settextcolor the editbox handle and COLORREF. I hope this helps. Good luck!

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    32
    I tried to catch the WM_CTLCOLOR message but I got the following errors:

    error C2065: 'WM_CTLCOLOR' : undeclared identifier
    error C2051: case expression not constant

    Both of these errors are for the line:

    case WM_CTLCOLOR:

    Which is within my DlgProc() function. I have windows.h included (well DUH since I'm programming a Win32 app, but just thought I'd confirm that). Is there any other header file that I need to include in order to be able to catch this windows message?
    cerion
    Use the code Luke.

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    15
    You must be using a Read-only or disabled edit box. That being the case, the WM_CTLCOLORSTATIC is the message you need to catch. All you have to do now is call setbkcolor and settextcolor. setbkcolor will set the background color of the text, and settextcolor will set the text color.

    The WHITE_BRUSH object you used, sets the background color for the editbox window.

    setbkcolor takes the hDC and a COLORREF for the parameters, and so does settextcolor. The hDC is the Handle Device context for the editbox. You can create your own COLORREF by using the RGB() function.

    If you don't know how to get the hDC for the editbox, you can use the API function GetWindowDC(). you will need to pass this function the HWND for the Edit Box. It will return to you the HDC for the editbox.

    Let me know if you need anything else. Good Luck!
    Last edited by DrCodeGuru; 10-02-2001 at 06:30 PM.

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    32
    Okay can you show me an example of this? I used the following code but it didn't work out:

    case WM_CTLCOLORSTATIC:
    if( GetDlgCtrlID((HWND)lParam) == EDIT_FILES ) {
    SetBkColor(GetWindowDC(hwnd), RGB(255,255,255));
    SetTextColor(GetWindowDC(hwnd), RGB(0,0,0));
    }
    return(TRUE);
    cerion
    Use the code Luke.

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    15
    The problem you have, has to deal with the hwnd variable. hwnd has to be the hwnd for the editbox. Set a breakk point at the setbkcolor case line and Go into debug (f5)mode. Step thru the code. See what value is in the hwnd Variable, and make sure this code is being excuted. If you get 0X00000, you will know that this variable has no value. I would like to also see where you declared hwnd, and where you initialize it.

    I believe the hwnd that you used is the hwnd for you main application window, and not for the edit box.

    HWND hEditBox;

    hEditBox = CreateWindow(parameters for edit box)

    hEditBox should be the one you use in the setbkcolor and settextcolor.

  13. #13
    Registered User
    Join Date
    Sep 2001
    Posts
    32
    Well thats the problem. My editbox is on a dialog box (which is the main window of my program) and I used the MSVC++ 6.0 Resource Editor to create the entire dialog box. It doesn't have an HWND variable already set for it. And yes the hwnd I used was for the 'main window' of the program.

    How can I get the hwnd for the editbox if I don't create it like I would if I were simply appending it onto a window?
    cerion
    Use the code Luke.

  14. #14
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Use

    GetDlgItem( HWND hDialog, int ID_CTRL)

    it will return the HWND of ID_CTRL so you can use it in the line

    SetBkColor(GetWindowDC(hwnd), RGB(255,255,255))

    to give you

    SetBkColor(GetWindowDC(GetDlgItem(hwnd,ID_CRTL)), RGB(255,255,255))

    but you will need to ReleaseDC any DC you 'Get' or lose GDI's so I would catch for later use. (put the original color back before you ReleaseDC)
    Last edited by novacain; 10-04-2001 at 01:19 AM.

  15. #15
    Registered User
    Join Date
    Sep 2001
    Posts
    32
    Hmmm. Well the following code doesn't work still:

    case WM_CTLCOLORSTATIC:
    if( GetDlgCtrlID((HWND)lParam) == EDIT_FILES ) {
    SetBkColor(GetWindowDC(GetDlgItem(hwnd, EDIT_FILES)), RGB(255,255,255));
    SetTextColor(GetWindowDC(GetDlgItem(hwnd, EDIT_FILES)), RGB(0,0,0));
    }
    return(TRUE);

    novacain... one problem might be is to ReleaseDC() I need not only the hwnd it's related to, but also the HDC which I don't save for later use (in the above example). Besides I don't think I will ever be needing GDI's for this program. It's just a dialog based (completely text output) program.
    cerion
    Use the code Luke.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  2. Replies: 1
    Last Post: 07-13-2002, 05:45 PM
  3. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  4. To change text colour
    By Dirty Harry in forum C Programming
    Replies: 2
    Last Post: 06-16-2002, 03:04 AM
  5. Setting text colour
    By JamMan in forum C++ Programming
    Replies: 1
    Last Post: 11-19-2001, 11:43 AM