Thread: Static Text on Tab Controls

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    5

    Static Text on Tab Controls

    Hello Everybody.

    I currently have a tab control that has a static control that is made for each tab. Then each static text is then added onto the static control to make it possible for tab switching. The text can be drawn correctly, but I have a problem. The text has a static, gray-like, background that I am trying to remove. How would I go about doing that?

    My current code is:
    Code:
    hwnd_tabs_content[0] = CreateWindowEx(0,"STATIC","",SS_WHITERECT|WS_VISIBLE|WS_EX_TRANSPARENT|WS_CHILD|WS_EX_WINDOWEDGE,1,21,586,367,hwnd_tabs,NULL,g_hinst,NULL);
    HWND test=CreateWindowEx(0,"STATIC","asdf",WS_VISIBLE|WS_CHILD|WS_EX_TRANSPARENT,10,10,400,20,hwnd_tabs_content[0],NULL,g_hinst,NULL);
    SendMessage (test,WM_SETFONT,(WPARAM) GetStockObject(DEFAULT_GUI_FONT), 0);

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    I think that you have to ownerdraw the control to change the background color (with edit controls you can work with WM_CTLCOLOREDIT and WM_CTLCOLORSTATIC messages, but this don't apply to static controls). Just assign the SS_OWNERDRAW style and check the WM_DRAWITEM message on the control's parent window, the display it on your way:

    Code:
    case WM_DRAWITEM:
            {
            DRAWITEMSTRUCT *dis=(DRAWITEMSTRUCT*)lParam;
    
            char bff[100];
            int bkm;
            COLORREF cr;
            HFONT hf;
            
            SendMessage(dis->hwndItem,WM_GETTEXT,(WPARAM)sizeof(bff),(LPARAM)bff);
            FillRect(dis->hDC,&dis->rcItem,(HBRUSH)GetStockObject(WHITE_BRUSH));//use here the colorbrush you want
            DrawEdge(dis->hDC,&dis->rcItem,EDGE_SUNKEN,BF_RECT|BF_FLAT);
            
            bkm=SetBkMode(dis->hDC,TRANSPARENT);
            cr=SetTextColor(dis->hDC,0x00000000);
            hf=(HFONT)SelectObject(dis->hDC,GetStockObject(DEFAULT_GUI_FONT));
            
            DrawText(dis->hDC,bff,strlen(bff),&dis->rcItem,DT_TOP|DT_WORDBREAK);
            
            SetBkMode(dis->hDC,bkm);
            SetTextColor(dis->hDC,cr);
            SelectObject(dis->hDC,hf);
            }
        break;
    Only one point to be care: before finsh the operation, reset the HDC values to the originals (see the 3 last lines).

    Hope that helps
    Niara

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Another thing, the WS_EX_ styles should be applied as dwExStyle, not dwStyle:

    Code:
    CreateWindowEx(WS_EX_TRANSPARENT,"STATIC","",SS_WHITERECT|WS_VISIBLE...
    Niara

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    5
    Quote Originally Posted by Niara View Post
    I think that you have to ownerdraw the control to change the background color (with edit controls you can work with WM_CTLCOLOREDIT and WM_CTLCOLORSTATIC messages, but this don't apply to static controls). Just assign the SS_OWNERDRAW style and check the WM_DRAWITEM message on the control's parent window, the display it on your way:

    Code:
    case WM_DRAWITEM:
            {
            DRAWITEMSTRUCT *dis=(DRAWITEMSTRUCT*)lParam;
    
            char bff[100];
            int bkm;
            COLORREF cr;
            HFONT hf;
            
            SendMessage(dis->hwndItem,WM_GETTEXT,(WPARAM)sizeof(bff),(LPARAM)bff);
            FillRect(dis->hDC,&dis->rcItem,(HBRUSH)GetStockObject(WHITE_BRUSH));//use here the colorbrush you want
            DrawEdge(dis->hDC,&dis->rcItem,EDGE_SUNKEN,BF_RECT|BF_FLAT);
            
            bkm=SetBkMode(dis->hDC,TRANSPARENT);
            cr=SetTextColor(dis->hDC,0x00000000);
            hf=(HFONT)SelectObject(dis->hDC,GetStockObject(DEFAULT_GUI_FONT));
            
            DrawText(dis->hDC,bff,strlen(bff),&dis->rcItem,DT_TOP|DT_WORDBREAK);
            
            SetBkMode(dis->hDC,bkm);
            SetTextColor(dis->hDC,cr);
            SelectObject(dis->hDC,hf);
            }
        break;
    Only one point to be care: before finsh the operation, reset the HDC values to the originals (see the 3 last lines).

    Hope that helps
    Niara
    How would I go about implementing that code? I am not getting the WM_DRAWITEM message.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    "..I am not getting the WM_DRAWITEM message..."

    You have to set the 'SS_OWNERDRAW' style on the control declaration, and wait for the message on the controls parent window: you are appending the 'test' control to the 'hwnd_tabs_content[0]' window, so you have to search the message on its window procedure; as 'hwnd_tabs_content[0]' is a standard control you have to subclasse it to check for the WM_DRAWITEM message. Or till on ownerdraw mode you have to draw the entire control you also con subclasse that control instead searching for the message on its parent window.

    Niara
    Last edited by Niara; 04-18-2010 at 08:48 AM.

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

    From MSDN:

    "WM_CTLCOLORSTATIC
    A static control, or an edit control that is read-only or disabled, sends the WM_CTLCOLORSTATIC message to its parent window when the control is about to be drawn. By responding to this message, the parent window can use the specified device context handle to set the text and background colors of the static control. "

    This is why dialogs (containing the ctrls) are generally used for this type of thing (as a TAB ctrl does not have a callback but uses notify reflects for msg processing/handling).
    "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. Need Help in Compiling errors
    By sick in forum C Programming
    Replies: 2
    Last Post: 01-21-2010, 03:26 AM
  2. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  3. Transparent Static Controls, Non-MFC
    By X PaYnE X in forum Windows Programming
    Replies: 3
    Last Post: 08-06-2003, 10:32 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Setting static text color -- I know I'm doing something wrong
    By Garfield in forum Windows Programming
    Replies: 4
    Last Post: 01-26-2002, 01:59 PM