📄 cslider.cpp
字号:
//===========================================================================
// CSlider.cpp : implementation file
//===========================================================================
#include "stdafx.h"
#include "CSlider.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include "resource.h"
//===========================================================================
// CSlider
//===========================================================================
CSlider::CSlider()
{
m_bDragging = FALSE;
m_ThumbPos = 0;
m_Pos = 0;
m_Min = m_Max = 0;
m_Callback = NULL;
m_param = NULL;
m_brush = ::CreateSolidBrush( RGB(128,128,128) );
m_bmpBack1 = NULL;
m_bmpBack2 = NULL;
m_bmThumb = NULL;
}
//===========================================================================
// ~CSlider
//===========================================================================
CSlider::~CSlider()
{
if ( m_bmpBack1 ) delete m_bmpBack1;
if ( m_bmpBack2 ) delete m_bmpBack2;
if ( m_bmThumb ) delete m_bmThumb;
}
//===========================================================================
// BEGIN_MESSAGE_MAP
//===========================================================================
BEGIN_MESSAGE_MAP(CSlider, CNCtrl)
END_MESSAGE_MAP()
//===========================================================================
// OnMouseMove
//===========================================================================
void CSlider::OnMouseMove(UINT nFlags, POINT point)
{
// TRACE(_T("x= %d, y= %d \n"), (short)point.x, point.y);
int width = m_Rect.right - m_Rect.left;
if( m_bDragging )
{
int x = (short)point.x;
if ( x < 0 )
x = 0;
else if ( x >= width )
x = width - 1;
m_ThumbPos = x;
m_Pos = (m_ThumbPos * (m_Max-m_Min))/ width;
if ( m_Callback ) m_Callback(m_param);
InvalidateRect(NULL, FALSE);
}
CNCtrl::OnMouseMove(nFlags, point);
}
//===========================================================================
// OnPaint
//===========================================================================
void CSlider::OnDraw(HDC hdc)
{
RECT rect;
GetClientRect(&rect);
FillRect(hdc, &rect, m_brush);
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
m_bmpBack1->Draw( hdc, rect.left, rect.top, width, height );
m_bmpBack2->Draw( hdc, rect.left, rect.top, m_ThumbPos, height );
int pos = m_ThumbPos-6;
if( pos < 0 )
pos = 0;
m_bmThumb->DrawTrans(hdc, rect.left + pos, rect.top + 2);
}
//===========================================================================
// OnLButtonUp
//===========================================================================
void CSlider::OnLButtonUp(UINT nFlags, POINT point)
{
ReleaseCapture();
int width = m_Rect.right - m_Rect.left;
int x = (short)point.x;
if ( x < 0 )
x = 0;
else if ( x >= width )
x = width-1;
m_ThumbPos = x;
m_Pos = (m_ThumbPos * (m_Max-m_Min))/ width;
m_bDragging = FALSE;
if ( m_Callback ) m_Callback(m_param);
InvalidateRect(NULL, FALSE);
// CNCtrl::OnLButtonUp(nFlags, point);
}
//===========================================================================
// OnLButtonDown
//===========================================================================
void CSlider::OnLButtonDown(UINT nFlags, POINT point)
{
SetCapture(GetSafeHwnd());
int width = m_Rect.right - m_Rect.left;
int x = (short)point.x;
if ( x < 0 )
x = 0;
else if ( x >= width )
x = width-1;
m_ThumbPos = x;
m_Pos = (m_ThumbPos * (m_Max-m_Min))/ width;
m_bDragging =TRUE;
InvalidateRect(NULL, FALSE);
if ( m_Callback ) m_Callback(m_param);
// CNCtrl::OnLButtonDown(nFlags, point);
}
//===========================================================================
// SetRange
//===========================================================================
void CSlider::SetRange(int nMin, int nMax, BOOL bRedraw)
{
m_Min = nMin;
m_Max = nMax;
GetClientRect( &m_Rect );
}
void CSlider::GetRange(int* nMin, int *nMax)
{
*nMin = m_Min;
*nMax = m_Max;
}
//===========================================================================
// SetSkin
//===========================================================================
void CSlider::SetSkin( UINT uiBackGround1, UINT uiBackGround2, UINT uiTickImage )
{
m_bmpBack1 = new CZipBitmap();
m_bmpBack1->LoadBitmap(MAKEINTRESOURCE(uiBackGround1));
m_bmpBack2 = new CZipBitmap();
m_bmpBack2->LoadBitmap(MAKEINTRESOURCE(uiBackGround2));
m_bmThumb = new CZipBitmap();
m_bmThumb->LoadBitmap(MAKEINTRESOURCE(uiTickImage));
}
//===========================================================================
// SetCallback
//===========================================================================
void CSlider::SetCallback( funcCallback *pCallback, void *param )
{
m_Callback = pCallback;
m_param = param;
}
//===========================================================================
// GetPos
//===========================================================================
UINT32 CSlider::GetPos()
{
return m_Pos;
}
//===========================================================================
// SetPos
//===========================================================================
UINT32 CSlider::SetPos( UINT32 nPos )
{
if ( m_Max <= m_Min )
return 0;
int width = m_Rect.right - m_Rect.left;
UINT32 Pos = m_Pos;
m_Pos = nPos;
__int64 temp = nPos;
m_ThumbPos = (int)(temp * width / (m_Max-m_Min));
RedrawWindow(GetSafeHwnd(), NULL, NULL, RDW_INVALIDATE | RDW_ERASENOW);
return Pos;
}
//===========================================================================
// RefreshRect
//===========================================================================
void CSlider::RefreshRect()
{
GetClientRect( &m_Rect );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -