The CAdjustSlider that I overwrite from CSliderCtrl can not send WM_HSCROLL message when i drag the

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I writed a class named CADjustSlider which inherited from CSliderCtrl, but when i drag the slider of the ctrl with mouse, its parent windows WM_HSCROLL message handler does not response.

thank you very much for reading and giving some advices to me.
the header file:
#if !defined(AFX_MYSLIDER_H__E434F151_26B7_45C1_89F0_2E59B3D642E2__INCLUDED_)<br/>
#define AFX_MYSLIDER_H__E434F151_26B7_45C1_89F0_2E59B3D642E2__INCLUDED_
#if _MSC_VER > 1000<br/>
#pragma once<br/>
#endif // _MSC_VER > 1000<br/>
<br/>
// CAdjustSlider window
class CAdjustSlider : public CSliderCtrl<br/>
{<br/>
// Construction<br/>
public:<br/>
CAdjustSlider();
// Attributes<br/>
public:
// Operations<br/>
public:
// Overrides<br/>
// ClassWizard generated virtual function overrides<br/>
//{{AFX_VIRTUAL(CAdjustSlider)<br/>
protected:<br/>
virtual void PreSubclassWindow();<br/>
//}}AFX_VIRTUAL
// Implementation<br/>
public:<br/>
virtual ~CAdjustSlider();
// Generated message map functions<br/>
protected:<br/>
//{{AFX_MSG(CAdjustSlider)<br/>
afx_msg void OnPaint();<br/>
afx_msg BOOL OnEraseBkgnd(CDC* pDC);<br/>
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);<br/>
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);<br/>
afx_msg void OnMouseMove(UINT nFlags, CPoint point);<br/>
//}}AFX_MSG<br/>
private:<br/>
bool m_flagLBtnDownInThumb; // true:鼠标左键在thumb区域被按下;<br/>
CRect m_rtClient; // slider控件的客户区域;<br/>
CRect m_rtChannel; // channel的矩形区域大小;<br/>
CRect m_rtThumb; // thumb的矩形区域大小;<br/>
CRect m_rtCurPosThumb; // thumb当前的区域;<br/>
CPoint m_ptInChannel;<br/>
CBitmap m_ThumbMask;<br/>
CBitmap m_ThumbBmp;<br/>
COLORREF m_BackColor; // thumb滑块区域的背景色,一般和控件所在对话框的颜色相同,重绘时,会用到该颜色;<br/>
public:<br/>
void SetBkcolor(COLORREF rgb); // 设置m_BackColor;<br/>
void SetPos(int nPos);<br/>
DECLARE_MESSAGE_MAP()<br/>
afx_msg void OnTimer(UINT_PTR nIDEvent);<br/>
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}<br/>
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_MYSLIDER_H__E434F151_26B7_45C1_89F0_2E59B3D642E2__INCLUDED_)
the source file:

#include "stdafx.h"<br/>
#include "AdjustSlider.h"
<br/>
#ifdef _DEBUG<br/>
#define new DEBUG_NEW<br/>
#undef THIS_FILE<br/>
static char THIS_FILE[] = __FILE__;<br/>
#endif
/////////////////////////////////////////////////////////////////////////////<br/>
// CAdjustSlider
CAdjustSlider::CAdjustSlider()<br/>
{<br/>
m_flagLBtnDownInThumb = false;<br/>
m_ThumbMask.LoadBitmap(IDB_BITMAP_THUMBMASK);<br/>
m_ThumbBmp.LoadBitmap(IDB_BITMAP_THUMB);<br/>
m_BackColor = RGB(211, 211, 211);<br/>
}
CAdjustSlider::~CAdjustSlider()<br/>
{<br/>
if (m_ThumbBmp.m_hObject)<br/>
{<br/>
m_ThumbBmp.DeleteObject();<br/>
}<br/>
if (m_ThumbMask.m_hObject)<br/>
{<br/>
m_ThumbMask.DeleteObject();<br/>
}<br/>
}
<br/>
BEGIN_MESSAGE_MAP(CAdjustSlider, CSliderCtrl)<br/>
//{{AFX_MSG_MAP(CAdjustSlider)<br/>
ON_WM_PAINT()<br/>
ON_WM_ERASEBKGND()<br/>
ON_WM_LBUTTONUP()<br/>
ON_WM_LBUTTONDOWN()<br/>
ON_WM_MOUSEMOVE()<br/>
//}}AFX_MSG_MAP<br/>
ON_WM_TIMER()<br/>
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////<br/>
// CAdjustSlider message handlers
void CAdjustSlider::OnPaint() <br/>
{<br/>
CPaintDC dc(this); // device context for painting<br/>
<br/>
// TODO: Add your message handler code here<br/>
<br/>
dc.FillRect(&m_rtClient, &CBrush(m_BackColor));<br/>
dc.FillRect(&m_rtChannel, &CBrush(RGB(255, 255, 255)));<br/>
<br/>
<br/>
<br/>
CDC *pDC, pMemDC, pMaskDC;<br/>
pDC = GetDC();<br/>
pMemDC.CreateCompatibleDC(pDC);<br/>
pMaskDC.CreateCompatibleDC(pDC);
pMemDC.SelectObject(&m_ThumbBmp);<br/>
pMaskDC.SelectObject(&m_ThumbMask);
pDC->BitBlt(m_rtCurPosThumb.left, m_rtCurPosThumb.top, m_rtThumb.Width(), m_rtThumb.Height(), &pMaskDC, 0, 0, MERGEPAINT);<br/>
pDC->BitBlt(m_rtCurPosThumb.left, m_rtCurPosThumb.top, m_rtThumb.Width(), m_rtThumb.Height(), &pMemDC, 0, 0, SRCAND);
ReleaseDC(pDC);
// Do not call CSliderCtrl::OnPaint() for painting messages<br/>
}
BOOL CAdjustSlider::OnEraseBkgnd(CDC* pDC) <br/>
{<br/>
// TODO: Add your message handler code here and/or call default<br/>
<br/>
return true;<br/>
}
void CAdjustSlider::OnLButtonUp(UINT nFlags, CPoint point) <br/>
{<br/>
// TODO: Add your message handler code here and/or call default<br/>
if (m_flagLBtnDownInThumb)<br/>
{<br/>
m_flagLBtnDownInThumb = false;<br/>
ReleaseCapture();<br/>
}<br/>
<br/>
CSliderCtrl::OnLButtonUp(nFlags, point);<br/>
}
void CAdjustSlider::OnLButtonDown(UINT nFlags, CPoint point) <br/>
{<br/>
// TODO: Add your message handler code here and/or call default<br/>
if (m_rtCurPosThumb.PtInRect(point))<br/>
{<br/>
m_flagLBtnDownInThumb = true; <br/>
SetCapture();<br/>
}<br/>
else if (m_rtChannel.PtInRect(point))<br/>
{<br/>
int nMax = 0; <br/>
int nMin = 0; <br/>
GetRange(nMin,nMax); <br/>
int nPos = (nMax - nMin)*(point.x - m_rtClient.left - m_rtChannel.left)/(m_rtChannel.right - m_rtChannel.left);
<br/>
nPos += nMin;<br/>
SetPos(nPos); <br/>
<br/>
Invalidate();<br/>
CSliderCtrl::OnLButtonDown(nFlags, point);<br/>
}<br/>
}
void CAdjustSlider::OnMouseMove(UINT nFlags, CPoint point) <br/>
{<br/>
// TODO: Add your message handler code here and/or call default<br/>
if (MK_LBUTTON == nFlags)<br/>
{<br/>
if (m_flagLBtnDownInThumb)<br/>
{<br/>
if (point.x > m_rtChannel.right)<br/>
{<br/>
point.x = m_rtChannel.right;<br/>
}<br/>
if (point.x < m_rtChannel.left)<br/>
{<br/>
point.x = m_rtChannel.left;<br/>
}
int nMax = 0; <br/>
int nMin = 0; <br/>
GetRange(nMin,nMax); <br/>
int nPos = (nMax - nMin)*(point.x - m_rtClient.left - m_rtChannel.left)/(m_rtChannel.right - m_rtChannel.left);
<br/>
nPos += nMin;<br/>
SetPos(nPos);<br/>
Invalidate();<br/>
//::SendMessage(GetParent()->GetSafeHwnd(), WM_HSCROLL, TB_THUMBTRACK, 0);<br/>
}<br/>
}
CSliderCtrl::OnMouseMove(nFlags, point);<br/>
}

void CAdjustSlider::PreSubclassWindow() <br/>
{<br/>
// TODO: Add your specialized code here and/or call the base class<br/>
GetClientRect(&m_rtClient);<br/>
GetChannelRect(&m_rtChannel);<br/>
GetThumbRect(&m_rtThumb);
m_rtChannel.bottom = m_rtChannel.top + 5;<br/>
m_rtThumb.top = m_rtChannel.bottom + 1;<br/>
m_rtThumb.bottom = m_rtThumb.top + 11;<br/>
m_rtThumb.left = m_rtChannel.left - 5;<br/>
m_rtThumb.right = m_rtThumb.left + 11;
m_rtCurPosThumb = m_rtThumb;
m_ptInChannel.x = m_rtCurPosThumb.left + m_rtCurPosThumb.Width() / 2;<br/>
m_ptInChannel .y = m_rtChannel.top + m_rtChannel.Height() / 2;
<br/>
CSliderCtrl::PreSubclassWindow();<br/>
}
void CAdjustSlider::SetBkcolor(COLORREF rgb)<br/>
{<br/>
m_BackColor = rgb;<br/>
}
void CAdjustSlider::SetPos(int nPos)<br/>
{<br/>
int min, max;<br/>
GetRange(min, max);<br/>
<br/>
if (nPos < min)<br/>
{<br/>
nPos = min;<br/>
}<br/>
if (nPos > max)<br/>
{<br/>
nPos = max;<br/>
}<br/>
<br/>
int temp = (m_rtChannel.right - m_rtChannel.left) * (nPos - min) / (max - min);<br/>
m_rtCurPosThumb.left = m_rtChannel.left + temp - m_rtThumb.Width() / 2;<br/>
if (m_rtCurPosThumb.left < m_rtChannel.left - m_rtThumb.Width() / 2)<br/>
{<br/>
m_rtCurPosThumb.left = m_rtChannel.left - m_rtThumb.Width() / 2;<br/>
}<br/>
if (m_rtCurPosThumb.left > m_rtChannel.right - m_rtThumb.Width() / 2)<br/>
{<br/>
m_rtCurPosThumb.left = m_rtChannel.right - m_rtThumb.Width() / 2;<br/>
}<br/>
m_rtCurPosThumb.right = m_rtCurPosThumb.left + m_rtThumb.Width();<br/>
m_rtCurPosThumb.bottom = m_rtCurPosThumb.top + m_rtThumb.Height();<br/>
m_rtCurPosThumb.top = m_rtChannel.bottom + 1;
CSliderCtrl::SetPos(nPos);<br/>
}
void CAdjustSlider::OnTimer(UINT_PTR nIDEvent)<br/>
{<br/>
// TODO: Add your message handler code here and/or call default<br/>
if (nIDEvent == 1)<br/>
{<br/>
SendMessage(WM_LBUTTONDOWN);<br/>
//SetTimer(1, 100, NULL);<br/>
}<br/>
CSliderCtrl::OnTimer(nIDEvent);<br/>
}




View the full article
 
Back
Top