Establecer una ventana transparente

Nov 24 2020

He estado intentando configurar la ventana de BlueStacks como transparente:

DWORD MakeWindowTransparent(HWND hWnd, unsigned char factor)
{
    /* First, see if we can get the API call we need. If we've tried
     * once, we don't need to try again. */
    if (!initialized)
    {
        HMODULE hDLL = LoadLibrary(L"user32");

        pSetLayeredWindowAttributes =
            (PSLWA)GetProcAddress(hDLL, "SetLayeredWindowAttributes");

        initialized = TRUE;
    }

    if (pSetLayeredWindowAttributes == NULL)
        return FALSE;

    /* Windows need to be layered to be made transparent. This is done
     * by modifying the extended style bits to contain WS_EX_LAYARED. */
    SetLastError(0);

    auto winlong = SetWindowLong(hWnd,
    GWL_EXSTYLE,
    GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);

    if ((winlong == 0) && (GetLastError() != 0)) { 
        auto error = GetLastErrorAsString(); 
        return FALSE;
    } 

    if (!pSetLayeredWindowAttributes(hWnd,RGB(255, 255, 255),factor, LWA_COLORKEY | LWA_ALPHA))
    { 
        auto error = GetLastErrorAsString(); 
        return FALSE; 
    } 

    return TRUE;
}


int main() {
  HWND hWnd = FindWindowA(NULL, L"BlueStacks");
  MakeWindowTransparent(hWnd, 0);
}

BlueStacks puede ejecutarse opengly directx, he probado el código anterior, usando ambos modos.

MakeWindowTransparent está devolviendo 0

pSetLayeredWindowAttributes auto error = GetLastErrorAsString();de error devuelto es: wrong parameter.

Probé el código con otras ventanas OpenGL y no se detuvo en ninguno de los errores, también la ventana se volvió transparente correctamente.

Alguna información que he recopilado sobre la ventana:

Agradezco cualquier ayuda.

Respuestas

1 VladFeinstein Nov 24 2020 at 06:18

¿Por qué usa ambas banderas LWA_COLORKEY | LWA_ALPHA? Si está especificando un color RGB(255, 255, 255), solo use LWA_COLORKEY.

Además, ¿por qué te estás metiendo GetProcAddress? SetLayeredWindowAttributesestá disponible a partir de Windows 2000; ¿Está apuntando a plataformas más antiguas?

No estoy familiarizado con BlueStacks , pero SetLayeredWindowAttributes para hacer una ventana transparente solo funciona una parte del tiempo, lo que sugiere que SetLayeredWindowAttributesno funciona Direct3D; ¿Sabes si usa BlueStacksDirect3D ?

Para OpenGLver esto: ¿Cómo hacer un contexto de renderizado OpenGL con fondo transparente?

ACTUALIZACIÓN Estaba jugando con BlueStacks y pensé que puede volver a vincularlo a otra ventana (eso no se recomienda, ya que necesitará manejar algunos casos extremos). De todos modos, usé una Notepadventana existente y pude establecer alfa en ella, y eso afectó a la ventana secundaria de BlueStacks :

int main() {
  HWND hWnd = FindWindow(NULL, L"BlueStacks");
  HWND hWndN = FindWindow(NULL, L"Untitled - Notepad");
  ::SetParent(hWnd, hWndN);

  auto winlong1 = SetWindowLongPtr(hWnd, GWL_EXSTYLE, 0);
  auto winlong2 = SetWindowLongPtr(hWnd, GWL_STYLE, WS_CHILD | WS_VISIBLE);
  ::SetWindowPos(hWnd, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOSIZE | SWP_NOZORDER | SWP_NOREDRAW | SWP_NOACTIVATE);

  MakeWindowTransparent(hWndN, 200);
}

También he limpiado su función para deshacerme de GetProcAddress:

DWORD MakeWindowTransparent(HWND hWnd, unsigned char factor)
{
  auto winlong = SetWindowLong(hWnd,
    GWL_EXSTYLE,
    GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);

  if ((winlong == 0) && (GetLastError() != 0)) {
    return FALSE;
  }

  if (!SetLayeredWindowAttributes(hWnd, 0, factor, LWA_ALPHA))
  {
    return FALSE;
  }

  return TRUE;
}