창을 투명하게 설정

Nov 24 2020

BlueStacks 창을 투명하게 설정하려고했습니다 .

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는 opengldirectx에서 실행할 수 있으며 두 모드를 모두 사용하여 위의 코드를 테스트했습니다.

MakeWindowTransparent 0을 반환합니다.

pSetLayeredWindowAttributes auto error = GetLastErrorAsString();반환 된 오류 : wrong parameter.

다른 OpenGL 창으로 코드를 테스트했으며 오류가 발생해도 일시 중지되지 않았으며 창이 올바르게 투명 해졌습니다.

창에 대해 수집 한 정보 :

도움을 주셔서 감사합니다.

답변

1 VladFeinstein Nov 24 2020 at 06:18

두 플래그를 모두 사용하는 이유는 무엇 LWA_COLORKEY | LWA_ALPHA입니까? 색상을 지정하는 RGB(255, 255, 255)경우 LWA_COLORKEY.

또한, 당신은 왜 엉망 GetProcAddress입니까? SetLayeredWindowAttributesWindows 2000부터 사용할 수 있습니다. 그보다 오래된 플랫폼을 타겟팅하고 있습니까?

나는 익숙하지 않다 블루스 택스 하지만 SetLayeredWindowAttributes 만 시간의 일부가 작동 창이 투명하게 그 제안 SetLayeredWindowAttributes에 일을하지 않습니다 Direct3D; BlueStacks가 사용 하는지 알고 Direct3D있습니까?

대한 OpenGL이를 참조하십시오 어떻게 투명 배경으로 OpenGL은 렌더링 컨텍스트를 만드는 방법?

업데이트 나는 BlueStacks를 가지고 놀고 있었고 다른 창으로 다시 부모를 지정할 수 있다고 생각했습니다 (일부 경우를 처리해야하므로 권장하지 않습니다). 어쨌든 기존 Notepad창을 사용했고 여기에 알파를 설정할 수 있었으며 이는 자식 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);
}

또한 다음을 제거하기 위해 함수를 정리했습니다 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;
}