0

enter image description here

I need multiple slider in MFCToolbar as its buttons. I can put slider in toolbar and shows slider. But I can't send slider position change message to parent toolbar or parent mainframe class. Also can not show tooltip for current position of slider.

These are steps to do it:

  1. I derived a class clsMySlider from CSliderCtrl, overloaded its OnHScroll method
  2. Derived a class from CMFCToolbarButton clsMySliderButton and put an instance of clsMySlider inside it.
  3. Added a method Initialize to clsMySliderButton and create instance of clsMySlider in this method
  4. m_pParent and m_pToolbar are CWnd* of mainframe window and MFC toolbar pointers
  5. Used CMFCToolbar::ReplaceButton to replace my first and second button of toolbar with 2 instances of clsMySliderButton
  6. Overloaded OnHScroll method in MainFarme window to catch messages which Windows send to main frame
void clsMySlider::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    // TODO: Add your message handler code here and/or call default
    ::PostMessage(m_pParent->GetSafeHwnd(), WM_SLIDER_HSCROLL, NULL, nPos);
    ::PostMessage(m_pToolbar->GetSafeHwnd(), WM_SLIDER_HSCROLL, NULL, nPos);

    CSliderCtrl::OnHScroll(nSBCode, nPos, pScrollBar);
}
void CMainFrame::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    // TODO: Add your message handler code here and/or call default
        int dummyVar=nPos;

    CFrameWndEx::OnHScroll(nSBCode, nPos, pScrollBar);
}

but problem is OnHScroll never gets called in clsMySlider or CMainFrame window.

I appreciate your help.please let me know how to send position change events to parent toolbar and parent mainframe window and how can I show tooltip to show slider position.

class clsMySlider : public CSliderCtrl
{
    DECLARE_DYNAMIC(clsMySlider)

public:
    clsMySlider(CWnd* parent, CMFCToolBar* toolbar);
    virtual ~clsMySlider();

protected:
    DECLARE_MESSAGE_MAP()

    CWnd* m_pParent;
    CMFCToolBar* m_pToolbar;
public:
    afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
    afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
};

class clsMySliderButton : public CMFCToolBarButton
{
public:
    clsMySliderButton ();
    virtual ~clsMySliderButton ();

public:
    CWnd* GetParentWnd();
    
    virtual BOOL CanBeDropped(CMFCToolBar* pToolbar) { return FALSE; }//So user can not position this button on a toolbar or menu during customization.
    virtual BOOL CanBeStored() const { return FALSE; }//So button cannot be stored as part of a drag-and-drop operation
    virtual BOOL CanBeStretched() const { return TRUE; }//So button can be stretched in customization mode
    virtual HWND GetHwnd();
    virtual const CRect GetInvalidateRect() const;
    virtual BOOL HaveHotBorder() const { return TRUE; }//So a border of the button is not displayed when a user selects the button
    virtual BOOL OnBeforeDrag() const { return FALSE; }//So disable dragging of the button
    virtual SIZE OnCalculateSize(CDC* pDC, const CSize& sizeDefault, BOOL bHorz);
    virtual void OnDraw(CDC* pDC,const CRect& rect,CMFCToolBarImages* pImages,BOOL bHorz = TRUE,BOOL bCustomizeMode = FALSE,BOOL bHighlight = FALSE,BOOL bDrawBorder = TRUE,BOOL bGrayDisabledButtons = TRUE);
    //virtual BOOL OnGetCustomToolTipText(CString& strToolTip);
    virtual void OnSize(int iSize);
    virtual BOOL SetACCData(CWnd* pParent, CAccessibilityData& data) { return FALSE; }//So toolbar button does not provide accessibility data
    virtual void SetStyle(UINT nStyle);
    BOOL Initialize(CRect r, CWnd* pParent, CMFCToolBar* pToolbar);
    virtual HBRUSH OnCtlColor(CDC* pDC, UINT nCtlColor)
    {
        CBrush b;
        b.CreateSolidBrush(NULL_BRUSH);
        return (HBRUSH)b.GetSafeHandle();
    }
    virtual BOOL OnUpdateToolTip(CWnd* pWndParent, int iButtonIndex, CToolTipCtrl& wndToolTip, CString& str)
    {
        if (!m_pSlider || iButtonIndex!=10)
            return FALSE;
        str.Format(L"Current Value=%d", m_pSlider->GetPos());
        return TRUE;
    }
    virtual BOOL OnToolHitTest(const CWnd* pWnd,TOOLINFO* pTI);
    virtual BOOL IsOwnerOf(HWND hwnd) 
    {
        if (!m_pSlider)
            return FALSE;
        return hwnd == m_pSlider->GetSafeHwnd(); 
    }
    virtual void OnChangeParentWnd(CWnd* pWndParent)
    {

    }
private :
    clsMySlider* m_pSlider;
    CMFCToolTipCtrl* m_pTooltip;
    CWnd* m_pParent;

};

 //Inside CMainFrame::OnCreate
//CMFCToolBar       m_wndToolBar;
//clsMySliderButton m_ZoomSlider;
       CRect r;
    m_wndToolBar.GetItemRect(10, &r);
    r.right += r.Width() * 3;
    m_ZoomSlider.Initialize(r, this, &m_wndToolBar);
    m_wndToolBar.ReplaceButton(10, m_ZoomSlider);
    m_wndToolBar.UpdateButton(10);
5
  • What is the WM_SLIDER_HSCROLL message? If you meant WM_HSCROLL, then it looks like your wParam and lParam are wrong (mixed-up)? Commented Feb 3 at 14:22
  • But, maybe not really helpful, but you should probably consider using a CDialogBar class instead of CMFCToolBar, as that allows for more types of embedded controls. Commented Feb 3 at 14:28
  • That's a user-defined message that I want to send to parent window.but program control never reach there.First I thought when slider changes position,CSliderCtrl::OnHScroll is called,but it not happened,Then I though maybe Windows send HScroll message to parent windows so I overloaded CMainFrame::OnHScroll,still no success.The only thing I didn't check is maybe Windows send HScroll message to CMFCToolbar object.I will check it and then write the result
    – R1349
    Commented Feb 3 at 15:37
  • 1
    That's why I think a toolbar derived from CDialogBar would be better - it allows you to add DECLARE_MESSAGE_MAP() in your derived class and then implement a BEGIN_MESSAGE_MAP() which has the handlers for the various slider control messages. Commented Feb 3 at 15:42
  • Right,your suggestion works.this solution also works.I derived a class from CMFCToolbar and overloadded its WM_HSCROLL message handler as you suggested.each slider control in this toolbar sends its message to this toolbar and I could catch it inside this handler.thank you for nice suggestion which game me a clue for solution.
    – R1349
    Commented Feb 4 at 19:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.