简体   繁体   English

winform滚动条自定义

[英]winform scroll bar custom

Sorry about the translator.对不起译者。 I want to customize the scroll bar with winform.我想用winform自定义滚动条。

What I want is the ability to drag the control out of the bar and return to the original value without changing the value when the mouse is raised.我想要的是能够在鼠标抬起时将控件拖出栏并返回到原始值而不更改值。

I've tried scrollbar.mouseLeave , but it's not immediate and the value has to be outside the client's range so it doesn't change.我试过scrollbar.mouseLeave ,但它不是立即生效的,而且值必须在客户端的范围之外,所以它不会改变。 It must be completed with winform.必须用winform来完成。 I appreciate your help.我感谢您的帮助。

If you scroll out of range of that scrollbar and mouse-up, you want to return to the original value.如果滚动超出该滚动条的范围并向上移动鼠标,则需要返回到原始值。 The existing value is the value before scrolling.现有值是滚动前的值。

在此处输入图像描述

The default behavior when you drag the thumb and the mouse leaves the client area of the scrollbar is resetting the Value property to it's before-dragging-the-thumb-value.当您拖动拇指并且鼠标离开滚动条的客户区时,默认行为是将Value属性重置为其拖动拇指之前的值。 You don't need to write any additional code if this is what you want.如果这是您想要的,您不需要编写任何额外的代码。

However, you can handle the Scroll event to override the default behavior to keep the last thumb position or the Minimum / Maximum values when the mouse pointer leaves the client area of the control:但是,您可以处理Scroll事件以覆盖默认行为,以在鼠标指针离开控件的工作区时保留最后一个缩略图 position 或Minimum / Maximum

private void testScrollBar_Scroll(object sender, ScrollEventArgs e)
{
    if (!testScrollBar.ClientRectangle.Contains(testScrollBar.PointToClient(MousePosition))
        && e.NewValue != e.OldValue)
    {
        e.NewValue = e.OldValue;
        testScrollBar.Invalidate();
    }
}

On the other hand, if you want to reset the value to the before-dragging-the-thumb-value only when you release the mouse button:另一方面,如果您只想在释放鼠标按钮时将值重置为拖动前的拇指值:

private void testScrollBar_Scroll(object sender, ScrollEventArgs e)
{
    if (e.Type == ScrollEventType.ThumbTrack &&
        !testScrollBar.ClientRectangle.Contains(testScrollBar.PointToClient(MousePosition)))
    {
        e.NewValue = e.OldValue;
        testScrollBar.Invalidate();
    }
}

The demo illustrates the three behaviors.该演示说明了这三种行为。

SOQ65712488

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM