Reputation: 105
I'm creating a new CComboBox
-derived class in order to paint all its regions and make it more customizable. I'm able to paint all it's areas and can draw the text of its drop down list. However, I don't know what to do to draw the text inside the control. I don't know why, the text of the control does not appear. Can anybody help me?
Header file
class CEasyComboBox : public CComboBox
{
private:
bool m_IsMouseOver; // Defines if the mouse is over the control.
bool m_bDropDownListStyle; // Type of the drop down list.
bool m_bIsButtonClick; // Defines if the button of the object has been clicked or not.
COLORREF m_clrBack; // Color of the background of the color.
COLORREF m_clrText; // Color of the text of the control.
COLORREF m_clrBorder; // Color of the border of the control.
COLORREF m_clrArrow; // Color of the arrow of the control.
public:
CEasyComboBox(); // Constructor
virtual ~CEasyComboBox(); // Destructor
afx_msg void OnMouseLeave(); // Called when the mouse leaves the control.
afx_msg void OnMouseMove(UINT nFlags, CPoint point); // Called everytime the mouse changes its location on the control.
protected:
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor); // Defines the colors of all existing elements of the control.
afx_msg void OnPaint(); // Paints the control.
afx_msg void OnLButtonDown(UINT nFlags, CPoint point); // Called when user clicks on left button of the mouse.
DECLARE_MESSAGE_MAP()
private:
void SetColors(); // Sets all colors of the control.
};
Cpp file
CEasyComboBox::CEasyComboBox()
{
// I set all colros:
SetColors();
}
CEasyComboBox::~CEasyComboBox()
{
}
BEGIN_MESSAGE_MAP(CEasyComboBox, CComboBox)
ON_WM_CTLCOLOR()
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
ON_WM_MOUSELEAVE()
ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()
void CEasyComboBox::SetColors()
{
// I define all colors of the object.
m_clrText = GENERIC_TEXT_COLOR;
m_clrBack = GENERIC_BACKGROUND_COLOR;
m_clrBorder = GENERIC_BORDER_COLOR;
// Depending on the mouse, I define the colrs.
if (m_IsMouseOver == true)
{
// I define all new colors.
m_clrBack = RGB(86, 81, 78);
}
// I define the color of the arrow equal as the borders.
m_clrArrow = m_clrText;
}
// CEasyComboBox message handlers
HBRUSH CEasyComboBox::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
// I check which control is going to be painted.
pDC->SetBkColor(m_clrBack);
pDC->SetTextColor(m_clrText);
HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);
return hbr;
}
void CEasyComboBox::OnPaint()
{
// I generate all requiered objects.
CPaintDC dc(this);
CRect ClientRect;
GetClientRect(&ClientRect);
// I paint the background of the combobox and its borders.
CBrush brush(m_clrBack);
CBrush* pOldBrush = dc.SelectObject(&brush);
dc.FillSolidRect(ClientRect, m_clrBorder);
ClientRect.DeflateRect(1, 1);
dc.FillRect(ClientRect, &brush);
// Depending on the stype of the element, I finish the painting.
DWORD dwComboStyle = GetStyle();
BYTE byteComboStyle = (BYTE)dwComboStyle;
if (byteComboStyle == CBS_SIMPLE)
{
dc.SelectObject(pOldBrush);
return;
}
// I resize the client rect.
int nButtonWidth = ::GetSystemMetrics(SM_CYSIZE);
DWORD dwExStyles = GetExStyle();
if (dwExStyles & WS_EX_RIGHT) ClientRect.right = ClientRect.left + nButtonWidth;
else ClientRect.left = ClientRect.right - nButtonWidth + 4;
// I draw the button if the mouse is over.
if (m_IsMouseOver == true) dc.FillSolidRect(&ClientRect, m_clrBorder);
else dc.FillSolidRect(ClientRect, m_clrBack);
// I draw the arrow.
CPoint CenterPoint(ClientRect.CenterPoint());
++CenterPoint.y;
CPoint UpperPoint(CenterPoint.x, CenterPoint.y - 4);
CPen Pen(PS_SOLID, 1, m_clrArrow);
CPen* pOldPen = dc.SelectObject(&Pen);
for (int i = 0; i < 4; ++i)
{
dc.MoveTo(CenterPoint);
VERIFY(dc.LineTo(UpperPoint));
--CenterPoint.x;
--CenterPoint.y;
--UpperPoint.x;
}
CenterPoint = ClientRect.CenterPoint();
++CenterPoint.y;
UpperPoint = CenterPoint;
UpperPoint.y -= 4;
for (int i = 0; i < 4; ++i)
{
dc.MoveTo(CenterPoint);
VERIFY(dc.LineTo(UpperPoint));
++CenterPoint.x;
--CenterPoint.y;
++UpperPoint.x;
}
dc.SelectObject(pOldBrush);
dc.SelectObject(pOldPen);
}
void CEasyComboBox::OnLButtonDown(UINT nFlags, CPoint point)
{
//the combo gets this message when its Button is pressed
m_bIsButtonClick = !m_bIsButtonClick;
CComboBox::OnLButtonDown(nFlags, point);
}
void CEasyComboBox::OnMouseLeave()
{
// I define the state of the variable.
m_IsMouseOver = false;
SetColors();
CComboBox::OnMouseLeave();
}
void CEasyComboBox::OnMouseMove(UINT nFlags, CPoint point)
{
// I define the state of the variable.
if(m_IsMouseOver == false)
{
m_IsMouseOver = true;
SetColors();
}
CComboBox::OnMouseMove(nFlags, point);
}
Upvotes: 0
Views: 720
Reputation: 1368
I adapte for you some thing that i have already done in a old project :
HBRUSH CHSpellComboBox::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hBGBrush = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);
switch (nCtlColor)
{
case CTLCOLOR_LISTBOX :
pDC->SetBkColor(RGB(200,0,0));
break;
}
pDC->SetTextColor(RGB(255,255,255));
hBGBrush = CreateSolidBrush(RGB (0,200,0));
return hBGBrush;
}
Upvotes: 2