none
How does explorer change the color of it's title bar when dark mode it enabled? RRS feed

  • Question

  • I want to achieve the same look that explorer does when the windows dark mode is enabled.Explorer title bar is dark

    I want to the same with my app. I have removed the normal title bar and extended the title bar. I can draw on the title bar, but if I try, it will draw over the caption buttons. I want to change the behind color.

    Here is my code:

    #include <Windows.h> 
    #include <Windowsx.h> //for `GET_X_LPARAM` etc.
    ...
    LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        static int cy_titlebar_ = 100;
        static RECT border_thickness;
    
        LRESULT result;
        if(DwmDefWindowProc(hWnd, msg, wParam, lParam, &result))
            return result;
    
        switch(msg)
        {
        case WM_CREATE:
        {
            //find border thickness
            border_thickness = { 0 };
            if(GetWindowLongPtr(hWnd, GWL_STYLE) & WS_THICKFRAME)
            {
                AdjustWindowRectEx(&border_thickness,
                    GetWindowLongPtr(hWnd, GWL_STYLE) & ~WS_CAPTION, FALSE, NULL);
                border_thickness.left *= -1;
                border_thickness.top *= -1;
            }
            else if(GetWindowLongPtr(hWnd, GWL_STYLE) & WS_BORDER)
            {
                border_thickness = { 1,1,1,1 };
            }
    
            MARGINS margins = { 0, 0, cy_titlebar_, 0 };
            DwmExtendFrameIntoClientArea(hWnd, &margins);
            SetWindowPos(hWnd, NULL, 0, 0, 0, 0, 
                        SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);
            return 0;
        }
    
        case WM_NCCALCSIZE:
        {
            if(wParam)
            {
                RECT& r = reinterpret_cast<LPNCCALCSIZE_PARAMS>(lParam)->rgrc[0];
                r.left += border_thickness.left;
                r.right -= border_thickness.right;
                r.bottom -= border_thickness.bottom;
                return 0;
            }
            break;
        }
    
        case WM_NCHITTEST:
        {
            result = DefWindowProc(hWnd, msg, wParam, lParam);
            if(result == HTCLIENT)
            {
                POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
                ScreenToClient(hWnd, &pt);
                if(pt.y < border_thickness.top) return HTTOP;
                if(pt.y < cy_titlebar_)  return HTCAPTION;
            }
            return result;
        }
    
        case WM_PAINT:
        {
            PAINTSTRUCT ps;
            auto hdc = BeginPaint(hWnd, &ps);
    
            //paint opaque:
            RECT rc{ 0, 0, 100, cy_titlebar_ };
            BP_PAINTPARAMS params = { sizeof(params), BPPF_NOCLIP | BPPF_ERASE };
            HDC memdc;
            HPAINTBUFFER hbuffer = BeginBufferedPaint(
                        hdc, &rc, BPBF_TOPDOWNDIB, &params, &memdc);
    
            auto brush = CreateSolidBrush(RGB(0, 0, 0));
            FillRect(memdc, &rc, brush);
            DeleteObject(brush);
    
            SetBkMode(memdc, TRANSPARENT);
            DrawText(memdc, L"Opaque", -1, &rc, 0);
            BufferedPaintSetAlpha(hbuffer, &rc, 255);
            EndBufferedPaint(hbuffer, TRUE);
    
            EndPaint(hWnd, &ps);
            return 0;
        }
    
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        }
    
        return DefWindowProc(hWnd, msg, wParam, lParam);
    }

    How can I achieve the same explorer look?

    Thanks.

    Friday, September 6, 2019 5:57 PM

Answers

  • OK I found my answer.

    I used 

    DwmSetWindowAttribute(hwnd, 19, &(BOOL){1}, sizeof BOOL);

    to set the dark theme on the window. And if I want to use dark theme controls, I will use DarkMode_Explorer window theme with SetWindowTheme.

    Thanks anyways.

    • Marked as answer by NT_pro Wednesday, November 6, 2019 5:06 AM
    Wednesday, November 6, 2019 5:06 AM

All replies

  • Hi,

    Refer to Custom Window Frame Using DWM.

    I don't recommend custom borders and title repainting. It's really hard to do the right thing. The office just draws everything in the client area, but uses a normal border. In particular, minimizing positioning, maximizing and closing buttons is difficult because each window is different. Also consider accessibility, use larger fonts, and customize user settings.

    This case gives a another method using .png image.

    Best regards,

    Jeffrey


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.





    • Edited by Jeffrey Shao Monday, September 9, 2019 8:59 AM
    • Proposed as answer by Jeffrey Shao Friday, September 20, 2019 3:21 AM
    Monday, September 9, 2019 4:46 AM
  • OK I found my answer.

    I used 

    DwmSetWindowAttribute(hwnd, 19, &(BOOL){1}, sizeof BOOL);

    to set the dark theme on the window. And if I want to use dark theme controls, I will use DarkMode_Explorer window theme with SetWindowTheme.

    Thanks anyways.

    • Marked as answer by NT_pro Wednesday, November 6, 2019 5:06 AM
    Wednesday, November 6, 2019 5:06 AM