Receiving CSliderCtrl notify messages - TB_THUMBTRACK

Posted on 2004-08-02
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
  • 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?  
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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 380 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
repeatSeparator  java  challenge 13 57
Excel Use VBA to get user's Mac Address for their computer 5 159
tripleUp challenge 7 78
unix example issues 18 89
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

813 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now