Solved

Receiving CSliderCtrl notify messages - TB_THUMBTRACK

Posted on 2004-08-02
8
3,737 Views
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
            break;
        }
    }
}

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.  
0
Comment
Question by:rubinho
  • 3
  • 2
  • 2
  • +1
8 Comments
 
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):

BEGIN_MESSAGE_MAP(CYourView, CView)
      //{{AFX_MSG_MAP(CYourView)
      ON_WM_SIZE()
      ON_WM_CREATE()
      //}}AFX_MSG_MAP
      ON_NOTIFY(TB_THUMBTRACK, 1000, OnThumbTrack)      <---------------------
END_MESSAGE_MAP()

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:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/trackbar/trackbar.asp
0
 
LVL 7

Expert Comment

by:vijay_visana
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:
0
 

Author Comment

by:rubinho
ID: 11693682
Jaime:

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?  

Vijay:

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?  
0
 
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Expert Comment

by:nonubik
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.

ON_NOTIFY_RANGE(TB_THUMBTRACK, ID_FIRST, ID_FIRST + MAX_RANGE, OnThumbTrack) //define your own ID_FIRST and MAX_RANGE
0
 

Author Comment

by:rubinho
ID: 11703775
nonubik:

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" );
}

ON_NOTIFY_RANGE( TB_THUMBTRACK, CTRL_BASE_CONTROL, CTRL_BASE_CONTROL + CTRL_INCREMENT, DoControl )

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?
0
 
LVL 16

Accepted Solution

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

ON_WM_HSCROLL( )
ON_WM_VSCROLL( )

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

idem for VScroll
0
 

Author Comment

by:rubinho
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.  
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Include multiple hostnames in this command? 3 53
sameEnds challenge 3 133
isEverywhere  challenge 19 72
java ^ examples 8 62
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…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

911 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

23 Experts available now in Live!

Get 1:1 Help Now