Définir une fenêtre transparente
J'ai essayé de rendre la fenêtre BlueStacks 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 peut fonctionner opengl
et directx
, j'ai testé le code ci-dessus, en utilisant les deux modes.
MakeWindowTransparent
renvoie 0
pSetLayeredWindowAttributes
auto error = GetLastErrorAsString();
erreur renvoyée est: wrong parameter
.
J'ai testé le code avec d'autres fenêtres OpenGL, et il ne s'est interrompu dans aucune des erreurs, la fenêtre est également devenue transparente correctement.
Quelques informations que j'ai collectées sur la fenêtre:

Appréciez toute aide.
Réponses
Pourquoi utilisez-vous les deux indicateurs LWA_COLORKEY | LWA_ALPHA
? Si vous spécifiez une couleur RGB(255, 255, 255)
, utilisez simplement LWA_COLORKEY
.
Aussi, pourquoi vous dérangez-vous GetProcAddress
? SetLayeredWindowAttributes
est disponible à partir de Windows 2000; ciblez-vous des plates-formes plus anciennes que cela?
Je ne suis pas familier avec BlueStacks , mais SetLayeredWindowAttributes pour rendre une fenêtre transparente ne fonctionne qu'une partie du temps suggère que SetLayeredWindowAttributes
cela ne fonctionne pas avec Direct3D
; savez-vous si BlueStacks utilise Direct3D
?
Pour OpenGL
voir ceci: Comment créer un contexte de rendu OpenGL avec un fond transparent?
MISE À JOUR Je jouais avec ce BlueStacks , et j'ai pensé que vous pouvez le re-parent à une autre fenêtre (ce n'est pas recommandé, car vous devrez gérer certains cas extrêmes). Quoi qu'il en soit, j'ai utilisé une Notepad
fenêtre existante et j'ai pu définir l'alpha dessus, ce qui a affecté la fenêtre BlueStacks enfant :
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);
}
J'ai également nettoyé votre fonction pour me débarrasser 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;
}
