Couleurs et transparence des controles (api)

Description

Suite aux nombreuses questions dans le forum sur la transparence des

STATICs et l'utilisation des couleurs dans les contrôles, je me suis

dit qu'il serait utile de proposer un petit code source dans lequel je

rassemble tout ce que je sais à ce sujet. Tout réside dans le

traitement des messages suivants:
WM_CTLCOLORSTATIC: pour définir la couleur et la transparence d'un

STATIC, CHECKBOX ou bouton radio.
WM_CTLCOLOREDIT: pour définir la couleur du texte et de fond d'un EDIT.
WM_CTLCOLORLISTBOX: pour définir la couleur du texte et de fond d'une

LISTBOX.
WM_CTLCOLORSCROLLBAR: pour définir la couleur de fond d'une SCROLLBAR.
WM_CTLCOLORDLG: pour définir la couleur de fond d'une boite de

dialogue.
WM_DRAWITEM: pour définir la couleur du texte et de fond d'un bouton

créé avec le style BS_OWNERDRAW.
En cliquant sur un contôle de la boite de dialogue, ce programme change

la couleur, choisie au hasard, d'un STATIC en affichant sa valeur

héxadécimale.
Ce source n'utilise pas les ressources. Il suffit de le copier puis le

coller dans un nouveau projet WIN32. Il a été fait sous Visual C++ 6.

Source / Exemple :


#include <windows.h>

/******************************************************************/
/*************     Fonction de dessin des boutons     *************/
/******************************************************************/
void DessinerBouton(LPDRAWITEMSTRUCT lpds,LPCTSTR texte,COLORREF couleurtexte,COLORREF couleurfond)
{
	//Déclarations:
	SIZE dims;
	char nom[50];
	//Définir le texte du bouton:
	strcpy(nom, texte);
	//Déterminer les dimensions du texte:
    GetTextExtentPoint32(lpds->hDC, nom, strlen(nom), &dims);
	//Définir la couleur du texte:
    SetTextColor(lpds->hDC, couleurtexte);
	//Définir la couleur du fond:
    SetBkColor(lpds->hDC, couleurfond);
	//Déterminer l'état du bouton:
	BOOL etat=lpds->itemState & ODS_SELECTED;
	//Déterminer la largeur et la hauteur du bouton:
	int largeur=lpds->rcItem.right-lpds->rcItem.left;
	int hauteur=lpds->rcItem.bottom-lpds->rcItem.top;
	//Ecrire le texte sur le bouton:
    ExtTextOut(lpds->hDC,(largeur-dims.cx)/2+etat, (hauteur-dims.cy)/2+etat, ETO_OPAQUE | ETO_CLIPPED, &lpds->rcItem, nom, strlen(nom), NULL);
    //Dessiner le cadre du bouton selon son état:
	DrawEdge(lpds->hDC, &lpds->rcItem,(etat ? EDGE_SUNKEN : EDGE_RAISED ), BF_RECT);                
	//Retour:
	return;
}
/******************************************************************/

/******************************************************************/
/**************   Procédure de la boite de dialogue     ***********/					
/******************************************************************/
BOOL CALLBACK DialogProc(  HWND hDlg, UINT message, WPARAM wParam,LPARAM lParam  )
{
	//Déclarer et définir les couleurs de fond de certains contrôles:
	static HBRUSH hbDialog = CreateSolidBrush(RGB(200,175,100));
	static HBRUSH hbEdit= CreateSolidBrush(RGB(210,200,150));
	static HBRUSH hbScrollh= CreateSolidBrush(RGB(60,250,240));
	static HBRUSH hbScrollv= CreateSolidBrush(RGB(250,160,230));
	static HBRUSH hbList= CreateSolidBrush(RGB(120,170,130));
	//Déclarer et définir notre police:
	static HFONT hPolice=CreateFont(32,0,0,0,400,0,0,0,0,0,0,0,0,"Arial");
	//Déclaration de nos contrôles:
	static HWND hBouton1,hBouton2,hBouton3,hBouton4,hBouton5,hBouton6,hEdit;
	static HWND hCheckbox1,hCheckbox2,hRadio1,hRadio2,hScrollh,hScrollv,hList,hQuitter;
	static HWND hStatic1,hStatic2,hStatic3,hStatic4,hStatic5,hRadios,hChecks,hCadre;
	 
	switch (message)
	{
		case WM_INITDIALOG://Initialisation de notre boite de dialogue
			{
				//Ecrire le titre de la boite de dialogue:
				SetWindowText(hDlg,"Couleurs et transparence");
				//Création de nos contrôles:
				hBouton1=CreateWindow("BUTTON",0,WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,20,30,80,24,hDlg,0,0,0);
				hBouton2=CreateWindow("BUTTON",0,WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,20,70,80,24,hDlg,0,0,0);
				hBouton3=CreateWindow("BUTTON",0,WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,20,110,80,24,hDlg,0,0,0);
				hBouton4=CreateWindow("BUTTON",0,WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,20,150,80,24,hDlg,0,0,0);
				hBouton5=CreateWindow("BUTTON",0,WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,20,190,80,24,hDlg,0,0,0);
				hBouton6=CreateWindow("BUTTON",0,WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,20,230,80,24,hDlg,0,0,0);
				hQuitter=CreateWindow("BUTTON",0,WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,310,230,80,24,hDlg,0,0,0);
				hEdit=CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT",0,WS_CHILD | WS_VISIBLE,130,233,150,20,hDlg,0,0,0);
				hCheckbox1=CreateWindow("BUTTON","Check 1",WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX ,308,167,100,16,hDlg,0,0,0);
				hCheckbox2=CreateWindow("BUTTON","Check 2",WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX ,308,187,100,16,hDlg,0,0,0);
				hRadio1=CreateWindow("BUTTON","Radio 1",WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON ,305,106,100,16,hDlg,0,0,0);
				hRadio2=CreateWindow("BUTTON","Radio 2",WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON ,305,126,100,16,hDlg,0,0,0);
				hScrollh=CreateWindow("SCROLLBAR",0,WS_CHILD | WS_VISIBLE | SBS_HORZ,20,270,400,20,hDlg,0,0,0);  
				hScrollv=CreateWindow("SCROLLBAR",0,WS_CHILD | WS_VISIBLE | SBS_VERT,420,30,20,240,hDlg,0,0,0);
				hList=CreateWindowEx(WS_EX_CLIENTEDGE,"LISTBOX",0,WS_CHILD | WS_VISIBLE,130,110,150,104,hDlg,0,0,0);
				hRadios=CreateWindow("BUTTON",0,WS_CHILD | WS_VISIBLE | BS_GROUPBOX,300,91,90,56,hDlg,0,0,0);
				hChecks=CreateWindow("BUTTON",0,WS_CHILD | WS_VISIBLE | BS_GROUPBOX,300,151,90,59,hDlg,0,0,0);
				hCadre=CreateWindow("BUTTON",0,WS_CHILD | WS_VISIBLE | BS_GROUPBOX,130,39,260,48,hDlg,0,0,0);
				hStatic1=CreateWindow("STATIC","Edit :",WS_CHILD | WS_VISIBLE ,130,216,80,16,hDlg,0,0,0);
				hStatic2=CreateWindow("STATIC","ListBox :",WS_CHILD | WS_VISIBLE ,130,93,80,16,hDlg,0,0,0);
				hStatic3=CreateWindow("STATIC","www.cppfrance.com",WS_CHILD | WS_VISIBLE | SS_CENTER,132,49,256,36,hDlg,0,0,0);
				hStatic4=CreateWindow("STATIC","Static :",WS_CHILD | WS_VISIBLE ,130,30,80,16,hDlg,0,0,0);
				//Ajouter deux éléments dans la LISTBOX:
				SendMessage(hList,LB_ADDSTRING,0,(LPARAM)"Premier élément");
				SendMessage(hList,LB_ADDSTRING,0,(LPARAM)"Deuxième élément");
				//Changer la police de notre STATIC principal:
				SendMessage(hStatic3,WM_SETFONT,(WPARAM)hPolice,1);
				//Donner le focus à notre EDIT:
				SetFocus(hEdit);
				return 0;			
			}

		case WM_CLOSE://Fermeture
			    //Suppression des objets créés:
				DeleteObject(hbDialog);
				DeleteObject(hbEdit);
				DeleteObject(hbScrollh);
				DeleteObject(hbScrollv);
				DeleteObject(hbList);
				DeleteObject(hPolice);
				//Fermeture de la Boite de dialogue:
				EndDialog(hDlg,0);
				return 0;

		case WM_CTLCOLORDLG://Couleur de fond de la boite de dialogue
				return (INT_PTR)hbDialog;
		   
		case WM_DRAWITEM://Dessin des boutons
			{
				LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)lParam;

                if (lpdis->hwndItem==hBouton1)//Bouton jaune
                {
					DessinerBouton(lpdis,"Jaune",RGB(0,0,255),RGB(255,255,0));
				    return TRUE;
                }

				if (lpdis->hwndItem==hBouton2)//Bouton vert
                {     
					DessinerBouton(lpdis,"Vert",RGB(114,95,42),RGB(0,255,0));
                    return TRUE;
                }

				if (lpdis->hwndItem==hBouton3)//Bouton bleu
                {
					DessinerBouton(lpdis,"Bleu",RGB(255,255,0),RGB(0,0,255));		
                    return TRUE;
                }

				if (lpdis->hwndItem==hBouton4)//Bouton rouge
                {
					DessinerBouton(lpdis,"Rouge",RGB(255,255,255),RGB(255,0,0));		
                    return TRUE;
				}

			    if (lpdis->hwndItem==hBouton5)//Bouton blanc
                {
					DessinerBouton(lpdis,"Blanc",RGB(255,0,0),RGB(255,255,255));		                  
                    return TRUE;
                }

		    	if (lpdis->hwndItem==hBouton6)//Bouton noir
                {
					DessinerBouton(lpdis,"Noir",RGB(0,255,255),RGB(0,0,0));		                  
                    return TRUE;
                }

				if (lpdis->hwndItem==hQuitter)//Bouton Quitter
                {
					DessinerBouton(lpdis,"Quitter",RGB(255,255,0),RGB(114,95,42));		                  
                    return TRUE;
                }
                break;
			}

		case WM_CTLCOLORSTATIC://Dessin des STATICs, RADIOs et CHECKBOXes
			{
				if ((HWND)lParam==hCheckbox1) 
				{
					SetTextColor((HDC)wParam,RGB(0,0,255));
					SetBkMode((HDC)wParam, TRANSPARENT);
					return (BOOL)GetStockObject(NULL_BRUSH);
				}

				if ((HWND)lParam==hCheckbox2) 
				{
					SetTextColor((HDC)wParam,RGB(0,0,255));
					SetBkMode((HDC)wParam, TRANSPARENT);
					return (BOOL)GetStockObject(NULL_BRUSH);
				}

				if ((HWND)lParam==hRadio1) 
				{
					SetTextColor((HDC)wParam,RGB(255,0,0));
					SetBkMode((HDC)wParam, TRANSPARENT);
					return (BOOL)GetStockObject(NULL_BRUSH);
				}

				if ((HWND)lParam==hRadio2) 
				{
					SetTextColor((HDC)wParam,RGB(255,0,0));
					SetBkMode((HDC)wParam, TRANSPARENT);
					return (BOOL)GetStockObject(NULL_BRUSH);
				}

				if ((HWND)lParam==hStatic1) 
				{
					SetTextColor((HDC)wParam,RGB(255,255,255));
					SetBkMode((HDC)wParam, TRANSPARENT);
					return (BOOL)GetStockObject(NULL_BRUSH);
				}

				if ((HWND)lParam==hStatic2) 
				{
					SetTextColor((HDC)wParam,RGB(255,255,255));
					SetBkMode((HDC)wParam, TRANSPARENT);
					return (BOOL)GetStockObject(NULL_BRUSH);
				}

				if ((HWND)lParam==hStatic3) 
				{
					SetTextColor((HDC)wParam,RGB(170,200,175));
					SetBkMode((HDC)wParam, TRANSPARENT);
					return (BOOL)GetStockObject(NULL_BRUSH);
				}

				if ((HWND)lParam==hStatic4) 
				{
					SetTextColor((HDC)wParam,RGB(255,255,255));
					SetBkMode((HDC)wParam, TRANSPARENT);
					return (BOOL)GetStockObject(NULL_BRUSH);
				}
				return 0;
			}

		case WM_CTLCOLOREDIT://Dessin du contrôle EDIT
			{
				if ((HWND)lParam==hEdit) 
				{
					SetTextColor((HDC)wParam,RGB(70,110,60));
					SetBkColor((HDC)wParam,RGB(210,200,150));
					return (BOOL) hbEdit;
				}			
				return 0;
			}

		case WM_CTLCOLORSCROLLBAR://Dessin des SCROLLBARs
			{
				if ((HWND)lParam==hScrollh)  return (BOOL) hbScrollh;
				if ((HWND)lParam==hScrollv)  return (BOOL) hbScrollv;
				return 0;
			}

		case WM_CTLCOLORLISTBOX://Dessin de la LISTBOX
			{
				if ((HWND)lParam==hList)
				{
					SetTextColor((HDC)wParam,RGB(255,255,0));
					SetBkColor((HDC)wParam,RGB(120,170,130));
					return (BOOL) hbList;
				}
				return 0;
			}

		case WM_COMMAND://Clic sur un contrôle
			{  
				//Obtenir le device contexte du STATIC principal:
				HDC hdc=GetDC(hStatic3);
				//Déterminer la zone dessinable:
			    RECT rect;
			    GetClientRect(hStatic3,&rect);
				//Effacer contenu du STATIC:
			    FillRect(hdc,&rect,hbDialog);
				//Choisir une couleur au hasard:
			    int couleur=rand()%256*0x10000+rand()%256*0x100+rand()%256;
			    //Définir la couleur du texte:
				SetTextColor(hdc,couleur);
				//Rendre le Static transparent:
			    SetBkMode(hdc,TRANSPARENT);
				//Choisir notre police:
			    HFONT hOrigi=(HFONT)SelectObject(hdc,hPolice);
				//Convertir la couleur en valeur héxadécimale:
			    char valeurhexa[6];
			    itoa(couleur,valeurhexa,16);
		        //Ecrire la valeur dans le STATIC
			    DrawText( hdc, valeurhexa, -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
			    //Restaurer la police originale:
				SelectObject(hdc,hOrigi);
				//Libérer le device context:
			    ReleaseDC(hStatic3,hdc);

				if ((HWND)lParam==hQuitter)//clic sur Quitter
				{
					//Envoi du message de fermeture
					SendMessage(hDlg,WM_CLOSE,0,0);
					break;
				}
		   
			}
	}
	//Retour:
	return 0;
}
/******************************************************************/

/******************************************************************/
/*****************  Fonction WinMain  *****************************/
/******************************************************************/
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{  
	//Allouer de la mémoire pour notre dialog template:
    LPDLGTEMPLATE lpdt = ( LPDLGTEMPLATE) GlobalAlloc(GPTR, 512); 
    if (!lpdt) return 1;    
    // Définir les propriétés de la boite de dialogue:
    lpdt->style = DS_CENTER | WS_POPUP | WS_MINIMIZEBOX| WS_SYSMENU | DS_MODALFRAME | WS_CAPTION; 
    lpdt->x = lpdt->y  = 0;  lpdt->cx = 230; lpdt->cy = 155;         
    //Lancer la boite de dialogue
    DialogBoxIndirect(hInstance,lpdt,NULL,(DLGPROC)DialogProc);
	//Libération de la mémoire allouée:
    GlobalFree((HGLOBAL)lpdt); 
	//Retour:
	return 0;
}
/******************************************************************/

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.