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:
- I derived a class
clsMySlider
fromCSliderCtrl
, overloaded itsOnHScroll
method - Derived a class from
CMFCToolbarButton
clsMySliderButton
and put an instance ofclsMySlider
inside it. - Added a method
Initialize
toclsMySliderButton
and create instance ofclsMySlider
in this method m_pParent
andm_pToolbar
areCWnd*
of mainframe window and MFC toolbar pointers- Used
CMFCToolbar::ReplaceButton
to replace my first and second button of toolbar with 2 instances ofclsMySliderButton
- 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);
WM_SLIDER_HSCROLL
message? If you meantWM_HSCROLL
, then it looks like yourwParam
andlParam
are wrong (mixed-up)?CDialogBar
class instead ofCMFCToolBar
, as that allows for more types of embedded controls.CDialogBar
would be better - it allows you to addDECLARE_MESSAGE_MAP()
in your derived class and then implement aBEGIN_MESSAGE_MAP()
which has the handlers for the various slider control messages.