tags:

views:

329

answers:

1

I design a class which derives from CScrollBar in a dialog-based MFC application. In the dialog class (MyScrollbarDlg), I set the ON_WM_VSCROLL message and declare the OnVSCroll method in the .h file. I also Implement the OnVScroll() method in the corresponding .cpp file. But to my surprise, when I click the arrow at the buttom of of the scrollbar, it only did its work at the first time.

I want to debug my code, so I set a break point in the OnVScroll method. Then strange thing happened. The break point was only arrived at the first click. When I continue, the scrollbar disappeared from the dialog! I've no idea how this could happen. Could anyone help me to fix this annoying bug? Thank you very much!

I have set the scroll range like below: SCROLLINFO scrllInfo; m_scrollbar.GetScrollInfo(&scrllInfo, SIF_ALL); scrllInfo.nMax = 15; scrllInfo.nMin = 0; scrllInfo.nPage = 0; scrllInfo.nPos = 0; m_scrollbar.SetScrollInfo(&scrllInfo, TRUE);

btw, I don't use the embedded scrollbar of the dialog, but a scrollbar control which set to be a member of the dialog.

I did some experiment and found that the problem I mentioned only occurred when I click the arrow at the bottom of the scrollbar control. It acts well when I click the other arrow or drag the scroll box. Is there anything different when clicking the bottom arrow of a scrollbar which I am not aware of?

I figured out that when the scroll message is NOT THUMBPOSITION or THUMBTRACK, the parameter nPos of OnVScroll() method is not used. So I changed my code like this:

int currPos = m_scrollbar.GetScrollPos(); m_scrollbar.SetScrollPos(currPos + 1, TRUE);

This time it acts normal, but if I set a break point, the scrollbar disappeared again. Do you know why?

+1  A: 

Make sure you have set the scroll range. If you don't set this, your method will only get called once.

Been there...


{edit} I've had better luck with

m_ScrollBar.SetScrollRange(min, max);

than setting scrollinfo.

You might want to check the flags field to see if it is set properly before saving.

Also, double check that you call

m_ScrollBar.SetScrollPos(CurPos);

to set the position once you click on the button

Brad Bruce