xref: /MusicPlayer2/MusicPlayer2/PlayerProgressBar.cpp (revision 6ff78c2af22a2540d4da0cd487a360b3fffc6e91)
1 // PlayerProgressBar.cpp : 实现文件
2 //
3 
4 #include "stdafx.h"
5 #include "MusicPlayer2.h"
6 #include "PlayerProgressBar.h"
7 #include "DrawCommon.h"
8 
9 
10 // CPlayerProgressBar
11 
IMPLEMENT_DYNAMIC(CPlayerProgressBar,CStatic)12 IMPLEMENT_DYNAMIC(CPlayerProgressBar, CStatic)
13 
14 CPlayerProgressBar::CPlayerProgressBar()
15     : m_theme_color(theApp.m_app_setting_data.theme_color)
16 {
17 }
18 
~CPlayerProgressBar()19 CPlayerProgressBar::~CPlayerProgressBar()
20 {
21 }
22 
23 
SetProgress(int progress)24 void CPlayerProgressBar::SetProgress(int progress)
25 {
26     m_progress = progress;
27     if (m_progress < 0)
28         m_progress = 0;
29     if (m_progress > 100)
30         m_progress = 100;
31     Invalidate();
32 }
33 
SetBackgroundColor(COLORREF back_color)34 void CPlayerProgressBar::SetBackgroundColor(COLORREF back_color)
35 {
36     m_back_color = back_color;
37 }
38 
SetBarCount(int bar_cnt)39 void CPlayerProgressBar::SetBarCount(int bar_cnt)
40 {
41     m_bar_count = bar_cnt;
42 }
43 
BEGIN_MESSAGE_MAP(CPlayerProgressBar,CStatic)44 BEGIN_MESSAGE_MAP(CPlayerProgressBar, CStatic)
45 	ON_WM_PAINT()
46 END_MESSAGE_MAP()
47 
48 
49 
50 // CPlayerProgressBar 消息处理程序
51 
52 
53 
54 void CPlayerProgressBar::OnPaint()
55 {
56 	CPaintDC dc(this); // device context for painting
57 	// TODO: 在此处添加消息处理程序代码
58 	// 不为绘图消息调用 CStatic::OnPaint()
59     CRect rect;
60     GetClientRect(rect);
61 
62     //双缓冲绘图
63     CDrawDoubleBuffer drawDoubleBuffer(&dc, rect);
64     CDrawCommon drawer;
65     drawer.Create(drawDoubleBuffer.GetMemDC());
66 
67     //开始绘图
68     int gap_width = rect.Width() / m_bar_count / 4;
69     if (gap_width < 1)
70         gap_width = 1;
71     drawer.FillRect(rect, m_back_color);
72     CRect rc_tmp{ rect };
73     rc_tmp.DeflateRect(theApp.DPI(1), theApp.DPI(1));
74     //绘制进度条
75     int bar_width = (rc_tmp.Width() - theApp.DPI(2) * 2) / m_bar_count;     //每一格的宽度
76     if (bar_width < gap_width + 1)
77         bar_width = gap_width + 1;
78     int progress_width = bar_width * m_bar_count + theApp.DPI(2) * 2;
79     rc_tmp.right = rect.left + progress_width;
80     drawer.FillRect(rc_tmp, RGB(255, 255, 255));
81     drawer.DrawRectOutLine(rc_tmp, m_theme_color.dark1, theApp.DPI(1), false);
82     int bar_cnt = m_progress / (100 / m_bar_count) + 1;        //格子数
83     int last_bar_percent = m_progress % (100 / m_bar_count);
84     CRect rc_bar{ rc_tmp };
85     rc_bar.DeflateRect(theApp.DPI(2), theApp.DPI(2));
86     rc_bar.right = rc_bar.left + bar_width - gap_width;
87     int start_x_pos = rc_bar.left;
88     for (int i = 0; i < bar_cnt; i++)
89     {
90         rc_bar.MoveToX(start_x_pos + i * bar_width);
91         if (i != bar_cnt - 1)
92         {
93             drawer.FillRect(rc_bar, m_theme_color.dark1, true);
94         }
95         else
96         {
97             BYTE alpha = ALPHA_CHG(last_bar_percent * m_bar_count);
98             drawer.FillAlphaRect(rc_bar, m_theme_color.dark1, alpha, true);
99         }
100     }
101 }
102