Slider Control 控件自绘采用OnPaint方法进行自绘,自绘方式包括控件进度条自绘和控件把手自绘。
继承类redraw_slider.h头文件
#pragma once
#include "afxcmn.h"
class redraw_slider : public CSliderCtrl
{
public:
redraw_slider();
virtual ~redraw_slider() { delete_gdi_objs(); }
afx_msg void OnPaint();
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
DECLARE_MESSAGE_MAP()
public:
void set_bk_color(COLORREF bk) { m_dlg_bk_color = bk; }
void set_thumb_rate(float rate) { m_thumb_rate = rate; }
void set_thumb_colors(COLORREF out, COLORREF in, COLORREF highlight) { m_thumb_color = out; m_thumb_on_color = in; m_thumb_color = highlight; init_gdi_objs(); }
void set_channel_colors(COLORREF bk, COLORREF left) { m_channel_bk_color = bk; m_channel_left_color = left; init_gdi_objs(); }
private:
void init_gdi_objs();
void delete_gdi_objs();
void owner_draw(CDC* pDc);
void get_ellipse_rect(CRect& rect);
private:
COLORREF m_dlg_bk_color;
COLORREF m_channel_bk_color;
COLORREF m_channel_left_color;
COLORREF m_thumb_color;
COLORREF m_thumb_on_color;
COLORREF m_thumb_selected_color;
float m_thumb_rate;
BOOL thumb_down;
BOOL thumb_selected;
CPen pen_thumb_selected;
CPen pen_thumb;
CPen pen_thumb_on;
CBrush brush_thumb_selected;
CBrush brush_thumb;
CBrush brush_thumb_on;
};
继承类redraw_slider.cpp文件
#include"pch.h"
#include "redraw_slider.h"
#define SLIDER_BK_COLOR RGB(0,0,255)
#define SLIDER_LEFT_COLOR RGB(246, 179, 127)
#define SLIDER_THUMB_COLOR RGB(255,105,105)
#define SLIDER_THUMB_ON_COLOR RGB(0,255,0)
#define SLIDER_THUMB_SELECTED_COLOR RGB(210,105,30)
#define DLG_BK_COLOR RGB(240, 255, 255)
redraw_slider::redraw_slider()
{
m_dlg_bk_color = COLORREF(DLG_BK_COLOR);
m_channel_bk_color = COLORREF(SLIDER_BK_COLOR);
m_channel_left_color = COLORREF(SLIDER_LEFT_COLOR);
m_thumb_color = COLORREF(SLIDER_THUMB_COLOR);
m_thumb_on_color = COLORREF(SLIDER_THUMB_ON_COLOR);
m_thumb_selected_color = COLORREF(SLIDER_THUMB_SELECTED_COLOR);
m_thumb_rate = (float)0.6;
thumb_down = TRUE;
thumb_selected = TRUE;
init_gdi_objs();
}
void redraw_slider::init_gdi_objs()
{
pen_thumb_selected.CreatePen(PS_DASHDOTDOT, 2, m_thumb_selected_color);
pen_thumb.CreatePen(PS_DASHDOTDOT, 2, m_thumb_color);
pen_thumb_on.CreatePen(PS_DASHDOTDOT, 2, m_thumb_on_color);
brush_thumb_selected.CreateSolidBrush(m_thumb_selected_color);
brush_thumb.CreateSolidBrush(m_thumb_color);
brush_thumb_on.CreateSolidBrush(m_thumb_on_color);
}
void redraw_slider::delete_gdi_objs()
{
DeleteObject(pen_thumb_selected);
DeleteObject(pen_thumb);
DeleteObject(pen_thumb_on);
DeleteObject(brush_thumb_selected);
DeleteObject(brush_thumb);
DeleteObject(brush_thumb_on);
}
BEGIN_MESSAGE_MAP(redraw_slider, CSliderCtrl)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()
void redraw_slider::OnPaint()
{
CSliderCtrl::OnPaint();
CDC* pDc = GetDC();
owner_draw(pDc);
ReleaseDC(pDc);
}
void redraw_slider::owner_draw(CDC* pDc)
{
CRect clientRect;
GetClientRect(clientRect);
CRect channelRect;
GetChannelRect(&channelRect);
CRect thumbRect;
GetThumbRect(thumbRect);
pDc->SetBkMode(TRANSPARENT);
pDc->FillSolidRect(clientRect, m_dlg_bk_color);
pDc->FillSolidRect(channelRect, m_channel_bk_color);
pDc->Draw3dRect(channelRect, m_channel_bk_color, m_channel_bk_color);
CRect leftRect;
leftRect.left = channelRect.left;
leftRect.top = channelRect.top;
leftRect.bottom = channelRect.bottom;
leftRect.right = thumbRect.left;
pDc->FillSolidRect(leftRect, m_channel_left_color);
pDc->Draw3dRect(leftRect, m_channel_left_color, m_channel_left_color);
CPen* oldpen;
CBrush* oldbrush;
if (thumb_down)
{
oldpen = pDc->SelectObject(&pen_thumb_selected);
oldbrush = pDc->SelectObject(&brush_thumb_selected);
}
else if (!thumb_down && thumb_selected)
{
oldpen = pDc->SelectObject(&pen_thumb_on);
oldbrush = pDc->SelectObject(&brush_thumb_on);
}
else
{
oldpen = pDc->SelectObject(&pen_thumb);
oldbrush = pDc->SelectObject(&brush_thumb);
}
get_ellipse_rect(thumbRect);
pDc->Rectangle(thumbRect);
pDc->SelectObject(oldpen);
pDc->SelectObject(oldbrush);
}
void redraw_slider::get_ellipse_rect(CRect& rect)
{
CPoint center = rect.CenterPoint();
int length = 0;
rect.Width() < rect.Height() ? length = (int)(rect.Height() * m_thumb_rate) : length = (int)(rect.Width() * m_thumb_rate);
rect.left = center.x - length / 2;
rect.right = center.x + length / 2;
rect.top = center.y - length / 2;
rect.bottom = center.y + length / 2;
}
void redraw_slider::OnLButtonDown(UINT nFlags, CPoint point)
{
CRect thumbRect;
GetThumbRect(thumbRect);
get_ellipse_rect(thumbRect);
if (thumbRect.PtInRect(point) && !thumb_down)
{
thumb_down = TRUE;
InvalidateRect(thumbRect);
}
CSliderCtrl::OnLButtonDown(nFlags, point);
}
void redraw_slider::OnLButtonUp(UINT nFlags, CPoint point)
{
thumb_down = FALSE;
CSliderCtrl::OnLButtonUp(nFlags, point);
}
void redraw_slider::OnMouseMove(UINT nFlags, CPoint point)
{
CRect thumbRect;
GetThumbRect(thumbRect);
get_ellipse_rect(thumbRect);
BOOL inrect = thumbRect.PtInRect(point);
if (inrect != thumb_selected)
{
thumb_selected = inrect;
InvalidateRect(thumbRect);
}
CSliderCtrl::OnMouseMove(nFlags, point);
}