winform滚动条美化-Panel
查阅网上关于滚动条的美化资料,多数不符合我的要求;打算动手自己撸一个;最终实现效果如下图:
实现方式为隐藏系统滚动条,设置控件工作区域,把自定义滚动条放到对应的位置实现滚动操作;后面会一步步的实现该功能;
参考资料包含不限于:
C#程序员开发WinForm必须知道的 Window 消息大全-阿里云开发者社区
首先是自定义滚动条的实现
1.创建滚动条控件继承Control
2.添加一些必要的属性:滚动条最大值、背景色、滑块颜色等等
3.绘制自己想要的样式并添加行为事件
绘制滑块圆角代码
-
private void DrawSlider(Graphics g)
-
{
-
GraphicsPath path;
-
if (rectSlider.Width == 0 || rectSlider.Height == 0)
-
{
-
return;
-
}
-
if (rectSlider.Width > rectSlider.Height)
-
{
-
//水平
-
path = DrawHelper.GetHorizontal(this.rectSlider);
-
}
-
else
-
{
-
//垂直
-
path = DrawHelper.GetVertical(this.rectSlider);
-
}
-
-
using (SolidBrush sb = new SolidBrush(SliderColor))
-
{
-
g.FillPath(sb, path);
-
}
-
path.Dispose();
-
-
}
重新鼠标按下(OnMouseDown)抬起(OnMouseUp)和移动(OnMouseMove)的事件来实现我们需要完成的操作,需要记录鼠标按下的点处理移动距离;触发事件的时机可自行调整;
展示代码为demo示例,存在很大优化空间
-
public class VSliderControl : Control
-
{
-
protected override CreateParams CreateParams
-
{
-
get
-
{
-
CreateParams cp = base.CreateParams;
-
cp.ExStyle &= ~0x02000000;
-
return cp;
-
}
-
}
-
-
private int yLocation;
-
private bool isMouseDown = false;
-
private float movePix = 0;
-
//最大值
-
private double maxPos = 100;
-
//当前位置
-
private int pos = 1;
-
public int Pos
-
{
-
get { return pos; }
-
set
-
{
-
if (value > maxPos||value<0)
-
{ return; }
-
pos = value;
-
SetMovePix();
-
this.Invalidate();
-
}
-
}
-
//平均1 Pos多少像素
-
private double avePos = 1;
-
-
public new event EventHandler Scroll = null;
-
-
//滑块大小
-
private RectangleF rectSlider;
-
-
private Color sliderColor;
-
public Color SliderColor { get => sliderColor; set => sliderColor = value; }
-
-
-
-
public double MaxPos
-
{
-
get { return maxPos; }
-
set
-
{
-
if (maxPos != value)
-
{
-
maxPos = value;
-
this.AutoCalcSize();
-
}
-
}
-
}
-
-
-
public VSliderControl()
-
{
-
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
-
this.SetStyle(ControlStyles.DoubleBuffer, true);
-
this.SetStyle(ControlStyles.ResizeRedraw, true);
-
this.SetStyle(ControlStyles.Selectable, true);
-
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
-
this.SetStyle(ControlStyles.UserPaint, true);
-
}
-
protected override void OnPaint(PaintEventArgs e)
-
{
-
-
base.OnPaint(e);
-
-
Graphics g = e.Graphics;
-
g.SetGDIHigh();
-
-
DrawSlider(g);
-
-
-
}
-
protected override void OnMouseDown(MouseEventArgs e)
-
{
-
base.OnMouseDown(e);
-
if (rectSlider.Contains(e.Location))
-
{
-
isMouseDown = true;
-
yLocation = e.Y;
-
}
-
}
-
protected override void OnMouseMove(MouseEventArgs e)
-
{
-
base.OnMouseMove(e);
-
if (isMouseDown)
-
{
-
int mY = (e.Y - yLocation);
-
if (mY rectSlider.Height rectSlider.Y > this.Height)
-
{
-
movePix = this.Height - rectSlider.Height - rectSlider.Y;
-
-
}
-
else if (mY rectSlider.Y < -1)
-
{
-
movePix = - rectSlider.Y;
-
}
-
else
-
if (mY != 0)
-
{
-
yLocation = e.Y;
-
movePix = mY;
-
}
-
PixToPos();
-
AutoCalcSize();
-
Scroll?.Invoke(this, new EventArgs());
-
}
-
}
-
protected override void OnMouseUp(MouseEventArgs e)
-
{
-
base.OnMouseUp(e);
-
isMouseDown = false;
-
-
}
-
-
protected override void OnClientSizeChanged(EventArgs e)
-
{
-
base.OnClientSizeChanged(e);
-
AutoCalcSize();
-
}
-
private void AutoCalcSize()
-
{
-
double sliderL = this.Height / maxPos * this.Height;
-
double pix = (this.Height ) / (maxPos - this.Height);
-
if (double.IsNaN( sliderL )|| sliderL == 0)
-
return;
-
-
-
float y = movePix rectSlider.Y;
-
int h = Convert.ToInt32(sliderL);
-
rectSlider.X = -0.5f;
-
rectSlider.Y = y;
-
rectSlider.Width = this.Width ;
-
rectSlider.Height = h;
-
avePos = pix;
-
this.Invalidate();
-
movePix = 0;
-
}
-
-
private void SetMovePix()
-
{
-
double sliderL = this.Height / maxPos * this.Height;
-
double pix = (this.Height- sliderL) *pos/ (maxPos - this.Height);
-
rectSlider.Y = float.Parse( pix.ToString("0.00"));
-
movePix = 0;
-
}
-
private void PixToPos()
-
{
-
double sliderL = this.Height / maxPos * this.Height;
-
double pix = (maxPos - this.Height)/(this.Height - sliderL) *rectSlider.Y;
-
pos = Convert.ToInt32(Math.Ceiling(pix));
-
}
-
private void DrawSlider(Graphics g)
-
{
-
GraphicsPath path;
-
if (rectSlider.Width == 0 || rectSlider.Height == 0)
-
{
-
return;
-
}
-
if (rectSlider.Width > rectSlider.Height)
-
{
-
//水平
-
path = DrawHelper.GetHorizontal(this.rectSlider);
-
}
-
else
-
{
-
//垂直
-
path = DrawHelper.GetVertical(this.rectSlider);
-
}
-
-
using (SolidBrush sb = new SolidBrush(SliderColor))
-
{
-
g.FillPath(sb, path);
-
}
-
path.Dispose();
-
-
}
-
-
}
未完成待续
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhigjgeg
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
excel下划线不显示怎么办
PHP中文网 06-23 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel图片置于文字下方的方法
PHP中文网 06-27 -
微信运动停用后别人还能看到步数吗
PHP中文网 07-22