Win32 (GDI) - set opacity to STATIC Control

I am using C - (NO MFC or GDI +) : -)

I want to set the opacity of my child window to say 100 (my child window is a control STATIC

). I was wondering if this is possible, and if so, someone can point me in the right direction on how to do this.

Here's my setup:

I create a parent window like this:

HWND     hWnd;
WNDCLASS wndCls_s;


wndCls_s.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wndCls_s.lpfnWndProc   = MainWndProc;
wndCls_s.cbClsExtra    = 0;
wndCls_s.cbWndExtra    = 0;
wndCls_s.hInstance     = hInstance;
wndCls_s.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_BSN_64));
wndCls_s.hCursor       = LoadCursor(NULL, IDC_ARROW);
wndCls_s.hbrBackground = CreateSolidBrush(RGB(0, 0, 0));
wndCls_s.lpszMenuName  = NULL;
wndCls_s.lpszClassName = pszCName;

if (RegisterClass(&wndCls_s) == 0)
    return EXIT_FAILURE;


/* Creating Window */
hWnd = CreateWindow(
    pszCName, pszCName,
    WS_VISIBLE | WS_POPUP | WS_SYSMENU | WS_CLIPCHILDREN,
    0, 0, WND_WIDTH, WND_HEIGHT,
    NULL, NULL, hInstance, NULL);

      

In my MainWndProc

:

case WM_CREATE: 
    {   
    HWND hWndChild = CreateWindow(
        L"STATIC", (LPCTSTR) NULL,
        WS_CHILD | WS_VISIBLE,
        10, 10, 110, 110,
        hWnd, (HMENU) (int) 10000, 
        g_hInst, NULL);
    }
    break;
case WM_CTLCOLORSTATIC:
    {
    COLORREF dwColor; 

    dwColor = RGB(255, 0, 0);
    hDC = (HDC) wParam;

    //SetBkColor(hDC, dwColor);
    SetBkMode(hDC, TRANSPARENT);

    /*
    This is not going to work for child window
    SetWindowLong(
        hWnd, GWL_EXSTYLE,
        GetWindowLong((HWND)lParam, GWL_EXSTYLE) & ~WS_EX_LAYERED);

        SetLayeredWindowAttributes(
            (HWND)lParam, 0, 100, LWA_ALPHA);
        RedrawWindow((HWND)lParam, NULL, NULL, RDW_ERASE | RDW_INVALIDATE);
    */

    if (g_hBrushRed == NULL)
        g_hBrushRed = CreateSolidBrush(dwColor);
    }
    return (INT_PTR)g_hBrushRed;

      

+3


source to share


1 answer


Why are you turning on transparency with TRANSPARENT

when you want to return a valid background brush? You don't have to SetBkMode

, and then your red brush will be used by the control.



+2


source







All Articles