winform自定義控制元件
阿新 • • 發佈:2019-01-05
Point originPoint;
Point originsetRectPoint;
bool setRectDown = false;
void MySlider_MouseUp(object sender, MouseEventArgs e)
{
setRectDown = false;
}
void MySlider_MouseMove(object sender, MouseEventArgs e)
{
if (setRectDown)
{
int dd = e.Location.X - originPoint.X;
double percent = (double)(originsetRectPoint.X + dd - this.backRect.X) / (this.backRect.Width - this.backRect.Height);
if (percent < 0)
{
this.Value = minimum;
this.foreRect.Width = 0;
this.setRect.X = backRect.X;
}
else if (percent > 1)
{
this.Value = maximum;
this.foreRect.Width = this.backRect.Width;
this.setRect.X = backRect.X + backRect.Width - backRect.Height;
}
else
{
this.Value = percent * maximum;
this.foreRect.Width = (int)(percent * this.backRect.Width);
this.setRect.X = originsetRectPoint.X + dd;
}
Invalidate();
}
}
void MySlider_MouseDown(object sender, MouseEventArgs e)
{
if (setRect.Contains(e.Location))
{
this.originPoint = e.Location;
originsetRectPoint = this.setRect.Location;
this.setRectDown = true;
}
}