Definir uma janela transparente

Nov 24 2020

Tenho tentado definir a janela do 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 pode ser executado em opengle directx, eu testei o código acima, usando os dois modos.

MakeWindowTransparent está retornando 0

pSetLayeredWindowAttributes auto error = GetLastErrorAsString();erro retornado é: wrong parameter.

Testei o código com outras janelas OpenGL, e não pausou em nenhum dos erros, também a janela ficou transparente corretamente.

Algumas informações que coletei sobre a janela:

Agradeço qualquer ajuda.

Respostas

1 VladFeinstein Nov 24 2020 at 06:18

Por que você está usando as duas bandeiras LWA_COLORKEY | LWA_ALPHA? Se você está especificando uma cor RGB(255, 255, 255), basta usar LWA_COLORKEY.

Além disso, por que você está mexendo GetProcAddress? SetLayeredWindowAttributesestá disponível a partir do Windows 2000; você está almejando plataformas mais antigas do que isso?

Não estou familiarizado com BlueStacks , mas SetLayeredWindowAttributes para tornar uma janela transparente funciona apenas parte do tempo sugere que SetLayeredWindowAttributesnão funciona com Direct3D; você sabe se o BlueStacks usa Direct3D?

Para OpenGLver isso: Como fazer um contexto de renderização OpenGL com fundo transparente?

ATUALIZAÇÃO Eu estava brincando com esse BlueStacks e percebi que você pode redirecioná-lo para outra janela (isso não é recomendado, pois você precisará lidar com alguns casos extremos). De qualquer forma, usei uma Notepadjanela existente e consegui definir o alfa nela, e isso afetou a janela filho do 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);
}

Eu também limpei sua função para me livrar 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;
}