using System; using System.Windows.Forms; using System.Drawing; public class Scroll : Panel { public delegate void HandlerOI(object o, int arg); public event HandlerOI BaseChangedAnyway, BaseChangedMechanical, BaseChangedByCode; protected void OnBaseChangedAnyway(object o, int i) { } // ==================================================================================== protected void OnBaseChangedMechanical(object o, int i) { } // ==================================================================================== protected void OnBaseChangedByCode(object o, int i) { } // ==================================================================================== protected override void OnSizeChanged(EventArgs e) { if (this.Width < 15) {this.Width=15; } if (this.Height < 15) {this.Height=15; } this._setwhkernel(); } // ==================================================================================== protected override void OnMove(EventArgs e) { base.OnMove(e); } // ==================================================================================== public int getbase() { return bas; } // ==================================================================================== public void setbase(double itf) { bas=(int)Math.Floor(itf); this._setcode(); } // ==================================================================================== public void setwhole(double itf) { whole=(int)(itf); _setcode(); } // ==================================================================================== public void setview(double itf) { this.view=(int)itf; this._setcode(); } // ==================================================================================== public void setlargeChange(double itf) { this.largechange=(int)itf; } // ==================================================================================== public void setinverse(bool f) { this.inversef=f; this._setM(); } // ==================================================================================== public void setx(int itf) { this.Location = new Point(itf, this.Location.Y); } // ==================================================================================== public void sety(int itf) { this.Location = new Point(this.Location.X, itf); } // ==================================================================================== public void setw(int itf) { this.setwh(itf, this.Height); } // ==================================================================================== public void seth(int itf) { this.setwh(this.Width, itf); } // ==================================================================================== // ###################################################################################### // ################################ public <---> private ################################ // ###################################################################################### private int Q, pwhole, pview; private int bas=0, whole=100, view=0, largechange=10, pbase=0; private bool inversef = false; private bool dragging=false; // , nn=typeof(xtf); private int devipivot=0; private Button btfrom = new Button(), btto = new Button(), btM = new Button(); public Scroll() { this.BackColor = Color.LightGray; Controls.AddRange(new Control[]{ btfrom, btto, btM }); btfrom.BackColor = btto.BackColor = btM.BackColor = Color.Azure; BaseChangedAnyway += OnBaseChangedAnyway; BaseChangedMechanical += OnBaseChangedMechanical; BaseChangedByCode += OnBaseChangedByCode; this._setwhkernel(); this.btfrom.Click += (o,e)=>{ this.bas--; if (this.bas < 0) {this.bas=0; } this._setmechaFTG(); }; this.btto.Click+= (o, e)=>{ this.bas++; if (this.bas > this.whole-this.view) {this.bas=this.whole-this.view; } if (this.bas < 0) {this.bas=0; } this._setmechaFTG(); }; this.MouseDown += (o, e) => { int pv; if (this.Width > this.Height) {pv = e.X-Q; } else {pv = e.Y-Q; } // temporary if (this.inversef) {pv=this.pwhole-pv; } if (pv > this.pbase) { this.bas += this.largechange; if (this.bas > this.whole-this.view) {this.bas=this.whole-this.view; } } else { this.bas -= this.largechange; if (this.bas < 0) {this.bas=0; } } this._setmechaFTG(); }; this.btM.MouseDown += (o,edummy)=>{ Point e=MousePosition; int xy=e.X; if (this.Width < this.Height) {xy=e.Y; } if (this.inversef) { this.devipivot=-xy-this.pbase; } else { this.devipivot=xy-this.pbase; } this.dragging=true; }; this.btfrom.MouseMove += (o,e)=>{ screen(e); this._shift(e); }; this.btto.MouseMove += (o,e)=>{ screen(e); this._shift(e); }; this.MouseMove += (o,e) => { screen(e); this._shift(e); }; this.btM.MouseMove += (o,e)=>{ screen(e); this._shift(e); }; this.btM.MouseUp+= (o,e) => { this._shift(e); }; } // ==================================================================================== private void setwh(int wtf, int htf) { this.Width=wtf; this.Height=htf; if (wtf < 15) {this.Width=15; } if (htf < 15) {this.Height=15; } this._setwhkernel(); } // ==================================================================================== private void screen(MouseEventArgs e) { if (e.Button == MouseButtons.None) { this.dragging=false; } } // ==================================================================================== private void _shift(MouseEventArgs edummy) { Point e=MousePosition; if (!this.dragging) { return; } var pv=e.X; if (this.Width < this.Height) {pv=e.Y; } if (this.inversef) { pv=-pv; } var devipivothere= pv-this.pbase; this.pbase += devipivothere-this.devipivot; if (this.pbase > this._proomtot()) {this.pbase=this._proomtot(); } if (this.pbase < 0) {this.pbase=0; } if (this._proomtot() == 0) { this.bas=0; } else { this.bas=this.pbase*(this.whole-this.view)/this._proomtot(); } BaseChangedMechanical(this, this.bas); BaseChangedAnyway(this, this.bas); this._setM(); } // ==================================================================================== private void _setmechaFTG() { if (this.whole-this.view == 0) { this.pbase=0; } else { this.pbase=this.bas*this._proomtot()/(this.whole-this.view); } BaseChangedMechanical(this, bas); BaseChangedAnyway(this, bas); this._setM(); } // ==================================================================================== private void _setwhkernel() { if (this.Width > this.Height) { this.Q=this.Height; this.pwhole=this.Width-this.Q-this.Q; Button btL=this.btfrom, btR=this.btto; // temporary !!! if (this.inversef) { btR=this.btfrom; btL=this.btto; } btL.SetBounds(0, 0, Q, Q); btR.SetBounds(pwhole+Q, 0, Q, Q); btL.Text="<"; btR.Text=">"; } else { this.Q=this.Width; this.pwhole=this.Height-this.Q-this.Q; Button btT=this.btfrom, btB=this.btto; // temporary !!! if (this.inversef) { btB=this.btfrom; btT=this.btto; } btT.SetBounds(0, 0, Q, Q); btB.SetBounds(0, pwhole+Q, Q, Q); btT.Text="A"; btB.Text="V"; } this._setcode(); } // ==================================================================================== private void _setcode() { if (this.bas > this.whole-this.view) {this.bas=this.whole-this.view; } if (this.bas < 0) {this.bas=0; } this.pview = this.pwhole * this.view / this.whole; if (this.pview < 15) {this.pview=15; } if (this.pview > this.pwhole) {this.pview = this.pwhole; } if (this.whole-this.view == 0) { this.pbase=0; } else { this.pbase=this.bas*this._proomtot()/(this.whole-this.view); } BaseChangedByCode(this, this.bas); BaseChangedAnyway(this, this.bas); this._setM(); } // ==================================================================================== private void _setM() { if (this.Width > this.Height) { if (this.inversef) { btM.SetBounds(pwhole + Q - pbase - pview, 0, pview, Q); } else { btM.SetBounds(Q + pbase, 0, pview, Q); } } else { if (this.inversef) { btM.SetBounds(0, pwhole + Q - pbase - pview, Q, pview); } else { btM.SetBounds(0, Q + pbase, Q, pview); } } } // ==================================================================================== private int _proomtot() { int toret=this.pwhole-this.pview; if (toret < 0) {toret=0; } return toret; } // ==================================================================================== } // ########################################################################################