How to enable modern look and feel of tool bar for Win32 Application?

后端 未结 1 1052
醉话见心
醉话见心 2021-02-06 17:32

Recently I started learning GUI programming based on Win32 API. When I add tool bar control (from comctl32.lib) to my simple application I find it looks flat wh

1条回答
  •  北恋
    北恋 (楼主)
    2021-02-06 17:52

    Create Toolbar first and then Rebar.

    the rbBand.hbmBack must be set to NULL to get modern look toolbar, or remove RBBIM_BACKGROUND flag from the code below.

    Here's how to create rebar:

    HWND WINAPI CreateRebar (HWND hwndOwner)
    {
        REBARINFO     rbi;
        REBARBANDINFO rbBand;
        RECT          rc;
        HWND   hwndCB, hwndRB;
        DWORD  dwBtnSize;
    
        hwndRB = CreateWindowExW(WS_EX_TOOLWINDOW,
            REBARCLASSNAME,
            NULL,
            WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS |
            WS_CLIPCHILDREN | RBS_VARHEIGHT |
            CCS_NODIVIDER,
            0, 0, 0, 0,
            hwndOwner,
            NULL,
            GetModuleHandleW(NULL),
            NULL);
        if (!hwndRB)
            return NULL;
        // Initialize and send the REBARINFO structure.
        rbi.cbSize = sizeof(REBARINFO);  // Required when using this
        // structure.
        rbi.fMask = 0;
        rbi.himl = (HIMAGELIST)NULL;
        if (!SendMessage(hwndRB, RB_SETBARINFO, 0, (LPARAM)&rbi))
            return NULL;
        // Initialize structure members that both bands will share.
        rbBand.cbSize = sizeof(REBARBANDINFO);  // Required
        rbBand.fMask = RBBIM_COLORS | RBBIM_TEXT | RBBIM_BACKGROUND |
            RBBIM_STYLE | RBBIM_CHILD | RBBIM_CHILDSIZE |
            RBBIM_SIZE;
        rbBand.fStyle = RBBS_CHILDEDGE | RBBS_FIXEDBMP;
        rbBand.hbmBack = NULL;  //
        // Create the combo box control to be added.
        hwndCB = CreateWindowW(TEXT("COMBOBOX"), NULL,
            WS_CHILD | WS_VISIBLE | CBS_HASSTRINGS | CBS_DROPDOWNLIST,
            410, 20, 120, 110, (HWND) NULL, NULL, NULL, NULL);;
        // Set values unique to the band with the combo box.
        GetWindowRect(hwndCB, &rc);
        rbBand.lpText = "Combo Box";
        rbBand.hwndChild = hwndCB;
        rbBand.cxMinChild = 0;
        rbBand.cyMinChild = rc.bottom - rc.top;
        rbBand.cx = 200;
    
        // Add the band that has the combo box.
        SendMessage(hwndRB, RB_INSERTBAND, (WPARAM)-1, (LPARAM)&rbBand);
    
    
        // Get the height of the toolbar.
        dwBtnSize = SendMessage(toolbar1, TB_GETBUTTONSIZE, 0, 0);
    
        // Set values unique to the band with the toolbar.
        rbBand.lpText = "Tool Bar";
        rbBand.hwndChild = toolbar1;
        rbBand.cxMinChild = 0;
        rbBand.cyMinChild = HIWORD(dwBtnSize);
        rbBand.cx = 250;
    
        // Add the band that has the toolbar.
        SendMessage(hwndRB, RB_INSERTBAND, (WPARAM)-1, (LPARAM)&rbBand);
        return (hwndRB);
    }
    

    0 讨论(0)
提交回复
热议问题