CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2019
    Posts
    7

    Change the background for each tab

    I decided to study the work of tabs in WinAi. Add a tabcontol in this way.
    Code:
    #include <Windows.h>
    #include <commctrl.h>
    
    static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
      
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
        WNDCLASS wc;
        LPCSTR applicationName = "Tab";
     
        wc.style = CS_HREDRAW | CS_VREDRAW;
        wc.lpfnWndProc = WndProc;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hInstance = hInstance;
        wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        wc.lpszMenuName = NULL;
        wc.lpszClassName = applicationName;
        
        if(!RegisterClass(&wc))
        {
            MessageBox(NULL, "Cannot register class", "Error", MB_OK | MB_ICONERROR);
            return false;
        }
     
        HWND hwnd = CreateWindow(applicationName, applicationName, WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                                478, 178, 500, 500, NULL, NULL, hInstance, NULL);
     
        if(!hwnd)
        {
            MessageBox(NULL, "Cannot create main window", "Error", MB_OK | MB_ICONERROR);
            return false;
        }
        
    	HWND htabcontrol = CreateWindow(WC_TABCONTROL, NULL, WS_CHILD | WS_VISIBLE, 111, 111,
                                280, 282, hwnd, NULL, hInstance, NULL);
        HWND button = CreateWindow("button", "Button 1", WS_CHILD | WS_VISIBLE, 150, 150,
                                70, 20, hwnd, NULL, hInstance, NULL);
    	
        TC_ITEM tabitem;
        tabitem.mask = TCIF_TEXT;
        tabitem.pszText = "Tab 1";
        SendMessage(htabcontrol, TCM_INSERTITEM, 0, LPARAM(&tabitem));
        tabitem.pszText = "Tab 2";
        SendMessage(htabcontrol, TCM_INSERTITEM, 1, LPARAM(&tabitem));
    
        MSG msg;
     
        while(GetMessage(&msg, 0, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        
        return 0;
    }
     
     
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {   
        switch(msg)
        {
            case WM_DESTROY:
            {
                PostQuitMessage(0);
                return 0;
            }
    
            case WM_CLOSE:
            {
                DestroyWindow(hwnd);
                return 0;
            }
     
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);        
        }
    	return 0;
    }
    Also I added the manifest to change the design on XP style. I want to change the tabs background (highlighted in red in the image) to gray. What needs to be added to my code to make it happen?
    WhiteTab.png

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,835

    Re: Change the background for each tab

    Usually, you would have a separate child dialog (or other appropriate window) for each tab and when a particular tab is selected, display the appropriate child control over the client area of the tab control. You wouldn't usually write/create anything on the tab client area directly. There's plenty of info about how this is done on the internet.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jan 2019
    Posts
    7

    Re: Change the background for each tab

    Quote Originally Posted by 2kaud View Post
    Usually, you would have a separate child dialog (or other appropriate window) for each tab and when a particular tab is selected, display the appropriate child control over the client area of the tab control. You wouldn't usually write/create anything on the tab client area directly. There's plenty of info about how this is done on the internet.
    Oh, you probably didn't understand me. I have not problem with the layout of elements on the tabs. I need to change the background color of the tab, that is circled in red on the screen. I want the background color be gray (240, 240, 240), not white.
    If I misunderstood you, please throw off an example from Google.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,407

    Re: Change the background for each tab

    Quote Originally Posted by Kasfong View Post
    Oh, you probably didn't understand me. I have not problem with the layout of elements on the tabs. I need to change the background color of the tab, that is circled in red on the screen. I want the background color be gray (240, 240, 240), not white.
    If I misunderstood you, please throw off an example from Google.
    In your case the mentioned by 2kaud "child control over the client area of the tab control" could be a dialog or property page window.
    so you have to change the BKcolor for this dialog or property page window.
    Last edited by VictorN; January 31st, 2019 at 09:16 AM. Reason: spellcheck
    Victor Nijegorodov

  5. #5
    Join Date
    Jan 2019
    Posts
    7

    Re: Change the background for each tab

    Quote Originally Posted by VictorN View Post
    In your case the mentioned by 2kaud "child control over the client area of the tab control" could be a dialog or property page window.
    so you have to chane the BKcolor for this dialog or property page window.
    I am a newbie with WinApi, so correct whether I understood correctly.
    We create a dialog box in the resource file (in the case of Visual Studio), which, depending on the tabs, are put in the right place. For each tab we create our own window, which we display from resources as necessary. And the background color of the tab change through the background of the dialog box.
    Thus, there is no method like SetColor(hwndTabControl, RGB(240, 240, 240))?

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,407

    Re: Change the background for each tab

    Quote Originally Posted by Kasfong View Post
    I am a newbie with WinApi, so correct whether I understood correctly.
    We create a dialog box in the resource file (in the case of Visual Studio), which, depending on the tabs, are put in the right place. For each tab we create our own window, which we display from resources as necessary. And the background color of the tab change through the background of the dialog box.
    Thus, there is no method like SetColor(hwndTabControl, RGB(240, 240, 240))?
    With the code like
    Code:
    SetColor(hwndTabControl, RGB(240, 240, 240))?[/QUOTE]
    you are trying to set the color of the tabcontrol window, however your goal is to set the color of the dialog box window that is placed below your tabcontrol.
    Victor Nijegorodov

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Change the background for each tab

    Look at this another way. Change the background color to
    Code:
    wc.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
    Then see what happens.

  8. #8
    Join Date
    Jan 2019
    Posts
    7

    Re: Change the background for each tab

    Quote Originally Posted by VictorN View Post
    With the code like
    Code:
    SetColor(hwndTabControl, RGB(240, 240, 240))?
    you are trying to set the color of the tabcontrol window, however your goal is to set the color of the dialog box window that is placed below your tabcontrol.
    I mean, there is no other way to change the background of an empty tab box except for inserting a dialog box.

    Quote Originally Posted by Arjay View Post
    Look at this another way. Change the background color to
    Code:
    wc.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
    Then see what happens.
    In relation to the tabbox, nothing has changed. The background of the window turned dark gray and the background of the tab remained white.

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,835

    Re: Change the background for each tab

    I mean, there is no other way to change the background of an empty tab box except for inserting a dialog box.
    You're not really understanding a tab control. The control has 2 or more tabs (could be 1 but then what's the point?). You get a notification when a tab is selected. As part of the notification code you show the appropriate child control (eg dialog, property etc) over the client area of the tab control (adjusting size as needed). The user doesn't see the client area of the tab control and you don't directly create controls for this area or draw on it etc. In your example in post #1. the button is a child of the main window, not the tab control. In some simple cases you can just use a single child static control that is positioned over the tab client area. For an example of this, see https://docs.microsoft.com/en-us/win...he-main-window
    This is a good starting point to learn about tab controls. For a detailed reference re tab control see https://docs.microsoft.com/en-us/win...trol-reference
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    Join Date
    Jan 2019
    Posts
    7

    Re: Change the background for each tab

    Quote Originally Posted by 2kaud View Post
    You're not really understanding a tab control. The control has 2 or more tabs (could be 1 but then what's the point?). You get a notification when a tab is selected. As part of the notification code you show the appropriate child control (eg dialog, property etc) over the client area of the tab control (adjusting size as needed). The user doesn't see the client area of the tab control and you don't directly create controls for this area or draw on it etc. In your example in post #1. the button is a child of the main window, not the tab control. In some simple cases you can just use a single child static control that is positioned over the tab client area. For an example of this, see https://docs.microsoft.com/en-us/win...he-main-window
    This is a good starting point to learn about tab controls. For a detailed reference re tab control see https://docs.microsoft.com/en-us/win...trol-reference
    Thanks for clarifying. I really don’t know much about it, because I started study it quite recently.
    In the example, the button is not part of the tabcontrol, it's only for checking the work of the manifest and design. All I wanted was for that.Untitled-1.jpg

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured