⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vscrollbar.cpp

📁 SimpleGraphicOperatingSystem 32位图形化操作系统 多进程 支持FAT32 详见www.sgos.net.cn
💻 CPP
字号:
#include <osdef.h>//WM_**
#include <System.h>
#include <Api.h>	//Sgos api

namespace System{

	class ButtonUp: public Button{
	private:
		VScrollBar* parent;
	public:
		ButtonUp(VScrollBar* p):Button(p, string(" "), 0)
		{
			parent = p;
		}
		virtual int OnMouseDown( int button, Point* p ){
			parent->SetValue2( parent->Value()-parent->MinStep() );
			Button::OnMouseDown( button, p );
		}
		virtual int DrawLabel(int type){
			//画个箭头
			int w = BaseWindow::Width();
			int h = BaseWindow::Height();
			Bitmap::Line( bitmap, w/4, h*3/4, w/2, h/4, Bitmap::BLACK );
			Bitmap::Line( bitmap, w/2, h/4, w*3/4, h*3/4, Bitmap::BLACK );
			Bitmap::Line( bitmap, w/4, h*3/4, w*3/4, h*3/4, Bitmap::BLACK );
			Label::DrawLabel(type);
		}
	};
	class ButtonDown: public Button{
	private:
		VScrollBar* parent;
	public:
		ButtonDown(VScrollBar* p):Button(p, string(" "), 0)
		{
			parent = p;
		}
		virtual int OnMouseDown( int button, Point* p ){
			parent->SetValue2( parent->Value()+parent->MinStep() );
			Button::OnMouseDown( button, p );
		}
		virtual int DrawLabel(int type){
			//画个箭头
			int w = BaseWindow::Width();
			int h = BaseWindow::Height();
			Bitmap::Line( bitmap, w/4, h/4, w/2, h*3/4, Bitmap::BLACK );
			Bitmap::Line( bitmap, w/2, h*3/4, w*3/4, h/4, Bitmap::BLACK );
			Bitmap::Line( bitmap,  w/4, h/4, w*3/4, h/4, Bitmap::BLACK );
			Label::DrawLabel(type);
		}
	};
	class ButtonMid: public Button{
	private:
		VScrollBar* parent;
		int bCatched;
		int oldWinY, oldWinX, oldMouseRelY;
	public:
		ButtonMid(VScrollBar* p):Button(p, string(" "), 0)
		{
			bCatched = false;
			parent = p;
		}
		virtual int OnMouseDown( int button, Point* p ){
			Point gM;
			Rect r;
			Mouse::GetMousePoint(&gM);	//we get the global mouse position.
			GetRect( r );
			oldMouseRelY = gM.y;
			oldWinY = r.top;
			oldWinX = r.left;
			bCatched = true;
			Button::OnMouseDown( button, p );
		}
		virtual int OnMouseUp( int button, Point* p ){
			bCatched = false;
			Button::OnMouseUp( button, p );
		}
		virtual int OnMouseLeave( int button ){
			bCatched = false;
			Button::OnMouseLeave( button );
		}
		virtual int OnMouseMove( Point* p ){
			if( bCatched ){
				short y;
				Point gM;
				Mouse::GetMousePoint(&gM);	//we get the global mouse position.
				y = gM.y-oldMouseRelY + oldWinY;
				//DPRINT("x=%d y=%d mx=%d my=%d newx=%d newy=%d", x, y, mx, my, x+winX, y+winY);
				int upup = parent->GetUpButton()->Height(),
					downdown = ((BaseWindow*)parent)->Height()-BaseWindow::Height()-parent->GetDownButton()->Height();
				int y2;
				if( y<upup )
					y2=upup;
				else if ( y>downdown )
					y2=downdown;
				else
					y2=y;
				//Move( oldWinX, y2, 0, 0 );
				if(downdown-upup>0){
					int t=(parent->Max()-parent->Min())*(y2-upup)/(downdown-upup);
					parent->SetValue2( t );
				}
				Button::OnMouseMove( p );
			}
		}
	};

	int VScrollBar::OnSize( int w, int h ){
		Width = w;
		Height = h;

		if( w<btnMid->Width() ){
			SetSize( btnUp->Width(), h );
			return -1;
		}
		if( h>=btnUp->Height()+btnDown->Height()+btnMid->Height() )
		{
			btnUp->Move(0,0,w,btnUp->Height());
			btnDown->Move(0,Height-btnDown->Height(),w,btnUp->Height());
			btnMid->Move(0,btnUp->Height(),w,btnUp->Height());
		}else if( h>=btnUp->Height()+btnDown->Height() ){
			btnUp->Move(0,0,w,btnUp->Height());
			btnDown->Move(0,Height-btnDown->Height(),w,btnUp->Height());
		}else{
			SetSize( w, btnUp->Height()+btnDown->Height() );
			return -1;
		}
		BaseWindow::OnSize(w,h);
		return 0;
	}
	VScrollBar::~VScrollBar(){
		delete btnUp;
		delete btnDown;
		delete btnMid;
	}

	VScrollBar::VScrollBar( BaseWindow* form, int style ):
		BaseWindow( form, string("VScroll Bar"), 0 )
	{
		type = TypeVScrollBar;
		bitmap->BackColor = Bitmap::LIGHTGRAY;
		btnUp = new ButtonUp(this);
		btnDown = new ButtonDown(this);
		btnMid = new ButtonMid(this);
		btnUp->Show();
		btnDown->Show();
		btnMid->Show();
		this->style = style;
		Width = btnUp->Width();
		Height = BaseWindow::Height();
		SetSize( Width, Height );
		Set( 0, 100, 1, 20 );
	}

	int VScrollBar::DrawVScrollBar(int type){

		Bitmap::RectFill( bitmap, 0, 0, Width-1, Height-1, Bitmap::LIGHTGRAY );
		btnUp->OnPaint();
		btnDown->OnPaint();
		btnMid->OnPaint();
		Refresh();
	}

	int VScrollBar::OnPaint(){
		DrawVScrollBar(0);
		BaseWindow::OnPaint();
	}

	int VScrollBar::OnMouseDown( int button, Point* p ){
		int upup = btnUp->Height(),
			downdown = Height-btnDown->Height(),
			up = btnMid->Top(),
			down = btnMid->Top()+btnMid->Height();
		if( p->y>upup&&p->y<up )
		{
			SetValue2(value-maxStep);
		}else if(p->y>down&&p->y<downdown){
			SetValue2(value+maxStep);
		}
	}

	int VScrollBar::OnChange(int v){
	}
	int VScrollBar::Max()
	{
		return max;
	}

	int VScrollBar::SetValue(int v){
		if( v<min )v=min;
		if( v>max )v=max;
		value = v;
		int t = (v-min)*(Height-btnUp->Height()-btnDown->Height()-btnMid->Height())/(max-min);
		btnMid->Move( 0, t+btnUp->Height(), 0, 0 );
		return true;
	}

	int VScrollBar::SetValue2(int v){
		if( v<min )v=min;
		if( v>max )v=max;
		SetValue(v);
		OnChange(v);
	}

	int VScrollBar::Set( int m_min, int m_max, int m_minStep, int m_maxStep ){
		min = m_min;
		max = m_max;
		minStep = m_minStep;
		maxStep = m_maxStep;
		value = min;
		if( m_max<=m_min ){
			max=1;
			btnMid->Move( 0, btnUp->Height(), btnMid->Width(), (Height-btnUp->Height()-btnDown->Height()) );
		}else{
			int t = maxStep*(Height-btnUp->Height()-btnDown->Height())/(max-min+maxStep);
			if( t<1 ) t = 2;
			btnMid->Move( 0, btnUp->Height(), btnMid->Width(), t );
		}
		Refresh();
		return true;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -