Introduction
CScrollBarEx
is a simple MFC control derived from CWnd
, it can display scrollbar max, min, and current value itself.
Background
Recently, in one of my projects, I needed one scrollbar control that can display max, min and current val itself. You know, Microsoft's scrollbar control can't display text, so I decided that I would code this control by myself. Now I completed this scrollbar control, and it is not so bad for VC++ beginner, I decided to share this control with all CodeProject workmates. I did not have a lot of time to work on this control, so I had to code it simply, but it satisfied my requirements. It has some features as follows:
- It is simple, and simple to use.
- It is similar to the main functions of MFC scrollbar control.
- It can display scrollbar max, min and current value.
Now the scrollbar control is here. Enjoy and please help me with your invaluable notes, bug reports, ideas, etc. that you think might improve the quality of this code.
Using the Code
Creating the Scrollbar Control
First we need to create a scrollbar control.
- Add ScrollBarEx.h and ScrollBarEx.cpp to your project.
- Add
#include " ScrollBarEx.h"
to the top of header file of class in which you want to add scrollbar control. - Add a member variable of type
CScrollBarEx
. - In your CPP file, use the
Create
method of the member variable to create a scrollbar control.
CRect rcScrollBar;
CStatic *pStatic = (CStatic *)GetDlgItem(IDC_SCROLLBAR_STATIC);
pStatic->GetWindowRect(rcScrollBar);
ScreenToClient(rcScrollBar);
m_ScrollBarEx.Create(WS_VISIBLE | WS_CHILD, rcScrollBar, this, 2000);
m_ScrollBarEx.SetScrollBarMinMaxVal(0, 300);
m_ScrollBarEx.SetScrollBarCurVal(100);
m_ScrollBarEx.SetScrollBarMinMaxValColor(RGB(0, 0, 0));
m_ScrollBarEx.SetScrollBarTextBkColor(RGB(255, 255, 0));
m_ScrollBarEx.SetScrollBarCurValColor(RGB(255, 0, 0));
- Scrollbar control notify message
When moving scrollbar control to any position, scrollbar control's parent window would receive scrollbar control's notify message, you can do like this.
afx_msg LRESULT OnScrollBarMsg(WPARAM wParam, LPARAM lParam);
BEGIN_MESSAGE_MAP(CComboboxTextDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CTLCOLOR()
ON_MESSAGE(WM_SCROLLBAR_MSG, OnScrollBarMsg)
END_MESSAGE_MAP()
LRESULT CScrollBarDemoDlg::OnScrollBarMsg(WPARAM wParam, LPARAM lParam)
{
double dblCurVal = *(double *)lParam;
TRACE("ScrollBar Cur Val = %f\n", dblCurVal);
return 0;
}
Next to Update
This scrollbar control is simple. I know that it needs many other functions to complete it, but I had no time, so if you update it, please send an mail to me. Thank you.
History
- 2012/1/10 Created scrollbar control
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.