Click here to Skip to main content
Click here to Skip to main content

Tagged as

Text ScrollBar Control

, 17 Jan 2012
Rate this:
Please Sign up or sign in to vote.
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.
    // 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.

        // 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)

Share

About the Author

mengxz

China China
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberSemiEleven21-Apr-13 17:59 
QuestionNice work! Pinmembersatanness7-Jun-12 23:38 
GeneralMy vote of 5 Pinmemberliuyonggang-jj17-May-12 23:23 
QuestionGood job. Thank you! PinmemberMember 4538074-Mar-12 8:33 
GeneralMy vote of 5 PinmemberJoxemi17-Jan-12 20:28 
It works fine and I like it.
Thank you for your work

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 17 Jan 2012
Article Copyright 2012 by mengxz
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid