-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgressBar.cpp
106 lines (85 loc) · 2.33 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
#include "stdafx.h"
#include <commctrl.h>
#include "ProgressBar.h"
extern HINSTANCE gInstance;
ProgressBar::ProgressBar()
: mProgressBar( NULL )
{
}
ProgressBar::~ProgressBar()
{
close();
}
void ProgressBar::open( HWND parent, uint16_t range, uint16_t step, const char* title )
{
if( mProgressBar != NULL )
{
close();
}
uint16_t parentWidth, parentHeight;
RECT rect;
if( GetWindowRect( parent, &rect ) )
{
parentWidth = static_cast< uint16_t >( rect.right - rect.left );
parentHeight = static_cast< uint16_t >( rect.bottom - rect.top );
}
// The scroll bar dimensions are arbitrarily picked
int barWidth = parentWidth / 4; // 25% of the width of the game window
int barHeight = parentHeight / 10; // 10% of the height of the game window
// Enforce a minimum dimension
if( barWidth < 200 )
barWidth = 200;
if( barHeight < 70 )
barHeight = 70;
// Center the progress bar in the game window
rect.left = ( parentWidth / 2 ) - ( barWidth / 2 );
rect.top = ( parentHeight / 2 ) - ( barHeight / 2 );
rect.right = rect.left + barWidth;
rect.bottom = rect.top + barHeight;
DWORD barStyle = WS_CHILD | WS_VISIBLE | PBS_SMOOTH;
// Add a title bar to the window if a title is set
if( title != NULL )
{
barStyle |= WS_CAPTION;
}
// Account for window titles, borders, etc in the progress bar's location
AdjustWindowRect( &rect, barStyle, FALSE );
mProgressBar = CreateWindow( PROGRESS_CLASS,
title,
barStyle,
rect.left, rect.top, barWidth, barHeight,
parent,
(HMENU) NULL,
gInstance,
NULL );
if( mProgressBar == NULL )
{
return;
}
// Enfore the range
// The progress bar should run from 0 to range with increments of step units
SendMessage( mProgressBar, PBM_SETRANGE, 0, MAKELPARAM( 0, range ) );
SendMessage( mProgressBar, PBM_SETSTEP, (WPARAM) step, 0 );
}
void ProgressBar::close( void )
{
if( mProgressBar != NULL )
{
DestroyWindow( mProgressBar );
mProgressBar = NULL;
}
}
void ProgressBar::step( void )
{
if( mProgressBar != NULL )
{
SendMessage( mProgressBar, PBM_STEPIT, 0, 0 );
}
}
void ProgressBar::setPosition( uint16_t position )
{
if( mProgressBar != NULL )
{
SendMessage( mProgressBar, PBM_SETPOS, (WPARAM) position, 0 );
}
}