magic_Nono
Messages postés
1878
Date d'inscription
jeudi 16 octobre 2003
Statut
Membre
Dernière intervention
16 mars 2011
27 oct. 2004 à 19:10
voici ma classe pour les couleurs des boutons
Fortement inspirée d'une de Vecchio que je remercie encore au passage
ça devrais t'aider....
++
// bbutton.cpp : implementation file
//Boutons avec couleur...
//vecchio
#include "stdafx.h"
#include "bbutton.hpp"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
const COLORREF CLR_BTN_WHITE = RGB(255, 255, 255);
const COLORREF CLR_BTN_BLACK = RGB(0, 0, 0);
const COLORREF CLR_BTN_DGREY = RGB(128, 128, 128);
const COLORREF CLR_BTN_GREY = RGB(192, 192, 192);
const COLORREF CLR_BTN_LLGREY = RGB(223, 223, 223);
const int BUTTON_IN = 0x01;
const int BUTTON_OUT = 0x02;
const int BUTTON_BLACK_BORDER = 0x04;
/////////////////////////////////////////////////////////////////////////////
// BButton
BButton::BButton()
{
m_textColor = GetSysColor(COLOR_BTNTEXT);
m_backgroundColor = GetSysColor(COLOR_BTNFACE);
m_disabledBackgroundColor = m_backgroundColor;
m_light = GetSysColor(COLOR_3DLIGHT);
m_highlight = GetSysColor(COLOR_BTNHIGHLIGHT);
m_shadow = GetSysColor(COLOR_BTNSHADOW);
m_darkShadow = GetSysColor(COLOR_3DDKSHADOW);
}
BButton::~BButton()
{
}
void BButton::SetColor(COLORREF new_text_colour, COLORREF new_background_colour)
{
m_textColor = new_text_colour;
m_backgroundColor = new_background_colour;
m_disabledBackgroundColor = GetSysColor(COLOR_BTNFACE);
Invalidate(FALSE);
}
void BButton::SetColor(COLORREF new_text_colour, COLORREF new_background_colour, COLORREF new_disabled_background_colour)
{
m_textColor = new_text_colour;
m_backgroundColor = new_background_colour;
m_disabledBackgroundColor = new_disabled_background_colour;
Invalidate(FALSE);
}
void BButton::ResetColor()
{
m_textColor = GetSysColor(COLOR_BTNTEXT);
m_backgroundColor = GetSysColor(COLOR_BTNFACE);
m_disabledBackgroundColor = m_backgroundColor;
m_light = GetSysColor(COLOR_3DLIGHT);
m_highlight = GetSysColor(COLOR_BTNHIGHLIGHT);
m_shadow = GetSysColor(COLOR_BTNSHADOW);
m_darkShadow = GetSysColor(COLOR_3DDKSHADOW);
Invalidate(FALSE);
}
BEGIN_MESSAGE_MAP(BButton, CButton)
//{{AFX_MSG_MAP(BButton)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// BButton message handlers
void BButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC *dc;
CRect focus_rect, button_rect, text_rect, offset_text_rect;
UINT state;
dc = CDC::FromHandle(lpDrawItemStruct->hDC);
state = lpDrawItemStruct->itemState;
focus_rect.CopyRect(&lpDrawItemStruct->rcItem);
button_rect.CopyRect(&lpDrawItemStruct->rcItem);
text_rect = button_rect;
text_rect.OffsetRect(-1, -1);
offset_text_rect = text_rect;
offset_text_rect.OffsetRect(1, 1);
// Set the focus rectangle to just past the border decoration
focus_rect.left += 4;
focus_rect.right -= 4;
focus_rect.top += 4;
focus_rect.bottom -= 4;
// Retrieve the button's caption
const int bufSize = 512;
TCHAR buffer[bufSize];
GetWindowText(buffer, bufSize);
if (state & ODS_DISABLED)
{
DrawFilledRect(dc, button_rect, m_disabledBackgroundColor);
}
else
{
DrawFilledRect(dc, button_rect, m_backgroundColor);
}
if (state & ODS_SELECTED)
{
DrawFrame(dc, button_rect, BUTTON_IN);
}
else
{
if ((state & ODS_DEFAULT) || (state & ODS_FOCUS))
{
DrawFrame(dc, button_rect, BUTTON_OUT | BUTTON_BLACK_BORDER);
}
else
{
DrawFrame(dc, button_rect, BUTTON_OUT);
}
}
if (state & ODS_DISABLED)
{
DrawButtonText(dc, offset_text_rect, buffer, CLR_BTN_WHITE);
DrawButtonText(dc, text_rect, buffer, CLR_BTN_DGREY);
}
else
{
if (state & ODS_SELECTED)
{
DrawButtonText(dc, offset_text_rect, buffer, m_textColor);
}
else
{
DrawButtonText(dc, text_rect, buffer, m_textColor);
}
}
if (state & ODS_FOCUS)
{
DrawFocusRect(lpDrawItemStruct->hDC, (LPRECT)&focus_rect);
}
}
void BButton::DrawFrame(CDC *dc, CRect r, int state)
{
COLORREF color;
if (state & BUTTON_BLACK_BORDER)
{
color = CLR_BTN_BLACK;
DrawLine(dc, r.left, r.top, r.right, r.top, color); // Across top
DrawLine(dc, r.left, r.top, r.left, r.bottom, color); // Down left
DrawLine(dc, r.left, r.bottom - 1, r.right, r.bottom - 1, color); // Across bottom
DrawLine(dc, r.right - 1, r.top, r.right - 1, r.bottom, color); // Down right
r.InflateRect(-1, -1);
}
if (state & BUTTON_OUT)
{
color = m_highlight;
DrawLine(dc, r.left, r.top, r.right, r.top, color); // Across top
DrawLine(dc, r.left, r.top, r.left, r.bottom, color); // Down left
color = m_darkShadow;
DrawLine(dc, r.left, r.bottom - 1, r.right, r.bottom - 1, color); // Across bottom
DrawLine(dc, r.right - 1, r.top, r.right - 1, r.bottom, color); // Down right
r.InflateRect(-1, -1);
color = m_light;
DrawLine(dc, r.left, r.top, r.right, r.top, color); // Across top
DrawLine(dc, r.left, r.top, r.left, r.bottom, color); // Down left
color = m_shadow;
DrawLine(dc, r.left, r.bottom - 1, r.right, r.bottom - 1, color); // Across bottom
DrawLine(dc, r.right - 1, r.top, r.right - 1, r.bottom, color); // Down right
}
if (state & BUTTON_IN)
{
color = m_darkShadow;
DrawLine(dc, r.left, r.top, r.right, r.top, color); // Across top
DrawLine(dc, r.left, r.top, r.left, r.bottom, color); // Down left
DrawLine(dc, r.left, r.bottom - 1, r.right, r.bottom - 1, color); // Across bottom
DrawLine(dc, r.right - 1, r.top, r.right - 1, r.bottom, color); // Down right
r.InflateRect(-1, -1);
color = m_shadow;
DrawLine(dc, r.left, r.top, r.right, r.top, color); // Across top
DrawLine(dc, r.left, r.top, r.left, r.bottom, color); // Down left
DrawLine(dc, r.left, r.bottom - 1, r.right, r.bottom - 1, color); // Across bottom
DrawLine(dc, r.right - 1, r.top, r.right - 1, r.bottom, color); // Down right
}
}
void BButton::DrawFilledRect(CDC *dc, CRect r, COLORREF color)
{
CBrush B;
B.CreateSolidBrush(color);
dc->FillRect(r, &B);
}
void BButton::DrawLine(CDC *dc, long sx, long sy, long ex, long ey, COLORREF color)
{
CPen new_pen;
CPen *old_pen;
new_pen.CreatePen(PS_SOLID, 1, color);
old_pen = dc->SelectObject(&new_pen);
dc->MoveTo(sx, sy);
dc->LineTo(ex, ey);
dc->SelectObject(old_pen);
new_pen.DeleteObject();
}
void BButton::DrawButtonText(CDC *dc, CRect r, const char *Buf, COLORREF m_textColor)
{
COLORREF previous_colour;
previous_colour = dc->SetTextColor(m_textColor);
dc->SetBkMode(TRANSPARENT);
dc->DrawText(Buf, strlen(Buf), r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
dc->SetTextColor(previous_colour);
}
Magic Nono: l'informagicien! 8-)