-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgressBar.cpp
executable file
·123 lines (105 loc) · 3.29 KB
/
ProgressBar.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "StdAfx.h"
#include ".\progressbar.h"
#include ".\infopanel.h"
#include ".\style.h"
CProgressBar::CProgressBar(void)
{
maxValue = 100;
currentValue = 0;
caption = _T("");
}
CProgressBar::~CProgressBar(void)
{
}
int CProgressBar::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
return 0;
}
BEGIN_MESSAGE_MAP(CProgressBar, CWnd)
ON_WM_PAINT()
ON_WM_MOUSEMOVE()
ON_WM_CREATE()
ON_WM_LBUTTONUP()
END_MESSAGE_MAP()
using namespace Gdiplus;
void CProgressBar::OnPaint()
{
int borderWidth = 2; //Width of border between control and active-rect.
CRect rect ;GetClientRect(rect);
CPaintDC tempDC (this);
Graphics g (tempDC.m_hDC);
g.SetSmoothingMode(Gdiplus::SmoothingModeNone);
g.SetTextRenderingHint(Gdiplus::TextRenderingHintAntiAlias);
StyleObject<Brush> backgroundBrush = Style::Instance().GetProgressbarBackgroundBrush();
g.FillRectangle(backgroundBrush, RectF(rect.left,rect.top,rect.right,rect.bottom));
float drawWidth = (currentValue/(float)maxValue) * (rect.Width()-2*borderWidth);
/////////////////////////////////////////////////////////////////////////////
// Draw the active part:
CRect activeBound(borderWidth,borderWidth,borderWidth+(int)drawWidth,rect.Height()-borderWidth*2);
backgroundBrush = Style::Instance().GetProgressbarActiveBrush(activeBound);
g.FillRectangle(backgroundBrush, RectF(activeBound.left,activeBound.top,activeBound.right-borderWidth,activeBound.bottom));
/////////////////////////////////////////////////////////////////////////////
// Draw caption
StyleObject<Font> captionFont = Style::Instance().GetProgressbarCaptionFont();
StringFormat format;
format.SetLineAlignment(StringAlignmentCenter);
RectF textSize;
g.MeasureString(
CT2W(caption),
caption.GetLength(),
captionFont,
RectF(borderWidth+5, borderWidth+2, rect.right-borderWidth-2, rect.bottom-borderWidth-2),
&textSize
);
StyleObject<Brush> textBrush;
RectF captionRect;
if(textSize.GetRight() + 5 + 5 > activeBound.right) {
//Text to large to fit in active region.
textBrush = Style::Instance().GetProgressbarAlternateCaptionBrush();
captionRect = RectF(rect.left,rect.top,rect.right,rect.bottom);
format.SetAlignment(StringAlignmentCenter);
}
else {
textBrush = Style::Instance().GetProgressbarCaptionBrush();
captionRect = RectF(borderWidth+5, borderWidth, rect.right-borderWidth, rect.bottom-borderWidth-2);
}
g.DrawString(
CT2W(caption),
caption.GetLength(),
captionFont,
captionRect,
&format,
textBrush);
}
void CProgressBar::OnMouseMove(UINT nFlags, CPoint point)
{
CWnd::OnMouseMove(nFlags, point);
}
void CProgressBar::SetMaxValue(UINT max)
{
this->maxValue = max;
Invalidate();
}
void CProgressBar::SetCurrentValue(UINT c)
{
this->currentValue = c;
Invalidate();
}
void CProgressBar::SetCaption(const CString& caption)
{
this->caption = caption;
Invalidate();
}
void CProgressBar::OnLButtonUp(UINT nFlags, CPoint point)
{
int id = this->GetDlgCtrlID();
if(id)
{
CWnd* parent = this->GetParent();
HWND hwnd = this->GetSafeHwnd();
LRESULT res = parent->SendMessage(WM_COMMAND, MAKELONG(id, BN_CLICKED), (LPARAM)hwnd);
}
CWnd::OnLButtonUp(nFlags, point);
}