Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

[WIN32 / MacOS Bouton] Comment dessiner de jolis boutons sous windows ?

3 réponses
Avatar
Will
Salut,

Je connais les controles ownerdraw... je voudrais savoir comment dessiner
les degrades pour les boutons!

J'aimerais avoir le meme resultat que sous Mac OS.

Avec les brushs classiques de GDI ou GDI+



Merci

3 réponses

Avatar
Christian ASTOR
Will wrote:


Je connais les controles ownerdraw... je voudrais savoir comment dessiner
les degrades pour les boutons!



Un dégradé n'est qu'une boucle d'une brush à une autre en changeant les
composantes RGB.
Donc GradientFill() de Msimg32.dll ou ci-dessous une des mes vieilles
fonctions faisant un dégradé multiple sur n couleurs =>

ex d'appel ds un WM_PAINT:

COLORREF nColors[256];
nColors[0] = RGB(0,0,255); // Bleu
nColors[1] = RGB(255,0,0); // Rouge
nColors[2] = RGB(255,255,0); // Jaune
nColors[3] = RGB(0,255,0); // Vert
nColors[4] = RGB(255,0,255); // Rose
PAINTSTRUCT ps;
BeginPaint( hWnd, &ps );
FillGradient(hWnd, ps.hdc, nColors, 5, FADE_Vertical);
EndPaint( hWnd, &ps );


#define FADE_Horizontal 0
#define FADE_Vertical 1
#define FADE_Center 2

bool FillGradient(HWND hWndToFill, HDC hClientDC, COLORREF* nTabColors,
int nNbColors, int nFadingStyle)
{
HDC hDC ;
RECT rect, rect2;
GetClientRect(hWndToFill, &rect);
CopyRect(&rect2, &rect);
if (hClientDC) hDC = hClientDC; else hDC = GetDC(hWndToFill);
HDC hDCMem = CreateCompatibleDC(hDC);
double nLeft, nTop, nRight, nBottom, nStepRed[256], nStepGreen[256],
nStepBlue[256];
nLeft = rect.left;
nTop = rect.top;
nRight = rect.right;
nBottom = rect.bottom;
int nWidth = rect.right - rect.left;
int nHeight = rect.bottom - rect.top;
HBITMAP hBitmap = (HBITMAP) CreateCompatibleBitmap (hDC, nWidth , nHeight);
HBITMAP hBitmapOld = (HBITMAP) SelectObject(hDCMem, hBitmap);
int nNbRgn, i, j;
int nColorBits = GetDeviceCaps( hDC, BITSPIXEL ) * GetDeviceCaps( hDC,
PLANES );
switch (nColorBits)
{
case 32 :
case 24 :
case 16 :
nNbRgn = 256;
break;
case 15 :
nNbRgn = 32;
break;
default :
nNbRgn = 64;
}
double nStepX = (double) nWidth / nNbRgn;
double nStepY = (double) nHeight / nNbRgn;
double nRedBegin[256], nGreenBegin[256], nBlueBegin[256], nRedEnd[256],
nGreenEnd[256], nBlueEnd[256];
double nLength = (double) nNbRgn / (nNbColors - 1);

for (i=0;i<nNbColors-1;i++)
{
nRedBegin[i] = GetRValue(nTabColors[i]);
nGreenBegin[i] = GetGValue(nTabColors[i]);
nBlueBegin[i] = GetBValue(nTabColors[i]);
nRedEnd[i] = GetRValue(nTabColors[i+1]);
nGreenEnd[i] = GetGValue(nTabColors[i+1]);
nBlueEnd[i] = GetBValue(nTabColors[i+1]);
nStepRed[i] = (nRedEnd[i] - nRedBegin[i]) / (nLength - 1);
nStepGreen[i] = (nGreenEnd[i] - nGreenBegin[i]) / (nLength - 1);
nStepBlue[i] = (nBlueEnd[i] - nBlueBegin[i]) / (nLength - 1);
}

int nCpt = 0;
j= 0;
double nStart = 1;
while ( nCpt < nNbRgn )
{
for ( double nPos=nStart;nPos<nStart+nLength ;nPos++)
{

HBRUSH hBrush = CreateSolidBrush( RGB( nRedBegin[j], nGreenBegin[j],
nBlueBegin[j]));
if (nFadingStyle == FADE_Horizontal)
{
nRight = nLeft + nStepX;
rect.right = (INT) nRight + 0.5F;
FillRect( hDCMem, &rect, hBrush);
nLeft = nRight;
rect.left = (INT) nLeft + 0.5F;

}
else if (nFadingStyle == FADE_Vertical)
{
nBottom = nTop + nStepY;
rect.bottom = (INT) nBottom + 0.5F;
FillRect( hDCMem, &rect, hBrush);
nTop = nBottom;
rect.top = (INT) nTop + 0.5F;
}
else
{
RECT recttemp;
nBottom = nTop + nStepY/2;
rect.bottom = (INT) nBottom + 0.5F;
SetRect(&recttemp, rect.left, rect.top, rect.right, rect.bottom);
FillRect( hDCMem, &recttemp, hBrush);
SetRect(&recttemp, rect2.left, rect2.bottom-rect.bottom, rect2.right,
rect2.bottom-rect.top);
FillRect( hDCMem, &recttemp, hBrush);
nTop = nBottom;
rect.top = (INT) nTop + 0.5F;
}
DeleteObject( hBrush );
nRedBegin[j] += nStepRed[j];
nGreenBegin[j] += nStepGreen[j];
nBlueBegin[j] += nStepBlue[j];
nCpt ++;
}
nStart= nPos;
j+=1;
}
GetClientRect(hWndToFill, &rect);
BitBlt(hDC, 0, 0, rect.right, rect.bottom, hDCMem,0,0,SRCCOPY);
SelectObject(hDCMem, hBitmapOld);
DeleteObject( hBitmap );
DeleteDC( hDCMem );
if (!hClientDC) ReleaseDC( hWndToFill, hDC );
return TRUE;
}
Avatar
Christian ASTOR
Will wrote:

Je connais les controles ownerdraw... je voudrais savoir comment dessiner
les degrades pour les boutons!



Un dégradé n'est qu'une boucle d'une brush à une autre en changeant les
composantes RGB.
Donc GradientFill() de Msimg32.dll ou ci-dessous une des mes vieilles
fonctions faisant un dégradé multiple sur n couleurs =>

ex d'appel ds un WM_PAINT:
(pour owner-drawn, on passe le rect de l'item du WM_DRAWITEM)

COLORREF nColors[256];
nColors[0] = RGB(0,0,255); // Bleu
nColors[1] = RGB(255,0,0); // Rouge
nColors[2] = RGB(255,255,0); // Jaune
nColors[3] = RGB(0,255,0); // Vert
nColors[4] = RGB(255,0,255); // Rose
PAINTSTRUCT ps;
BeginPaint( hWnd, &ps );
FillGradient(hWnd, ps.hdc, NULL, nColors, 5, FADE_Vertical);
EndPaint( hWnd, &ps );


#define FADE_Horizontal 0
#define FADE_Vertical 1

bool FillGradient(HWND hWndToFill, HDC hClientDC, RECT* rectToFill,
COLORREF* nTabColors, int nNbColors, int nFadingStyle)
{
HDC hDC ;
RECT rect, rect2;
if (IsRectEmpty(rectToFill))
GetClientRect(hWndToFill, &rect);
else
CopyRect(&rect, rectToFill);
if (hClientDC) hDC = hClientDC; else hDC = GetDC(hWndToFill);
HDC hDCMem = CreateCompatibleDC(hDC);
double nLeft, nTop, nRight, nBottom, nStepRed[256], nStepGreen[256],
nStepBlue[256];
nLeft = rect.left = 0;
nTop = rect.top = 0;
nRight = rect.right- rect.left;
nBottom = rect.bottom- rect.top;
CopyRect(&rect2, &rect);
int nWidth = rect.right - rect.left;
int nHeight = rect.bottom - rect.top;
HBITMAP hBitmap = (HBITMAP) CreateCompatibleBitmap (hDC, nWidth , nHeight);
HBITMAP hBitmapOld = (HBITMAP) SelectObject(hDCMem, hBitmap);
int nNbRgn, i, j;
int nColorBits = GetDeviceCaps( hDC, BITSPIXEL ) * GetDeviceCaps( hDC,
PLANES );
switch (nColorBits)
{
case 32 :
case 24 :
case 16 :
nNbRgn = 256;
break;
case 15 :
nNbRgn = 32;
break;
default :
nNbRgn = 64;
}
double nStepX = (double) nWidth / nNbRgn;
double nStepY = (double) nHeight / nNbRgn;
double nRedBegin[256], nGreenBegin[256], nBlueBegin[256], nRedEnd[256],
nGreenEnd[256], nBlueEnd[256];
double nLength = (double) nNbRgn / (nNbColors - 1);

for (i=0;i<nNbColors-1;i++)
{
nRedBegin[i] = GetRValue(nTabColors[i]);
nGreenBegin[i] = GetGValue(nTabColors[i]);
nBlueBegin[i] = GetBValue(nTabColors[i]);
nRedEnd[i] = GetRValue(nTabColors[i+1]);
nGreenEnd[i] = GetGValue(nTabColors[i+1]);
nBlueEnd[i] = GetBValue(nTabColors[i+1]);
nStepRed[i] = (nRedEnd[i] - nRedBegin[i]) / (nLength - 1);
nStepGreen[i] = (nGreenEnd[i] - nGreenBegin[i]) / (nLength - 1);
nStepBlue[i] = (nBlueEnd[i] - nBlueBegin[i]) / (nLength - 1);
}

int nCpt = 0;
j= 0;
double nStart = 1;
while ( nCpt < nNbRgn )
{
for ( double nPos=nStart;nPos<nStart+nLength ;nPos++)
{

HBRUSH hBrush = CreateSolidBrush( RGB( nRedBegin[j], nGreenBegin[j],
nBlueBegin[j]));
if (nFadingStyle == FADE_Horizontal)
{
nRight = nLeft + nStepX;
rect.right = (INT) nRight + 0.5F;
FillRect( hDCMem, &rect, hBrush);
nLeft = nRight;
rect.left = (INT) nLeft + 0.5F;

}
else if (nFadingStyle == FADE_Vertical)
{
nBottom = nTop + nStepY;
rect.bottom = (INT) nBottom + 0.5F;
FillRect( hDCMem, &rect, hBrush);
nTop = nBottom;
rect.top = (INT) nTop + 0.5F;
}

DeleteObject( hBrush );
nRedBegin[j] += nStepRed[j];
nGreenBegin[j] += nStepGreen[j];
nBlueBegin[j] += nStepBlue[j];
nCpt ++;
}
nStart= nPos;
j+=1;
}
if (IsRectEmpty(rectToFill))
GetClientRect(hWndToFill, &rect);
else
CopyRect(&rect, rectToFill);
BitBlt(hDC, rect.left, rect.top, rect.right, rect.bottom,
hDCMem,0,0,SRCCOPY);
SelectObject(hDCMem, hBitmapOld);
DeleteObject( hBitmap );
DeleteDC( hDCMem );
if (!hClientDC) ReleaseDC( hWndToFill, hDC );
return TRUE;
}
Avatar
Ahryman40k
je crois que sur www.codeguru.com y'a un article complet sur comment creer
des boutons avec le look MacOS


"Will" a écrit dans le message de news:
bo9dge$s59$
Salut,

Je connais les controles ownerdraw... je voudrais savoir comment dessiner
les degrades pour les boutons!

J'aimerais avoir le meme resultat que sous Mac OS.

Avec les brushs classiques de GDI ou GDI+



Merci