DeAtHCrAsH
Messages postés
2670
Date d'inscription
vendredi 25 janvier 2002
Statut
Membre
Dernière intervention
6 février 2013
22 avril 2005 à 12:38
Yep,
Il est tout a fait possible de le faire a condition que tu sois sous Windows 2000 minimum.
Voila un bout de code qui rend une fenetre transparente.
Tu n'as cas changer le handle passé en parametre a la fonction MySetLayeredWindowAttributes pour que la transparence s'applique a ton control.
#include <windows.h>
#define WS_EX_LAYERED 0x00080000
#define LWA_COLORKEY 0x00000001
#define LWA_ALPHA 0x00000002
BYTE bAlpha=255,Anim=1;
typedef BOOL (WINAPI *SLWA_FUNC)(HWND hwnd,COLORREF crKey,BYTE bAlpha,DWORD dwFlags);
BOOL MakeAlpha(HWND hwnd,BYTE alpha)
{
// Récupération ddu handle de User32.dll, puis de l'adresse de
// la fonction SetLayeredWindowAttributes dans cette Dll.
SLWA_FUNC MySetLayeredWindowAttributes;
HMODULE hUser32 = GetModuleHandle("USER32.DLL");
if (!hUser32)
return FALSE;
MySetLayeredWindowAttributes = (SLWA_FUNC)GetProcAddress(hUser32,"SetLayeredWindowAttributes");
if (MySetLayeredWindowAttributes)
{
// Application du paramètre alpha sur la fenêtre
MySetLayeredWindowAttributes(hwnd,NULL,alpha,LWA_ALPHA);
// il est également possible de combiner le mode "Alpha" avec
// le mode transparent LWA_COLORKEY. Dans ce cas, toutes les parties
// de la fenêtre qui seront de la couleur 'crKey' seront transparentes.
// Il sera donc possible de cliquer à travers la fenêtre
//
// Exemples :
// MySetLayeredWindowAttributes(hwnd,0x00000000,alpha,LWA_ALPHA | LWA_COLORKEY);
// => Toutes les parties noires de la fenêtre seront transparentes
// Le reste aura un paramètre alpha
// MySetLayeredWindowAttributes(hwnd,0x00FFFFFF,NULL,LWA_COLORKEY);
// => toutes les parties blanches seront transparentes
// Le reste sera normal
return TRUE;
}
else {
// Si pas Windows 2000 ou Windows XP, la fonction SetLayeredWindowAttributes
// n'existe pas, il est donc impossible d'appliquer le paramètre.
MessageBox(hwnd,"Impossible de passer la fenêtre en WS_EX_LAYERED","Erreur",MB_ICONERROR);
return FALSE;
}
}
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
long result = 0;
switch(message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_CREATE:
if (MakeAlpha(hwnd,bAlpha))
SetTimer(hwnd,11111,10,NULL);
else
PostQuitMessage(0);
break;
case WM_CLOSE:
PostQuitMessage(0);
break;
case WM_TIMER:
MakeAlpha(hwnd,bAlpha);
if (Anim==0)
{
bAlpha+=1;
if (bAlpha>=255) Anim=1;
}
else {
bAlpha-=1;
if (bAlpha<=0) Anim=0;
}
break;
case WM_PAINT:
PAINTSTRUCT ps;
HDC hdc;
RECT rect;
BeginPaint(hwnd,&ps);
hdc = GetDC(hwnd);
GetClientRect(hwnd,&rect);
DrawText(hdc,"C'est t'y pas beau ça ?",23,&rect,DT_CENTER);
ReleaseDC(hwnd,hdc);
EndPaint(hwnd,&ps);
break;
default:
result = DefWindowProc(hwnd, message, wParam, lParam);
break;
}
return result;
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX MyWC={
sizeof(WNDCLASSEX),CS_HREDRAW|CS_VREDRAW,MainWndProc,0L,0L,
hInstance, LoadIcon(NULL,IDI_APPLICATION),
LoadCursor(NULL, IDC_ARROW),
(HBRUSH) (COLOR_BTNFACE+1),
NULL,"MyWndClassEx",LoadIcon(NULL,IDI_APPLICATION)};
if(!RegisterClassEx(&MyWC))
return 0;
HWND hWndMain = CreateWindowEx(WS_EX_LAYERED,"MyWndClassEx","Fenêtre transparente", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,400,300, NULL,NULL,hInstance,NULL);
ShowWindow(hWndMain,nCmdShow);
UpdateWindow(hWndMain);
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
Shell