Win32 (GDI): establecer la opacidad del control ESTÁTICO

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

What I want is to set the opacity of my child window to let say 100 (my child window is a STATIC control). I was wondering if this is even possible and if so can someone please point me to the right direction on how to do it.

He aquí mi arreglo:

I create my Parent window as follow:

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);

En mi 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;

preguntado el 10 de marzo de 12 a las 01:03

Not possible, only top-level windows can have the WS_EX_LAYERED style. I suspect you're looking for WS_EX_TRANSPARENT. -

@HansPassant, are you suggesting to use SetWindowLong on child window with WS_EX_TRANSPARENT, and then process the WM_CTLCOLORSTATIC? Then how can I set the opacity? -

1 Respuestas

Why do you enable transparency with TRANSPARENT if you are going to return a valid brush for the background? You don't need SetBkMode and your red brush will be used by the control then.

respondido 11 mar '12, 11:03

Can you please explain more. Removing SetBkMode and returning the red brush won't transparency. - markfw

So you should have red opaque background, right? And what are you trying to reach, semi-transparent red background in term of alpha-blending, 100/255 blend with underlying image? - romano r

Yes, that's exactly what I want. So I can't return Solid Brush nor NULL brush (makes it fully transparent), so can please show me how to create transparent brush (do we even have that)? - markfw

You cannot do it in straightforward way buy specifying brush transparency level. What you can however do is to create/use temporary in-memory DC, request underlying windows(s) to paint into that DC, then paint your control image into another DC and then AlphaBlend one into the other coming up with final image for opaque presentation on the real DC. - romano r

En realidad no necesitas WM_CTLCOLORSTATIC at all anymore. It is a helper for default painting and you are going to take over drawing completely. You handle WM_PAINT (or control dependent custom-drawing, which is perhaps preferable), you can do everything there. If you are however going to pre-create some resources for reuse, such as bitmaps and device contexts, it would be a good idea to create them in WM_CREATE and destroy them in WM_DESTROY. The painting will take place in WM_PAINT anyway. You will also want to override WM_ERASEBACKGROUND with an empty handler. - romano r

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.