problem with a control color

This is a discussion on problem with a control color within the Windows Programming forums, part of the Platform Specific Boards category; I've changed the color of an editbox control, but there is just one little problem: If the editbox gets a ...

  1. #1
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768

    problem with a control color

    I've changed the color of an editbox control, but there is just one little problem:

    If the editbox gets a focus when it's empty, the background changes back to the default color.

    Here is the code:
    Code:
    case WM_CTLCOLOREDIT:
             {
                    HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 0));
                    SetBkMode((HDC)wParam, TRANSPARENT);
                    SetBkColor((HDC)wParam, RGB(0, 0, 0));
                    return (long)hBrush;
             }
    Why does it happen, and how can I fix it?

    Thank you.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I don't know but you have a resource leak. If you want a black background use:
    Code:
    HBRUSH hBrush = GetStockObject(BLACK_BRUSH);
    Otherwise, create the brush once and store it in a static variable for subsequent requests. Try giving the same response for WM_CTLCOLORSTATIC.

    Code:
    case WM_CTLCOLORSTATIC:
    case WM_CTLCOLOREDIT:
             {
                    HBRUSH hBrush = GetStockObject(BLACK_BRUSH);
                    SetBkMode((HDC)wParam, TRANSPARENT);
                    SetBkColor((HDC)wParam, RGB(0, 0, 0));
                    return (LRESULT) hBrush;
             }

  3. #3
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    well, that didn't help, but thanks for trying.
    also, what do you mean by a resource leak? black_brush if i want to use black, but what if i want to use some other colors, i can use the code i gave below, and they both give me the same result.

    the problem is still the same, the editbox is empty and once i click on it, to type something, the background changes back to the default color (white). and after i begin typing it turns to black... what the hell is going on???
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  4. #4
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    well I think I've found something... if the editbox has ES_MULTILINE, the problem goes away, but that's just weird...
    I'm using windows 98, could that be a bug of the system?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Try losing the transparent back mode.

    http://groups.google.com/groups?th=413a8c7eae7f96f4

    Link also discusses your resource leak.

  6. #6
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    I've read the answers to the question at the link you posted, but I still don't understand how can I fix this resource leak...
    First, this code has a resource leak. You are supposed to return the
    handle of brush that you own. You above code returns a brand new
    brush each time!

    The system DOES NOT delete the brush you return from this message, it
    simply uses it. You are supposed to allocate the brush *once* and
    return its handle for each message. When the window goes away, you
    delete the brush. If you get a WM_SYSCOLORCHANGE, you may need to
    destroy and current brush and allocate the new one. The point is,
    the system does not assume ownership of the brush and code like what
    you show above is "throwing away" a brush each time the edit window
    paints (at least).
    If i understand it right, I don't need to return the brush each time the WM_CTLCOLOREDIT is sent... but my question is where do I return the brush handle then?


    thanks
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You create the brush at startup, store it and return the same brush every time.

    Code:
    /* Brush stored in global variable */
    HBRUSH g_hBrush
    
    int WINAPI WinMain( ... )
    {
                    /* Create brush at startup */
                    g_hBrush = CreateSolidBrush(RGB(255, 0, 0));
    
                    ...
    
                    /* Destroy Brush at termination */
                    DeleteObject(g_hBrush);
                    return 0;
    }
    Code:
    case WM_CTLCOLOREDIT:
             {
                      ...
                    /* Return the same brush every time rather than 
                     * creating a new one */
                    return (LRESULT) g_hBrush;
             }
    Where you create, store and destory the brush may vary depending on your program, but you should only do it once.

    For example you could create the brush in WM_CREATE, store it in window memory and destory it in WM_DESTROY if you didn't want to use a global variable.

  8. #8
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    For example you could create the brush in WM_CREATE, store it in window memory and destory it in WM_DESTROY if you didn't want to use a global variable.
    How can I do it? How do I send the brush value from one message to another without using a global variable?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  9. #9
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Make it a static variable in the window procedure, above the switch statement.

  10. #10
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    But that way, it will be created each time the wndproc is called... that's not creating it once.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  11. #11
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Create it in WM_CREATE.

    Code:
    LRESULT CALLBACK MyProc( ... )
    {
      static HBRUSH brush ;
    
      switch( msg )
      {
        case WM_CREATE:
        {
           brush = CreateSolidBrush(blah) ;
           return 0 ;
        }
      }
    }
    Edit:

    Actually, since static variables are intialized using the first statement, you don't even need to use WM_CREATE.

    Example:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int bar()
    {
      printf( "calling bar\n" ) ;  
      return 1 ;
    }
    
    void foo()
    {
      static int i = bar() ;   // bar() is called only once, although foo() is called 3 times
      printf( "calling foo\n" ) ;  
    }
    
    int main(int argc, char *argv[])
    {
      foo() ;
      foo() ;
      foo() ;
      getchar() ;
      return 0;
    }
    Last edited by Dante Shamest; 03-02-2004 at 01:01 PM.

  12. #12
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    can you please define what is a static variable, and how do I free it?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  13. #13
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    nm... i've looked it up.

    a static variable is a local variable, it's a part of the function, it could only be initialized once, and it lasts till the program is terminated.


    Thanks to everyone who answered this thread.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 32 bit color depth bitmap problem with XP
    By kalabala in forum C++ Programming
    Replies: 0
    Last Post: 12-22-2008, 06:56 AM
  2. OpenGL Color update problem
    By arifin in forum C++ Programming
    Replies: 0
    Last Post: 07-22-2008, 01:17 AM
  3. Changing static control text color
    By maxorator in forum Windows Programming
    Replies: 6
    Last Post: 11-03-2005, 10:03 AM
  4. very weird .h problem
    By royuco77 in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2005, 08:55 AM
  5. get problem on text color
    By beely in forum C Programming
    Replies: 15
    Last Post: 01-18-2003, 03:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21