Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 11661

Put multiple CSliderCtrl in CMFCToolbar as toolbar buttons

$
0
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 CMFCToolbarButtonclsMySliderButton 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);

Viewing all articles
Browse latest Browse all 11661

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>