Affichage d'un bouton customisé
Bonjour,
je veux afficher les boutons d'une boîte de dialogue à ma façon.
Pour cela, j'utilise le code ci-dessous :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
case WM_NOTIFY:
{
draw_button = (LPNMHDR)lParam;
if(draw_button->code == NM_CUSTOMDRAW)
{
custom_draw_button = (LPNMCUSTOMDRAW)draw_button;
if(custom_draw_button->dwDrawStage==CDDS_PREPAINT)
{
if (draw_button->idFrom == ID_BUTTON_YES || draw_button->idFrom == ID_BUTTON_NO)
{
if (custom_draw_button->uItemState & CDIS_SELECTED) // On clique sur le bouton
FillRect(custom_draw_button->hdc, &(custom_draw_button->rc), selectbrush);
else if (custom_draw_button->uItemState & CDIS_HOT) // Survol de la souris
FillRect(custom_draw_button->hdc, &(custom_draw_button->rc), hotbrush);
else
FillRect(custom_draw_button->hdc, &(custom_draw_button->rc), defaultbrush);
old_font = SelectObject(custom_draw_button->hdc, hFont);
SetTextColor(custom_draw_button->hdc, RGB(255, 255, 255));
SetBkMode(custom_draw_button->hdc,TRANSPARENT);
if(draw_button->idFrom == ID_BUTTON_YES)
DrawText(custom_draw_button->hdc, "Oui",3,&(custom_draw_button->rc),DT_CENTER | DT_SINGLELINE | DT_VCENTER);
else
DrawText(custom_draw_button->hdc, "Non",3,&(custom_draw_button->rc),DT_CENTER | DT_SINGLELINE | DT_VCENTER);
SelectObject(custom_draw_button->hdc, old_font);
return CDRF_SKIPDEFAULT;
}
}
}
return CDRF_DODEFAULT;
}
break; |
Cela fonctionne bien, sauf que si je rajoute un troisième bouton, les 2 premiers ne fonctionnent plus correctement.
L'affichage du background des boutons ne se fait plus bien.
Quand on les survol, la couleur est celle par défaut et quand on ne les survol plus, c'est la couleur de survol qui est affichée.
C'est comme si le FillRect faisait bien la modif en cache, mais que l'affichage se faisait uniquement au prochain WM_NOTIFY.
J'ai identifié que cela venait du SendMessage, mais je ne comprends pas pourquoi.
Voici le nouveau code (j'ai juste rajouté le dernier else):
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
case WM_NOTIFY:
{
draw_button = (LPNMHDR)lParam;
if(draw_button->code == NM_CUSTOMDRAW)
{
custom_draw_button = (LPNMCUSTOMDRAW)draw_button;
if(custom_draw_button->dwDrawStage==CDDS_PREPAINT)
{
if (draw_button->idFrom == ID_BUTTON_YES || draw_button->idFrom == ID_BUTTON_NO)
{
if (custom_draw_button->uItemState & CDIS_SELECTED) // On clique sur le bouton
FillRect(custom_draw_button->hdc, &(custom_draw_button->rc), selectbrush);
else if (custom_draw_button->uItemState & CDIS_HOT) // Survol de la souris
FillRect(custom_draw_button->hdc, &(custom_draw_button->rc), hotbrush);
else
FillRect(custom_draw_button->hdc, &(custom_draw_button->rc), defaultbrush);
old_font = SelectObject(custom_draw_button->hdc, hFont);
SetTextColor(custom_draw_button->hdc, RGB(255, 255, 255));
SetBkMode(custom_draw_button->hdc,TRANSPARENT);
if(draw_button->idFrom == ID_BUTTON_YES)
DrawText(custom_draw_button->hdc, "Oui",3,&(custom_draw_button->rc),DT_CENTER | DT_SINGLELINE | DT_VCENTER);
else
DrawText(custom_draw_button->hdc, "Non",3,&(custom_draw_button->rc),DT_CENTER | DT_SINGLELINE | DT_VCENTER);
SelectObject(custom_draw_button->hdc, old_font);
return CDRF_SKIPDEFAULT;
}
else if (draw_button->idFrom == ID_BUTTON_CLOSE)
{
SendMessage(HwndButtonClose, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)HbmCroixSelected);
return CDRF_SKIPDEFAULT;
}
}
}
return CDRF_DODEFAULT;
}
break; |