• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3254
  • Last Modified:

handling slider control messages

Hi,

I am having an MFC - SDI application. I added a slider control to the main window (not in a dialog). Actually I wanted to add it to a toolbar.

I did add it to a toolbar, but when I add it to a toolbar I cannot receive WM_HSCROLL events when the slider is moved.

The nature of problem is as below.

I created a slider like this in the main window (child of CFrameWnd)

m_transpSlider = new CSliderCtrl();
m_transpSlider->Create(TBS_HORZ, CRect(310, 2, 410, 22), this, 1); // I am making main window as the parent of slider                  
                                                                                                    // control
m_transpSlider->ShowWindow(SW_SHOW);
m_transpSlider->SetRange(0, 255, true);
m_transpSlider->SetPos(255);
m_transpSlider->EnableWindow(false); // I want it to be disabled first

In the same way I created a toolbar like this,
                                            _______________-> main window (CFrameWnd) is parent
                                           |
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
            | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
            !m_wndToolBar.LoadToolBar(IDR_MAINTOOLBAR))
      {
            TRACE0("Failed to create toolbar\n");
            return -1;      // fail to create
      }

In this class's implementation's message map I have ON_WM_HSCROLL() message map which calls OnHScroll() function.

Everything is fine till here.

But if I change the slider control creation code to make the above toolbar as its parent, like this,

m_transpSlider = new CSliderCtrl();
m_transpSlider->Create(TBS_HORZ, CRect(310, 2, 410, 22), (CWnd *)&m_wndToolBar, 1);

I am not receiving the WM_HSCROLL events and the function OnHScroll() is not being called.

I am getting the slider control in the toolbar but when I move the slider I am not getting any events.

How to handle this issue.

thanks.

0
ramavorray
Asked:
ramavorray
  • 11
  • 8
1 Solution
 
nonubikCommented:
Because the Frame is not the slider's parent, the WM_HSCROLL message has to be treated in the toolbar class (as now it receives the mesasges)
0
 
ramavorrayAuthor Commented:
yes, but how to resolve it.

shall I have to declare a class that is derived from CToolBar and call CreateX from its contructor and shall I have create the slider control in that class and write a message map for WM_HSCROLL in that class
0
 
nonubikCommented:
You do not need to declare a class derived from CToolBar if you don't want. You can simply subclass the control using SubclassWindow. See MSDN on CWnd::SubclassWindow. Make sure to unsubclass it on destroying.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
ramavorrayAuthor Commented:
that looks good, but sorry that I did not understand it completely. MSDN pages are confusing to me.

Can you explain it a bit with respect to the above code, so that I can get a clear idea about it.

thanks.
0
 
ramavorrayAuthor Commented:
This is totally cofusing. I did not get any idea of why this SubclassWindow() is for.  I rea the MSDN pages and some other pages on web. But did not get any idea of what this is for.

what I did not understand is, how will the CFrameWnd class gets messages if we declare slider control as a child of CToolBar and where to use this subclasswindow() thing in order to handle this issue.
0
 
nonubikCommented:
I'm trying to experience this myslef but I have a problem: I cannot seem to make the slider appear when settign the toolbar as parent...
So I cannot test the SubclassWindow thingy yet
0
 
ramavorrayAuthor Commented:
oh, But I can see it in the toolbar if I make it a child of CToolBar

m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);      
DockControlBar(&m_wndToolBar);

I have these two lines of code to create the tool bar in addition to what I gave above, in case this might help you in trying out
0
 
ramavorrayAuthor Commented:
this might help you,

the tool bar should have enough length to accomodate slider control.

so, what I made it I just added some buttons to the toolbar that does nothing and they are overwritten by the Slider when it is displayed. funny way, in QT it is a lot easier.
0
 
nonubikCommented:
I make it appear :)
But you have to forget what I've said about the subclasswindow. It does not work in this case.
Try making a class for the toolbar.
0
 
nonubikCommented:
Or you can write a WindowProc for the toolbar where you handle only the WM_HSCROLL message and then change the wndproc of the toolbar with SetWindowLong.
0
 
ramavorrayAuthor Commented:
I tried making a separate class, but then I did not get the toolbar displayed.

Just a line instead of toolbar is being displayed, no idea why.

I made a class (derived from CToolBar) and I called CreateEx like above from that class, but I could not display it.
0
 
ramavorrayAuthor Commented:
Hi nonubik,

I succedded in making a separate class for the toolbar and attaching a slider control to it and displayed it. I can receive slider controls WM_HSCROLL notifcations in that class itself and I am able to handle it.

Now another issue has come up. when I select the slider control, it is being selected (a dotted line box around control is displayed) and when I click on left or right arrow keys on keyboard the slider control's thumb is changing position but then it doesnt generate WM_HSCROLL message,

I found out that, slider control when operated with keyboard gives messages like TB_LINEDOWN, TB_LINEUP

How to handle these. I used ON_COMMAND(TB_LINEDOWN, memberfxn) with no arguments to member fxn but it did not work

after that I tried using ON_NOTIFY(TB_LINEDOWN, 1, memberfxn)

with arguments NMHDR *, LRESULT * to the memberfxn, even this did not work.

application works well in both cases but those handler fucntions are not being called.

0
 
nonubikCommented:
>when I click on left or right arrow keys on keyboard the slider control's thumb is changing position but then it doesnt generate WM_HSCROLL message,
This sounds strange, you should still receive the WM_HSCROLL. TB_LINEDOW / UP are just the values of the nSBCode.
0
 
ramavorrayAuthor Commented:
oh yes, I was debugging by placing breakpoint with in a condition in the OnHScroll() function which is not reachable by debugger. It was my fault.

Another thing please, when I select the slider control, it is being activated and is displaying a dotted rectangle around it,
after that how to deactivate it. Not disabling it, but just remove it from focus of keyboard. How to just remove the focus of keyboard from the slider control.
0
 
ramavorrayAuthor Commented:
after the slider control is being selected only that slider is receiving the keyboard focus. The CFrameWnd class or CWnd class are not receving the KeyDown or KeyUp events.

Only the slider control is receving the WM_HSCROLL event with TB_LINEUP or TB_LINEDOWN messages. no other key event handlers are receving messages from keyboard after the slider control has been selected and only it is receiving those events.

I did not find any function in CToolBar class that helps in killing the focus from keyboard when not required
0
 
nonubikCommented:
Can use SetFocus()
0
 
ramavorrayAuthor Commented:
yes, thank you very much.

Now everything is working well, as I expected

when the slider control is selected it gets the focus and when user clicks mouse in other windows (like oepngl CWnd class) I am calling SetFocus() from with in that class and then that window is receiving keyboard focus.

I just called SetFocus() in OnMouseLButtonDown() of CWnd and it worked well

thanks
0
 
nonubikCommented:
Glad to have the problem sorted out
0
 
ramavorrayAuthor Commented:
cheers
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 11
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now