Imposta una finestra trasparente

Nov 24 2020

Ho provato a impostare la finestra di BlueStacks trasparente:

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 può essere eseguito opengle directx, ho testato il codice sopra, utilizzando entrambe le modalità.

MakeWindowTransparent sta restituendo 0

pSetLayeredWindowAttributes auto error = GetLastErrorAsString();errore restituito è: wrong parameter.

Ho testato il codice con altre finestre OpenGL e non si è fermato in nessuno degli errori, inoltre la finestra è diventata trasparente correttamente.

Alcune informazioni che ho raccolto sulla finestra:

Apprezzo qualsiasi aiuto.

Risposte

1 VladFeinstein Nov 24 2020 at 06:18

Perché stai usando entrambe le bandiere LWA_COLORKEY | LWA_ALPHA? Se stai specificando un colore RGB(255, 255, 255), usa semplicemente LWA_COLORKEY.

Inoltre, perché stai scherzando GetProcAddress? SetLayeredWindowAttributesè disponibile a partire da Windows 2000; stai prendendo di mira piattaforme più vecchie di così?

Non ho familiarità con BlueStacks , ma SetLayeredWindowAttributes per rendere trasparente una finestra funziona solo una parte del tempo suggerisce che SetLayeredWindowAttributesnon funziona con Direct3D; sai se BlueStacks utilizza Direct3D?

Per OpenGLvedere questo: come creare un contesto di rendering OpenGL con sfondo trasparente?

AGGIORNAMENTO Stavo giocando con quel BlueStacks e ho pensato che puoi ripeterlo in un'altra finestra (non è consigliato, poiché dovrai gestire alcuni casi limite). Ad ogni modo, ho usato una Notepadfinestra esistente e sono stato in grado di impostare alpha su di essa, e questo ha influenzato la finestra BlueStacks bambino :

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

Ho anche ripulito la tua funzione per sbarazzarti di 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;
}