1. 程式人生 > >介面程式設計-寫一個滑動條

介面程式設計-寫一個滑動條

//滑動條
class SliderChunk:public ButtonChunk
{
public:
	short nSliderLeft;
	short nSliderRight;
	short nSliderWidth;
	short nSliderHeight;

	short nMin;
	short nMax;
	short nPos;
	float fRate;//表示範圍與實際長度的比率

	BOOL bWholeMove;//整體移動還是保持固定只移動點

	SliderChunk(){
		
		bWholeMove = TRUE;
		nButtonClass = 0;// 行為和普通按鈕一樣 sliderbar
		nButtonStyle = 1;// 外形與普通按鈕不同



	}
	void SliderInit(){
		nSliderHeight = 20;
		nSliderWidth = 200;
		nSliderLeft = Left - nSliderWidth / 2;
		nSliderRight = Left + nSliderWidth / 2;
	}

	void SetRange(short nMyMin, short nMyMax){
		nMin = nMyMin;
		nMax = nMyMax;

		fRate = (nMax - nMin)/(float)(nSliderWidth-Width);
	}

	void SetPos(short nMyPos){

		nPos = nMyPos;

		if (nMyPos < nMin){
			nPos = nMin;
		}

		if (nMyPos>nMax)
			nPos = nMax;

		Left = (nPos - nMin)/fRate + nSliderLeft;
		Right = Left + Width;

		UpdateWordsLeft();
	}

	int GetPosByLocation(){
		if (Left < nSliderLeft)
			Left = nSliderLeft;

		if (Left>nSliderRight - Width)
			Left = nSliderRight - Width;

		nPos = (Left - nSliderLeft)*fRate + nMin;

		if (nPos > nMax)
			nPos = nMax;
		return nPos;
	}

	int GetPosDirect(){
		return nPos;
	}

	void MoveSlider(){
		if (bWholeMove){

			if (Right > nSliderRight)
			{
				nSliderRight = Right;
				nSliderLeft = nSliderRight - nSliderWidth;
			}

			if (Left < nSliderLeft){
				nSliderLeft = Left;
				nSliderRight = nSliderLeft + nSliderWidth;
			}
		}
		else{

			if (Right > nSliderRight)
			{
				Right = nSliderRight;
				Left = Right - Width;
			}

			if (Left < nSliderLeft){
				Left = nSliderLeft;
				Right = Left + Width;
			}

		}
		UpdateWordsLeft();
	}
};