|
Trouver une ressource
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
EDIT ET STATIC TRANSPARENTS SUR FOND BMP
Information sur la source
Description
Pour répondre à une question du forum, j'ai fait ce petit code source montrant comment rendre un Edit ou un Static transparent sur une fenêtre ayant comme fond une image bitmap. La transparence est réalisée en copiant la portion du bitmap correspondant aux coordonnées du contrôle (Static ou Edit) sur le fond de ce dernier. Pour cela, on sous-classe le contrôle et traite le message WM_ERASEBKGND. Dans la procédure de la fenêtre mère, on traite les messages WM_CTLCOLOREDIT et WM_CTLCOLORSTATIC. Ce petit projet est fait avec Visual C/C++ 2005 mais le code devrait aller avec tout autre compilateur sous Windows puisque c'est du WIN32 API. J'espère que ce sera utile à certains.
Source
- #include <windows.h>
-
- // Déclarations globales:
- WNDPROC oldstaticproc,oldeditproc;
- HDC hdcmem;
-
- // Procédure de sous-classement du Static:
- LRESULT CALLBACK staticproc(HWND hwnd,UINT msg,WPARAM wParam, LPARAM lParam)
- {
- if(msg==WM_ERASEBKGND)
- {
- POINT pt;
- RECT rect;
- GetClientRect(hwnd,&rect);// Obtenir les coordonnées de la zone cliente
- pt.x=rect.left; pt.y=rect.top;
- ClientToScreen(hwnd,&pt);// Les traduire en coordonnées écran
- ScreenToClient(GetParent(hwnd),&pt);// Les traduire en coordonnées fenêtre parente
- // Dessiner la portion du fond correspondante:
- BitBlt((HDC)wParam,0,0,rect.right,rect.bottom,hdcmem,pt.x,pt.y,SRCCOPY);
- return 1;
- }
- return CallWindowProc(oldstaticproc, hwnd, msg, wParam, lParam);
- }
-
- // Procédure de sous-classement de l'Edit:
- LRESULT CALLBACK editproc(HWND hwnd,UINT msg,WPARAM wParam, LPARAM lParam)
- {
- if(msg==WM_ERASEBKGND)
- {
- POINT pt;
- RECT rect;
- GetClientRect(hwnd,&rect);// Obtenir les coordonnées de la zone cliente
- pt.x=rect.left; pt.y=rect.top;
- ClientToScreen(hwnd,&pt);// Les traduire en coordonnées écran
- ScreenToClient(GetParent(hwnd),&pt);// Les traduire en coordonnées fenêtre parente
- // Dessiner la portion du fond correspondante:
- BitBlt((HDC)wParam,0,0,rect.right,rect.bottom,hdcmem,pt.x,pt.y,SRCCOPY);
- return 1;
- }
- return CallWindowProc(oldeditproc, hwnd, msg, wParam, lParam);
- }
-
- // Procédure de la fenêtre principale:
- LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam, LPARAM lParam)
- {
- static HBITMAP hbmp;
- static HWND hedit,hstatic,hquitter;
- static char buffer[100];
- switch (msg)
- {
- case WM_CREATE:
- // Création des contrôles:
- hstatic=CreateWindowEx(0,"static",0,WS_VISIBLE | WS_CHILD | SS_CENTER,30,60,300,20,hwnd,0,0,0);
- oldstaticproc=(WNDPROC)SetWindowLong(hstatic,GWL_WNDPROC,(long)staticproc);
- hedit=CreateWindowEx(WS_EX_CLIENTEDGE,"edit",0,WS_VISIBLE | WS_CHILD ,30,130,300,20,hwnd,0,0,0);
- oldeditproc=(WNDPROC)SetWindowLong(hedit,GWL_WNDPROC,(long)editproc);
- hquitter=CreateWindowEx(0,"button","Quitter",WS_VISIBLE | WS_CHILD ,140,190,80,20,hwnd,0,0,0);
- // Créer le Device Context en mémoire:
- hdcmem = CreateCompatibleDC(0);
- // Charger le bitmap de fond depuis les ressources de l'exécutable:
- hbmp = LoadBitmap(GetModuleHandle(0), "IDB_BMP");
- // Sélectionner ce bitmap pour le HDC en mémoire:
- SelectObject(hdcmem, hbmp);
- return 0;
-
- case WM_CTLCOLORSTATIC:
- case WM_CTLCOLOREDIT:
- // Définir le mode comme transparent:
- SetBkMode((HDC)wParam,TRANSPARENT);
- // Définir la couleur de texte (jaune):
- SetTextColor((HDC)wParam,RGB(255,255,0));
- // Retourner le HBRUSH de transparence:
- return (LRESULT)GetStockObject(NULL_BRUSH);
-
- case WM_PAINT:
- PAINTSTRUCT ps;
- HDC hdc;
- hdc=BeginPaint(hwnd,&ps);
- // Afficher l'image de fond sur la surface de la fenêtre:
- BitBlt(hdc,0,0,360,270,hdcmem,0,0,SRCCOPY);
- EndPaint(hwnd,&ps);
- return 0;
-
- case WM_COMMAND:
- if(HIWORD(wParam)==EN_CHANGE)
- {
- // Redessiner l'Edit après chaque modification:
- InvalidateRect((HWND)lParam,0,1);
- // Récupérer le texte de l'Edit:
- GetWindowText((HWND)lParam,buffer,100);
- // Le transmettre au Static:
- SetWindowText(hstatic,buffer);
- // Redessiner le Static:
- InvalidateRect(hstatic,0,1);
- return 0;
- }
- if((HWND)lParam==hquitter) SendMessage(hwnd,WM_CLOSE,0,0);
- return 0;
-
- case WM_CLOSE:
- DeleteDC(hdcmem);
- DeleteObject(hbmp);
- DestroyWindow(hwnd);
- break;
-
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- break;
- }
- return DefWindowProc(hwnd,msg,wParam,lParam);
- }
-
-
- int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR cmd,int show)
- {
- // Déclaration et initialisation de la strucrure WNDCLASSEX:
- WNDCLASSEX wc;
- memset(&wc,0,sizeof(wc));
- wc.cbSize=sizeof(WNDCLASSEX);
- wc.hInstance=hInst;
- wc.lpfnWndProc=WndProc;
- wc.hCursor=LoadCursor(0,IDC_ARROW);
- wc.hbrBackground=0;
- wc.lpszClassName=TEXT("mafenetre");
- // Enregistrement de notre classe de fenêtre:
- RegisterClassEx(&wc);
- // Creéation et affichage de la fenêtre:
- HWND hwnd=CreateWindowEx(0,"mafenetre","Edit et Static transparents sur fond BMP", WS_SYSMENU | WS_MINIMIZEBOX,0,0,366,296,0,0,0,0);
- ShowWindow(hwnd,1);
- UpdateWindow(hwnd);
- MSG Msg;
- // Boucle des messages:
- while (GetMessage(&Msg, 0, 0, 0))
- {
- TranslateMessage(&Msg);
- DispatchMessage(&Msg);
- }
- return 0;
- }
#include <windows.h>
// Déclarations globales:
WNDPROC oldstaticproc,oldeditproc;
HDC hdcmem;
// Procédure de sous-classement du Static:
LRESULT CALLBACK staticproc(HWND hwnd,UINT msg,WPARAM wParam, LPARAM lParam)
{
if(msg==WM_ERASEBKGND)
{
POINT pt;
RECT rect;
GetClientRect(hwnd,&rect);// Obtenir les coordonnées de la zone cliente
pt.x=rect.left; pt.y=rect.top;
ClientToScreen(hwnd,&pt);// Les traduire en coordonnées écran
ScreenToClient(GetParent(hwnd),&pt);// Les traduire en coordonnées fenêtre parente
// Dessiner la portion du fond correspondante:
BitBlt((HDC)wParam,0,0,rect.right,rect.bottom,hdcmem,pt.x,pt.y,SRCCOPY);
return 1;
}
return CallWindowProc(oldstaticproc, hwnd, msg, wParam, lParam);
}
// Procédure de sous-classement de l'Edit:
LRESULT CALLBACK editproc(HWND hwnd,UINT msg,WPARAM wParam, LPARAM lParam)
{
if(msg==WM_ERASEBKGND)
{
POINT pt;
RECT rect;
GetClientRect(hwnd,&rect);// Obtenir les coordonnées de la zone cliente
pt.x=rect.left; pt.y=rect.top;
ClientToScreen(hwnd,&pt);// Les traduire en coordonnées écran
ScreenToClient(GetParent(hwnd),&pt);// Les traduire en coordonnées fenêtre parente
// Dessiner la portion du fond correspondante:
BitBlt((HDC)wParam,0,0,rect.right,rect.bottom,hdcmem,pt.x,pt.y,SRCCOPY);
return 1;
}
return CallWindowProc(oldeditproc, hwnd, msg, wParam, lParam);
}
// Procédure de la fenêtre principale:
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam, LPARAM lParam)
{
static HBITMAP hbmp;
static HWND hedit,hstatic,hquitter;
static char buffer[100];
switch (msg)
{
case WM_CREATE:
// Création des contrôles:
hstatic=CreateWindowEx(0,"static",0,WS_VISIBLE | WS_CHILD | SS_CENTER,30,60,300,20,hwnd,0,0,0);
oldstaticproc=(WNDPROC)SetWindowLong(hstatic,GWL_WNDPROC,(long)staticproc);
hedit=CreateWindowEx(WS_EX_CLIENTEDGE,"edit",0,WS_VISIBLE | WS_CHILD ,30,130,300,20,hwnd,0,0,0);
oldeditproc=(WNDPROC)SetWindowLong(hedit,GWL_WNDPROC,(long)editproc);
hquitter=CreateWindowEx(0,"button","Quitter",WS_VISIBLE | WS_CHILD ,140,190,80,20,hwnd,0,0,0);
// Créer le Device Context en mémoire:
hdcmem = CreateCompatibleDC(0);
// Charger le bitmap de fond depuis les ressources de l'exécutable:
hbmp = LoadBitmap(GetModuleHandle(0), "IDB_BMP");
// Sélectionner ce bitmap pour le HDC en mémoire:
SelectObject(hdcmem, hbmp);
return 0;
case WM_CTLCOLORSTATIC:
case WM_CTLCOLOREDIT:
// Définir le mode comme transparent:
SetBkMode((HDC)wParam,TRANSPARENT);
// Définir la couleur de texte (jaune):
SetTextColor((HDC)wParam,RGB(255,255,0));
// Retourner le HBRUSH de transparence:
return (LRESULT)GetStockObject(NULL_BRUSH);
case WM_PAINT:
PAINTSTRUCT ps;
HDC hdc;
hdc=BeginPaint(hwnd,&ps);
// Afficher l'image de fond sur la surface de la fenêtre:
BitBlt(hdc,0,0,360,270,hdcmem,0,0,SRCCOPY);
EndPaint(hwnd,&ps);
return 0;
case WM_COMMAND:
if(HIWORD(wParam)==EN_CHANGE)
{
// Redessiner l'Edit après chaque modification:
InvalidateRect((HWND)lParam,0,1);
// Récupérer le texte de l'Edit:
GetWindowText((HWND)lParam,buffer,100);
// Le transmettre au Static:
SetWindowText(hstatic,buffer);
// Redessiner le Static:
InvalidateRect(hstatic,0,1);
return 0;
}
if((HWND)lParam==hquitter) SendMessage(hwnd,WM_CLOSE,0,0);
return 0;
case WM_CLOSE:
DeleteDC(hdcmem);
DeleteObject(hbmp);
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc(hwnd,msg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR cmd,int show)
{
// Déclaration et initialisation de la strucrure WNDCLASSEX:
WNDCLASSEX wc;
memset(&wc,0,sizeof(wc));
wc.cbSize=sizeof(WNDCLASSEX);
wc.hInstance=hInst;
wc.lpfnWndProc=WndProc;
wc.hCursor=LoadCursor(0,IDC_ARROW);
wc.hbrBackground=0;
wc.lpszClassName=TEXT("mafenetre");
// Enregistrement de notre classe de fenêtre:
RegisterClassEx(&wc);
// Creéation et affichage de la fenêtre:
HWND hwnd=CreateWindowEx(0,"mafenetre","Edit et Static transparents sur fond BMP", WS_SYSMENU | WS_MINIMIZEBOX,0,0,366,296,0,0,0,0);
ShowWindow(hwnd,1);
UpdateWindow(hwnd);
MSG Msg;
// Boucle des messages:
while (GetMessage(&Msg, 0, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return 0;
}
Sources du même auteur
Sources de la même categorie
Sources en rapport avec celle ci
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
mettre le fond du static en blanc [ par ggoufa ]
Salut a tous,est ce que quelqu'un sait comment on met le fond d'un static en blanc (ou d'une autre couleur) car j'ai mis le fond de ma boite de dialog
Bitmap et static [ par DjGonk ]
Slt!J'arriver a mettre une bitmap dans un static mais le probleme c'est que je voudrais que l'image et la taille du static. c a d quelle soit redimans
Static text fond transparant mais text plein au secours [ par youpiyoyo ]
Je sais po si mon titre est explicite mais j'aurai besoin d'ajouter sur une picture control un static text.le probleme c k'il garde la couleur de la b
couleur de fond d'une editbox [ par melkiorlenecrarque ]
salut kan je coche la case read-only d'une edit box, L devi1 griée!pe ton chanG la couleur de fon d'une edit box?G essayé SetColor() mé lé parametre m
[MFC] Image BITMAP en fond avec Edit et Combo Box visibles dessus [ par tom911 ]
Bonjour, je me prends la tête depuis plusieurs jours à essayer de mettre en arrière-plan une image bitmap qui se trouve dans un cStatic avec les MFC.E
poser un bitmap bouton sur un fond [ par keyaz ]
Bonjour, je suis actuellement en train d'essayer de faire apparaitre un bouton ac un bitmap sur un fond ayant lui meme un bitmap.Est ce que quelqu'un
CStatic fond transparent MFC [ par PACAL7585 ]
Bonjour,Je cherche a faire un CStatic avec un fondtransparent... j'ai trouver plein de code sur cppfrance, mais ca ne fonctionne pas avec mon appli, o
Mask Edit sous fond transparent [ par toxjamescook ]
Bonjour,j'aimerais savoir si il est possible de rendre le fond transparent d'un Mask Edit ou d'un Edit. j'ai pu voir pas mal de code permettant de cha
mettre un bitmap sur un fond qui a deja un bitmap, pb [ par gege1024 ]
voila: j'ai un dialogue avec une image en fond (bacground),par dessus je veux afficher un autre bitmap, mais le pb est que celui-ci s'affiche avec une
bitmap sur fond [ par yoshyman ]
est t il possible d' afficher un bit map sur un fond "colorié"?si oui comment?
|
Téléchargements
Logiciels à télécharger sur le même thème :
Comparez les prix Nouvelle version
|