Receiving CSliderCtrl notify messages - TB_THUMBTRACK

Posted on 2004-08-02
Medium Priority
Last Modified: 2013-11-20
I'm trying to get TB_THUMBTRACK messages from a CSliderCtrl in VC++ 6.0.  

In my parent window (a subclass of CView) I have overridden OnNotify( WPARAM wParam, LPARAM lParam, LRESULT* pResult ).  I have a number of CSliderCtrls in the view.  My code goes something like this:  

OnNotify( WPARAM wParam, LPARAM lParam, LRESULT* pResult ) {
   CWnd* pWnd = GetDlgItem( wParam );

    if ( pWnd->IsKindOf( RUNTIME_CLASS( CSliderCtrl ) ) {
        CSliderCtrl* pSlider = (CSliderCtrl*)pWnd;
        NMHDR pNMHDR = (NMHDR)lParam;

        switch ( pNMHDR->code ) {
        case TB_THUMBTRACK:
            // Should be here when user drags slider thumb but never get here

I've managed to work out that lParam->code never equals TB_THUMBTRACK only ever the values -12 or -16.  I can't find these values in CommCtrl.h which is where the majority of the notify defines are so I'm not totally sure what message is being received.  It certainly isn't the one I want, TB_ messages go from 0-8.  

What am I doing wrong here?  How can I receive the correct notify messages from the slider?  

Thanks for your help.  
Question by:rubinho
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11693478
Inside your CView derived implementation (.cpp) you will find a message map section. Insert a handler there, specifying control ID (1000 in this example, and handling function):

      ON_NOTIFY(TB_THUMBTRACK, 1000, OnThumbTrack)      <---------------------

After that, implement your handler function (and declare in your view class definition, of course):

void CYourView::OnThumbTrack(NMHDR* pNMHDR, LRESULT* pResult)
     // your handling code here

Read more about CSliderCtrl notification handler here:

Expert Comment

ID: 11693566
try OnHScroll

afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);

void CClientDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
     if (m_pPosition != NULL) {
         switch (nSBCode) {
         case TB_THUMBTRACK:

Author Comment

ID: 11693682

I am dynamically creating my controls and generating the control IDs at runtime because there are an unknown number of controls this means I cannot use the message map macros.  Is there any way of adding message handlers like this at runtime?  


I am trying to avoid using OnHScroll as the parent view also has a horizontal scrollbar and I feel this would make this area of code more complicated than it need be.  I am already handling messages from a CComboBox in my OnNotify handler and CEdit messages in an OnCommand handler, I don't want to be doing things in three places.  Any other suggestions?  
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 55

Expert Comment

by:Jaime Olivares
ID: 11693831
Try to capture all WM_NOTIFY events in view's PreTranslateMessage(), it's a hard work because you have to discriminate your desired notification correctly.
LVL 16

Expert Comment

ID: 11702044
>I am dynamically creating my controls and generating the control IDs at runtime because there are an unknown number of controls this means I cannot use the
>message map macros.  Is there any way of adding message handlers like this at runtime?

You can use ON_NOTIFY_RANGE macro instead of ON_NOTIFY. You must assign a large enough range and make sure no other kind of control gets in that range.


Author Comment

ID: 11703775

Have a look at what I tried (irrelavant code snipped):  

const int CTRL_BASE_CONTROL = 14000;
const int CTRL_INCREMENT = 999; // Gives range of 1000 unique control IDs

afx_msg void MyView::DoControl( UINT id, NMHDR* pNotifyStruct, LRESULT* result ) {
    TRACE( "MyView::DoControl() entered!\n" );

    TRACE( "MyView::DoControl() exited!\n" );


Still no joy.  I also tried substituting ON_COMMAND_RANGE for ON_NOTIFY_RANGE as this works for CComboBoxes and the CBN_SELENDOK notification message, still nothing.  

Any tips?
LVL 16

Accepted Solution

nonubik earned 1520 total points
ID: 11704118
Seems that the slider notification messages are received through WM_VSCROLL and WM_HSCROLL messages.


void MyView::HScroll ( UINT nSBCode, UINT /*nPos*/ )
      if( nSBCode == TB_THUMBTRACK )

idem for VScroll

Author Comment

ID: 11705140
Wow, it works!  

I'm fairly new to MFC and I've picked (well it was chosen for me) a fairly complex project.  Has to be said that I think its message handling leaves a lot to be desired.  I personally think there should be an alternative to message map macros or even a way to successfully bypass the default message handling (OnCommand and OnNotify should allow you to define your own behaviour).  

Well done on the solution, I thought I'd tried everything (and that I could avoid using the ON_WM_HSCROLL messages) but seemingly I hadn't tried the only thing that actually works.  I just hope that this doesn't screw up the horizontal scrolling of the actual view itself rather than the slider bars inside it.  The docs for CSliderCtrl do not make the behaviour at all obvious.  

I must say that I thought I went through a fairly logical thought process but this is MSFT so who knows!  Hell, I'm using WTL next time, it's free, slimmer and more expressive.  

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question