蓝色边框,选择时是深蓝色边框,下拉按钮可以自定义图片,不过只能是 15*16的BMP
class CMyComboBox : public CComboBox
{
// Construction
public:
CMyComboBox();
// normal_id 普通状态图片的ID
// hover_id hover状态图片的ID
// push_id push状态图片的ID
CMyComboBox(unsigned int normal_id,unsigned int hover_id,unsigned int push_id);
// Attributes
public:
// Operations
public:
// OverridesClassWizard
// generated virtual function overrides
//{{AFX_VIRTUAL(CMyComboBox)
protected:
virtual void PreSubclassWindow();
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CMyComboBox();
// Generated message map functions
protected:
//{{AFX_MSG(CMyComboBox)
afx_msg void OnPaint();
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
void DrawButton(CDC* pDC);
private:
unsigned int m_normal_id;
unsigned int m_hover_id;
unsigned int m_push_id;
//0-normal 1-hover 2-push
int m_button_state;
bool m_active;
bool m_draw;
};
#define BUTTON_STATE_NORMAL 0
#define BUTTON_STATE_HOVER (1<<1)
#define BUTTON_STATE_PUSH (1<<2)
/////////////////////////////////////////////////////////////////////////////
// CMyComboBox
CMyComboBox::CMyComboBox():m_active(false),
m_normal_id(0),
m_hover_id(0),
m_push_id(0),
m_button_state(BUTTON_STATE_NORMAL)
{
}
CMyComboBox::CMyComboBox(unsigned int normal_id,
unsigned int hover_id,
unsigned int push_id):
m_active(false),
m_normal_id(normal_id),
m_hover_id(hover_id),
m_push_id(push_id),
m_button_state(BUTTON_STATE_NORMAL)
{
}
CMyComboBox::~CMyComboBox()
{
}
BEGIN_MESSAGE_MAP(CMyComboBox, CComboBox)
//{{AFX_MSG_MAP(CMyComboBox)
ON_WM_PAINT()
ON_WM_MOUSEMOVE()
ON_WM_TIMER()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyComboBox message handlers
void CMyComboBox::PreSubclassWindow()
{
CRect rc;
GetWindowRect(&rc);
rc.left -= 2;
rc.top -= 15;
rc.bottom -= 23;
rc.right -= 8;
MoveWindow(&rc);
ModifyStyle(0,BS_OWNERDRAW);
CComboBox::PreSubclassWindow();
}
void
CMyComboBox::OnPaint()
{
CRect rc;
CDC* pDC;
Default();
pDC = GetDC();
GetClientRect(rc);
if(m_active)
pDC->Draw3dRect(rc, RGB(78,108,133), RGB(78,108,133));
else
pDC->Draw3dRect(rc, RGB(126,157,185), RGB(126,157,185));
//填充掉阴影
rc.DeflateRect(1,1);
pDC->Draw3dRect(rc, RGB(255,255,255), RGB(255,255,255));
//填充掉原来的按钮
rc.left += (rc.Width() - 18);
rc.DeflateRect(1,1);
pDC->Draw3dRect(rc, RGB(255,255,255), RGB(255,255,255));
DrawButton(pDC);
ReleaseDC(pDC);
}
void CMyComboBox::OnMouseMove(UINT nFlags, CPoint point)
{
m_draw = true;
SetTimer(1,10,NULL);
OnTimer(1);
CComboBox::OnMouseMove(nFlags, point);
}
void
CMyComboBox::OnTimer(UINT nIDEvent)
{
POINT pt;
CRect rc,rc_button;
GetCursorPos(&pt);
GetWindowRect(&rc);
rc_button = rc;
rc_button.left += rc_button.Width() - 17;
if(rc_button.PtInRect(pt))
{
if(m_button_state != BUTTON_STATE_PUSH)
m_button_state = BUTTON_STATE_HOVER;
}
else
m_button_state = BUTTON_STATE_NORMAL;
if(rc.PtInRect(pt))
{
m_active = true;
if(m_draw)
{
m_draw = false;
Invalidate();
}
}
else
{
KillTimer (1);
m_active = false;
m_button_state = 0;
Invalidate();
}
CComboBox::OnTimer(nIDEvent);
}
void
CMyComboBox::DrawButton(CDC* pDC)
{
if(m_normal_id == 0 || m_hover_id ==0 || m_push_id == 0)
return;
CDC MemDC;
CBitmap bitmap;
CRect rc;
GetWindowRect(&rc);
MemDC.CreateCompatibleDC(pDC);
if( m_button_state == BUTTON_STATE_NORMAL )
{
bitmap.LoadBitmap(m_normal_id);
}
else if( m_button_state == BUTTON_STATE_HOVER )
{
bitmap.LoadBitmap(m_hover_id);
}
else
{
bitmap.LoadBitmap(m_push_id);
}
MemDC.SelectObject(&bitmap);
pDC->BitBlt(rc.Width()-17,3,15,16,&MemDC,0,0,SRCCOPY);
}
void CMyComboBox::OnLButtonDown(UINT nFlags, CPoint point)
{
m_button_state = BUTTON_STATE_PUSH;
Invalidate();
CComboBox::OnLButtonDown(nFlags, point);
}
void CMyComboBox::OnLButtonUp(UINT nFlags, CPoint point)
{
m_button_state = BUTTON_STATE_HOVER;
Invalidate();
CComboBox::OnLButtonUp(nFlags, point);
}
{
// Construction
public:
CMyComboBox();
// normal_id 普通状态图片的ID
// hover_id hover状态图片的ID
// push_id push状态图片的ID
CMyComboBox(unsigned int normal_id,unsigned int hover_id,unsigned int push_id);
// Attributes
public:
// Operations
public:
// OverridesClassWizard
// generated virtual function overrides
//{{AFX_VIRTUAL(CMyComboBox)
protected:
virtual void PreSubclassWindow();
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CMyComboBox();
// Generated message map functions
protected:
//{{AFX_MSG(CMyComboBox)
afx_msg void OnPaint();
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
void DrawButton(CDC* pDC);
private:
unsigned int m_normal_id;
unsigned int m_hover_id;
unsigned int m_push_id;
//0-normal 1-hover 2-push
int m_button_state;
bool m_active;
bool m_draw;
};
#define BUTTON_STATE_NORMAL 0
#define BUTTON_STATE_HOVER (1<<1)
#define BUTTON_STATE_PUSH (1<<2)
/////////////////////////////////////////////////////////////////////////////
// CMyComboBox
CMyComboBox::CMyComboBox():m_active(false),
m_normal_id(0),
m_hover_id(0),
m_push_id(0),
m_button_state(BUTTON_STATE_NORMAL)
{
}
CMyComboBox::CMyComboBox(unsigned int normal_id,
unsigned int hover_id,
unsigned int push_id):
m_active(false),
m_normal_id(normal_id),
m_hover_id(hover_id),
m_push_id(push_id),
m_button_state(BUTTON_STATE_NORMAL)
{
}
CMyComboBox::~CMyComboBox()
{
}
BEGIN_MESSAGE_MAP(CMyComboBox, CComboBox)
//{{AFX_MSG_MAP(CMyComboBox)
ON_WM_PAINT()
ON_WM_MOUSEMOVE()
ON_WM_TIMER()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyComboBox message handlers
void CMyComboBox::PreSubclassWindow()
{
CRect rc;
GetWindowRect(&rc);
rc.left -= 2;
rc.top -= 15;
rc.bottom -= 23;
rc.right -= 8;
MoveWindow(&rc);
ModifyStyle(0,BS_OWNERDRAW);
CComboBox::PreSubclassWindow();
}
void
CMyComboBox::OnPaint()
{
CRect rc;
CDC* pDC;
Default();
pDC = GetDC();
GetClientRect(rc);
if(m_active)
pDC->Draw3dRect(rc, RGB(78,108,133), RGB(78,108,133));
else
pDC->Draw3dRect(rc, RGB(126,157,185), RGB(126,157,185));
//填充掉阴影
rc.DeflateRect(1,1);
pDC->Draw3dRect(rc, RGB(255,255,255), RGB(255,255,255));
//填充掉原来的按钮
rc.left += (rc.Width() - 18);
rc.DeflateRect(1,1);
pDC->Draw3dRect(rc, RGB(255,255,255), RGB(255,255,255));
DrawButton(pDC);
ReleaseDC(pDC);
}
void CMyComboBox::OnMouseMove(UINT nFlags, CPoint point)
{
m_draw = true;
SetTimer(1,10,NULL);
OnTimer(1);
CComboBox::OnMouseMove(nFlags, point);
}
void
CMyComboBox::OnTimer(UINT nIDEvent)
{
POINT pt;
CRect rc,rc_button;
GetCursorPos(&pt);
GetWindowRect(&rc);
rc_button = rc;
rc_button.left += rc_button.Width() - 17;
if(rc_button.PtInRect(pt))
{
if(m_button_state != BUTTON_STATE_PUSH)
m_button_state = BUTTON_STATE_HOVER;
}
else
m_button_state = BUTTON_STATE_NORMAL;
if(rc.PtInRect(pt))
{
m_active = true;
if(m_draw)
{
m_draw = false;
Invalidate();
}
}
else
{
KillTimer (1);
m_active = false;
m_button_state = 0;
Invalidate();
}
CComboBox::OnTimer(nIDEvent);
}
void
CMyComboBox::DrawButton(CDC* pDC)
{
if(m_normal_id == 0 || m_hover_id ==0 || m_push_id == 0)
return;
CDC MemDC;
CBitmap bitmap;
CRect rc;
GetWindowRect(&rc);
MemDC.CreateCompatibleDC(pDC);
if( m_button_state == BUTTON_STATE_NORMAL )
{
bitmap.LoadBitmap(m_normal_id);
}
else if( m_button_state == BUTTON_STATE_HOVER )
{
bitmap.LoadBitmap(m_hover_id);
}
else
{
bitmap.LoadBitmap(m_push_id);
}
MemDC.SelectObject(&bitmap);
pDC->BitBlt(rc.Width()-17,3,15,16,&MemDC,0,0,SRCCOPY);
}
void CMyComboBox::OnLButtonDown(UINT nFlags, CPoint point)
{
m_button_state = BUTTON_STATE_PUSH;
Invalidate();
CComboBox::OnLButtonDown(nFlags, point);
}
void CMyComboBox::OnLButtonUp(UINT nFlags, CPoint point)
{
m_button_state = BUTTON_STATE_HOVER;
Invalidate();
CComboBox::OnLButtonUp(nFlags, point);
}