How to make the radio button background transparent

Ni_Yin 11 Reputation points
2021-11-18T07:56:18+00:00

I created a simple win32 program, first create a window, its background is black. Then create its child window, but the background color of the child window is gray, not transparent. How to deal with making it transparent?
150448-hello-2021-11-18-15-54-15.png

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,426 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,537 questions
0 comments No comments
{count} vote

5 answers

Sort by: Most helpful
  1. Castorix31 81,741 Reputation points
    2021-11-18T08:24:26.857+00:00

    Handle WM_CTLCOLORSTATIC
    Call SetBkMode with TRANSPARENT
    and
    return (LRESULT)GetStockObject( HOLLOW_BRUSH );

    Test :

    150554-transp.jpg

    2 people found this answer helpful.

  2. Junjie Zhu - MSFT 15,056 Reputation points Microsoft Vendor
    2021-11-18T10:08:18.583+00:00

    Hello,
    Welcome to Microsoft Q&A!

    You can set the control's background color to be the same as the background.

    case WM_CTLCOLORSTATIC:  
       
            if ((HWND)lParam == GetDlgItem(hWnd, IDB_RES))  
            {  
                HWND hbn = (HWND)lParam;  
                HDC hdc = (HDC)wParam;  
                RECT rc;  
                TCHAR text[64];  
       
                GetWindowText(hbn, text, 63);  
                GetClientRect(hbn, &rc);  
                SetTextColor(hdc, RGB(255, 255, 255));  
                SetBkMode(hdc, TRANSPARENT);  
                DrawText(hdc, text, _tcslen(text), &rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);  
       
                return (INT_PTR)CreateSolidBrush(RGB(0,0,255));  
            }  
            break;  
      
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  3. Petrus 【KIM】 456 Reputation points
    2021-11-18T10:27:12.423+00:00

    Well.... You did it...but remove some code....then retry...

    BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)  
    {  
       hInst = hInstance; // Store instance handle in our global variable  
      
       HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW | WS_VISIBLE,  
          CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);  
      
       if (!hWnd)  
       {  
          return FALSE;  
       }  
      
       HWND hWnds[2][4];  
       hWnds[1][0] = CreateWindowEx(WS_EX_TRANSPARENT, L"BUTTON", L"AAAAA", WS_CHILD | BS_GROUPBOX | BS_TEXT | WS_GROUP | WS_VISIBLE, 10, 10, 120, 125, hWnd, (HMENU)0, hInstance, nullptr);  
       hWnds[0][0] = CreateWindowEx(0L, L"BUTTON", L"BBBBB", WS_CHILD | BS_AUTORADIOBUTTON | WS_VISIBLE, 10, 30, 120, 35, hWnd, (HMENU)0, hInstance, nullptr);  
       hWnds[0][1] = CreateWindowEx(0L, L"BUTTON", L"CCCCC", WS_CHILD | BS_AUTORADIOBUTTON | WS_VISIBLE, 10, 65, 120, 35, hWnd, (HMENU)0, hInstance, nullptr);  
       hWnds[0][2] = CreateWindowEx(0L, L"BUTTON", L"DDDDD", WS_CHILD | BS_AUTORADIOBUTTON | WS_VISIBLE, 10, 100, 120, 35, hWnd, (HMENU)0, hInstance, nullptr);  
       hWnds[0][3] = CreateWindowEx(0L, L"STATIC", L"EEEEE", WS_CHILD | WS_VISIBLE, 0, 150, 120, 35, hWnd, (HMENU)0, hInstance, nullptr);  
      
       ShowWindow(hWnd, nCmdShow);  
       UpdateWindow(hWnd);  
      
       return TRUE;  
    }  
      
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)  
    {  
        switch (message)  
        {  
    ......  
      
        case WM_CTLCOLORSTATIC:  
        {  
            HDC hdc = (HDC)wParam;  
            SetBkMode(hdc, TRANSPARENT);  
            SetTextColor(hdc, RGB(0x00, 0x00, 0xFF));     
            return (LRESULT)GetStockObject(NULL_BRUSH);  
        }  
    ......  
      
        default:  
            return DefWindowProc(hWnd, message, wParam, lParam);  
        }  
        return 0;  
    }  
    

    150593-image.png

    1 person found this answer helpful.
    0 comments No comments

  4. Petrus 【KIM】 456 Reputation points
    2021-11-18T08:38:59.113+00:00

    Show me your code....
    Your group box is suspicious....

    IDD_ABOUTBOX DIALOGEX 0, 0, 269, 155
    STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
    CAPTION "About MFCApplication1"
    FONT 8, "MS Shell Dlg", 0, 0, 0x1
    BEGIN
    GROUPBOX "Static",IDC_STATIC,43,15,194,102,0,WS_EX_TRANSPARENT
    ICON IDR_MAINFRAME,IDC_STATIC,14,14,20,20
    LTEXT "MFCApplication1, Version 1.0",IDC_STATIC,57,34,114,8,SS_NOPREFIX
    LTEXT "Copyright (C) 2021",IDC_STATIC,57,50,114,8
    DEFPUSHBUTTON "OK",IDOK,212,134,50,14,WS_GROUP
    CONTROL "Radio1",IDC_RADIO1,"Button",BS_AUTORADIOBUTTON,55,66,42,10
    CONTROL "Radio2",IDC_RADIO2,"Button",BS_AUTORADIOBUTTON,55,84,38,10
    CONTROL "Radio3",IDC_RADIO3,"Button",BS_AUTORADIOBUTTON,55,102,38,10
    END

    150556-image.png


  5. Ni_Yin 11 Reputation points
    2021-11-18T08:50:10.69+00:00

    @Castorix31

    #include "framework.h"
    #include "test2.h"
    if defined M_IX86
    #pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language=''"")
    elif defined M_X64
    #pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language=''"")
    else
    #pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='' publicKeyToken='6595b64144ccf1df' language=''"")
    endif
    
    HINSTANCE hInst; // 当前实例WCHAR szTitleMAX_LOADSTRING=L"hello"; // 标题栏文本WCHAR szWindowClassMAX_LOADSTRING=L"hellos"; // 主窗口类名bool AppsUseLightTheme = true;bool AutoChangeThemeColor = true;unsigned char FocusMAX_FOCUS = { 0, 0 };unsigned char MaxFocusMAX_FOCUS - 1 = { 3 };
    ATOM MyRegisterClass(HINSTANCE hInstance);int InitInstance(HINSTANCE, int);LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);void AutoChangeColor(HWND hWnd);void ColorChangeToLight(HWND hWnd);void ColorChangeToDark(HWND hWnd);
    int APIENTRY wWinMain(In HINSTANCE hInstance,In_opt HINSTANCE hPrevInstance,In LPWSTR lpCmdLine,In int nCmdShow){
    MainRectangle a;  
    a.mainrectangle.SetHight(11);  
    a.mainrectangle.SetWidth(23);  
    if (a.mainrectangle.GetArea() == 11 * 23)  
    {  
        MessageBox(NULL, L"提示", L"提示", MB_OK);  
    }  
    UNREFERENCED_PARAMETER(hPrevInstance);  
    UNREFERENCED_PARAMETER(lpCmdLine);  
    // TODO: 在此处放置代码。  
    if (!MyRegisterClass(hInstance)) {  
        return FALSE;  
    }  
    // 执行应用程序初始化:  
    return InitInstance(hInstance, nCmdShow);  
    }
    ATOM MyRegisterClass(HINSTANCE hInstance){WNDCLASSEXW wcex;
    wcex.cbSize = sizeof(WNDCLASSEX);  
    wcex.style          = CS_HREDRAW | CS_VREDRAW|CS_OWNDC;  
    wcex.lpfnWndProc    = WndProc;  
    wcex.cbClsExtra     = 0;  
    wcex.cbWndExtra     = 0;  
    wcex.hInstance      = hInstance;  
    wcex.hIcon          = LoadIcon(hInstance, IDI_APPLICATION);  
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);  
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);  
    wcex.lpszMenuName   = szWindowClass;  
    wcex.lpszClassName  = szWindowClass;  
    wcex.hIconSm        = LoadIcon(wcex.hInstance, IDI_APPLICATION);  
    return RegisterClassExW(&wcex);  
    }
    int InitInstance(HINSTANCE hInstance, int nCmdShow){
    HWND hWnd = CreateWindowEx(NULL, szWindowClass, szTitle, WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);  
    if (!hWnd)  
    {  
        return FALSE;  
    }  
    HWND hWnds[MAX_FOCUS][MAX_WINDOW];  
    hWnds[MAX_FOCUS - 1][0] = CreateWindowEx(NULL, L"button", L"颜色模式", WS_CHILD | WS_VISIBLE | BS_GROUPBOX, 0, 0, 120, 125, hWnd, HMENU(10), hInstance, nullptr);  
    hWnds[0][0] = CreateWindowEx(NULL, L"button", L"亮色模式", WS_CHILD | BS_AUTORADIOBUTTON | WS_VISIBLE | WS_TABSTOP, 0, 20, 120, 35, hWnd, HMENU(LightMode), hInstance, nullptr);  
    hWnds[0][1] = CreateWindowEx(NULL, L"button", L"暗色模式", WS_CHILD | BS_AUTORADIOBUTTON | WS_VISIBLE | WS_TABSTOP, 0, 55, 120, 35, hWnd, HMENU(DarkMode), hInstance, nullptr);  
    hWnds[0][2] = CreateWindowEx(NULL, L"button", L"自动模式", WS_CHILD | BS_AUTORADIOBUTTON | WS_VISIBLE | WS_TABSTOP, 0, 90, 120, 35, hWnd, HMENU(AutoColor), hInstance, nullptr);  
    hWnds[0][3] = CreateWindowEx(NULL, L"static", L"自动模式", WS_CHILD | WS_VISIBLE, 0, 130, 120, 35, hWnd, nullptr, hInstance, nullptr);  
    ShowWindow(hWnd, nCmdShow);  
    UpdateWindow(hWnd);  
    SetFocus(hWnds[Focus[0]][Focus[1]]);  
    MSG msg;  
    // 主消息循环:  
    while (GetMessage(&msg, nullptr, 0, 0))  
    {  
        TranslateMessage(&msg);  
        if (msg.message == WM_KEYDOWN) {  
            if (msg.wParam == VK_DOWN) {  
                if (Focus[1] <= MaxFocus[Focus[0]] - 2) {  
                    Focus[1]++;  
                }  
                else {  
                    Focus[1] = 0;  
                }  
                SetFocus(hWnds[Focus[0]][Focus[1]]);  
            }  
            if (msg.wParam == VK_UP) {  
                if (Focus[1] >= 1) {  
                    Focus[1]--;  
                }  
                else {  
                    Focus[1] = MaxFocus[Focus[0]] - 1;  
                }  
                SetFocus(hWnds[Focus[0]][Focus[1]]);  
            }  
            if (msg.wParam == VK_ESCAPE) {  
                PostMessage(hWnd, WM_DESTROY, NULL, NULL);  
            }  
        }  
        DispatchMessage(&msg);  
        if (AutoChangeThemeColor)  
        {  
            AutoChangeColor(hWnd);  
        }  
    }  
    return (int)msg.wParam;  
    }
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){switch (message){case WM_COMMAND:{int wmId = LOWORD(wParam);switch (wmId){case LightMode:AutoChangeThemeColor = false;ColorChangeToLight(hWnd);AppsUseLightTheme = true;break;case DarkMode:AutoChangeThemeColor = false;ColorChangeToDark(hWnd);AppsUseLightTheme = false;break;case AutoColor://DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);AutoChangeThemeColor = true;break;default:return DefWindowProc(hWnd, message, wParam, lParam);}}break;case WM_PAINT:{PAINTSTRUCT ps;InvalidateRect(hWnd, NULL, TRUE);HDC hdc = BeginPaint(hWnd, &ps);EndPaint(hWnd, &ps);}break;
    case WM_CTLCOLORSTATIC:  
        if (AppsUseLightTheme) {  
            SetTextColor((HDC)wParam, RGB(0, 0, 0));  
        }  
        else {  
            SetTextColor((HDC)wParam, RGB(255, 255, 255));  
        }  
        SetBkMode((HDC)wParam, TRANSPARENT);  
        return (BOOL)((HBRUSH)GetStockObject(NULL_BRUSH));  
        break;  
    case WM_DESTROY:  
        PostQuitMessage(0);  
        break;  
    default:  
        return DefWindowProc(hWnd, message, wParam, lParam);  
        }  
        return 0;  
    }  
    // “关于”框的消息处理程序。INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam){UNREFERENCED_PARAMETER(lParam);switch (message){case WM_INITDIALOG:return (INT_PTR)TRUE;
    case WM_COMMAND:  
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)  
        {  
            EndDialog(hDlg, LOWORD(wParam));  
            return (INT_PTR)TRUE;  
        }  
        break;  
    }  
    return (INT_PTR)FALSE;  
    }
    void AutoChangeColor(HWND hWnd){HKEY phkResult;TCHAR Result=L"\0";DWORD lpcbData=sizeof(Result);if (RegOpenKeyEx(HKEY_CURRENT_USER, L"SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize", NULL, KEY_EXECUTE, &phkResult)== ERROR_SUCCESS){if (RegQueryValueEx(phkResult, L"AppsUseLightTheme", NULL, NULL, (LPBYTE)Result, &lpcbData) == ERROR_SUCCESS){if (!(!Result0) != AppsUseLightTheme){AppsUseLightTheme = !AppsUseLightTheme;if (AppsUseLightTheme){ColorChangeToLight(hWnd);}else{ColorChangeToDark(hWnd);}}}}RegCloseKey(phkResult);}
    void ColorChangeToLight(HWND hWnd){SetClassLong(hWnd, GCL_HBRBACKGROUND, (long)(HBRUSH)GetStockObject(WHITE_BRUSH));PostMessage(hWnd, WM_PAINT, NULL, NULL);}
    void ColorChangeToDark(HWND hWnd){SetClassLong(hWnd, GCL_HBRBACKGROUND, (long)CreateSolidBrush(RGB(25, 25, 25)));PostMessage(hWnd, WM_PAINT, NULL, NULL);}
    
    0 comments No comments