Click here to Skip to main content
15,881,641 members
Articles / Desktop Programming / MFC
Article

Text ScrollBar Control

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
17 Jan 2012CPOL2 min read 49.8K   1.8K   14   5
CScrollBarEx is a simple MFC control derived from CWnd, it can display scrollbar max, min, and current value

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:

  1. It is simple, and simple to use.
  2. It is similar to the main functions of MFC scrollbar control.
  3. 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.

  1. Add ScrollBarEx.h and ScrollBarEx.cpp to your project.
  2. Add #include " ScrollBarEx.h" to the top of header file of class in which you want to add scrollbar control.
  3. Add a member variable of type CScrollBarEx.
  4. In your CPP file, use the Create method of the member variable to create a scrollbar control.
    C++
    // create scrollbar control
    // to get scrollbar control position
    CRect        rcScrollBar;
    CStatic *pStatic = (CStatic *)GetDlgItem(IDC_SCROLLBAR_STATIC);
    pStatic->GetWindowRect(rcScrollBar);
    ScreenToClient(rcScrollBar);
    
    m_ScrollBarEx.Create(WS_VISIBLE | WS_CHILD, rcScrollBar, this, 2000);
    // set max min current val
    m_ScrollBarEx.SetScrollBarMinMaxVal(0, 300);
    m_ScrollBarEx.SetScrollBarCurVal(100);
    // set color
    m_ScrollBarEx.SetScrollBarMinMaxValColor(RGB(0, 0, 0));
    m_ScrollBarEx.SetScrollBarTextBkColor(RGB(255, 255, 0));
    m_ScrollBarEx.SetScrollBarCurValColor(RGB(255, 0, 0));
  5. 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.

    C++
        // first declare msg response function in parent window header, under 
        //}}AFX_MSG and up DECLARE_MESSAGE_MAP(), just like this.
        afx_msg LRESULT OnScrollBarMsg(WPARAM wParam, LPARAM lParam);
    
        // second map scrollbar msg, between BEGIN_MESSAGE_MAP and END_MESSAGE_MAP(), 
        // just like this.
    BEGIN_MESSAGE_MAP(CComboboxTextDlg, CDialog)
        //{{AFX_MSG_MAP(CComboboxTextDlg)
        ON_WM_SYSCOMMAND()
        ON_WM_PAINT()
        ON_WM_QUERYDRAGICON()
        ON_WM_CTLCOLOR()
        //}}AFX_MSG_MAP
        ON_MESSAGE(WM_SCROLLBAR_MSG, OnScrollBarMsg)
    END_MESSAGE_MAP()
    
        // finally, code scrollbar msg process function
        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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
SemiEleven21-Apr-13 17:59
SemiEleven21-Apr-13 17:59 
QuestionNice work! Pin
satanness7-Jun-12 23:38
satanness7-Jun-12 23:38 
GeneralMy vote of 5 Pin
liuyonggang-jj17-May-12 23:23
liuyonggang-jj17-May-12 23:23 
QuestionGood job. Thank you! Pin
Member 4538074-Mar-12 8:33
Member 4538074-Mar-12 8:33 
GeneralMy vote of 5 Pin
Joxemi17-Jan-12 20:28
Joxemi17-Jan-12 20:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.