ProgressBar 进度条

SHIT--

 

Usage:

NSTS::CProgressBar g_pb;
NSTS::CProgressBar g_pb2;


WM_INITDIALOG:
RECT rcpb = { 0, 0, 100, 20 };

assert (g_pb.Init (hInst, hwndDlg, &rcpb));
assert (g_pb2.Attach (GetDlgItem (hwndDlg, IDC_PROGRESS1)));
assert (g_pb2.SetStep (5));

// 背景颜色
assert (g_pb2.SetBkColor (RGB (128, 128, 255)));
// 进度条颜色
assert (g_pb2.SetIndicatorColor (RGB (255, 0, 0)));

---
assert (g_pb.StepIt ());
assert (g_pb2.StepIt ());

PS:

这只是封装微软的进度条,当然自己实现比较好,可以做美化,以及实现百分比显示。

#ifndef    progressbarH
#define progressbarH

namespace NSTS {

#include <Windows.h>
#include <commctrl.h>

class CProgressBar {
public:
CProgressBar (void) {
m_hSelf = NULL;
m_hParent = NULL;
m_iStep = 10;
m_hInst = NULL;
};
~CProgressBar (void) {
Destroy ();
};

bool Init (HINSTANCE hInst, HWND hParent, const RECT* prc, bool fSmooth = false, bool fHorizon = true) {
Destroy ();
if (hInst == NULL || !IsWindow (hParent) || prc == NULL) {
return false;
}

InitCommonControls ();

m_hSelf = CreateWindowEx (0,
PROGRESS_CLASS,
NULL,
WS_CHILD | WS_VISIBLE | (fSmooth ? PBS_SMOOTH : 0) | (fHorizon ? 0 : PBS_VERTICAL),
prc->left,
prc->top,
prc->right - prc->left,
prc->bottom - prc->top,
hParent,
NULL,
hInst,
NULL);
if (m_hSelf == NULL) {
return false;
}

m_hParent = hParent;
m_hInst = hInst;

return true;
};
void Destroy (void) {
if (IsWindow (m_hSelf)) {
DestroyWindow (m_hSelf);
}

m_hSelf = NULL;
m_hParent = NULL;

m_iStep = 10;
m_hInst = NULL;
};
bool Attach (HWND hPB) {
if (IsWindow (hPB)) {
m_hParent = GetParent (hPB);
m_hSelf = hPB;

return true;
}

return false;
};

bool StepIt (void) {
if (IsWindow (m_hSelf)) {
SendMessage (m_hSelf, PBM_STEPIT, (WPARAM)0, (LPARAM)0);

return true;
}

return false;
};

bool SetStep (int iStep) {
if (IsWindow (m_hSelf) && iStep > 0) {
SendMessage (m_hSelf, PBM_SETSTEP, (WPARAM)iStep, (LPARAM)0);
m_iStep = iStep;

return true;
}

return false;
};

bool SetPos (int iPos) {
if (IsWindow (m_hSelf) && iPos > 0) {
SendMessage (m_hSelf, PBM_SETPOS, (WPARAM)iPos, (LPARAM)0);

return true;
}

return false;
};

bool SetLength (int iLength) {
if (IsWindow (m_hSelf) && iLength > 0) {
SendMessage (m_hSelf, PBM_SETRANGE32, (WPARAM)0, (LPARAM)iLength);
return true;
}

return false;
};


int GetCurrentPos (void) const {
if (IsWindow (m_hSelf)) {
return ((int)SendMessage (m_hSelf, PBM_GETPOS, (WPARAM)0, (LPARAM)0));
}

return -1;
};

int GetLength (void) const {
if (IsWindow (m_hSelf)) {
return ((int)SendMessage (m_hSelf, PBM_GETRANGE, (WPARAM)FALSE, (LPARAM)NULL));
}

return -1;
};

int GetStep (void) const {
if (IsWindow (m_hSelf)) {
return m_iStep;
}

return -1;
};


bool SetBkColor (COLORREF clrBk) {
if (IsWindow (m_hSelf)) {
SendMessage (m_hSelf, PBM_SETBKCOLOR, (WPARAM)0, (LPARAM)clrBk);
return true;
}

return false;
};

bool SetIndicatorColor (COLORREF clrIndicator) {
if (IsWindow (m_hSelf)) {
SendMessage (m_hSelf, PBM_SETBARCOLOR, (WPARAM)0, (LPARAM)clrIndicator);
return true;
}

return false;
};


HWND GetHwnd (void) const { return m_hSelf; };
HWND GetParent (void) const { return m_hParent; };
private:
HWND m_hSelf;
HWND m_hParent;
HINSTANCE m_hInst;

int m_iStep;
};

}

#endif // progressbarH




你可能感兴趣的:(ProgressBar)