如何将 winforms 不透明度绑定到 TrackBar(滑块)

发布于 2024-08-27 08:05:45 字数 636 浏览 10 评论 0原文

我有一个带有 BindingSource 的 winform,它的 DataSource 中有一个名为 Opacity 的 int 属性。我在 winform 上还有一个 TrackBar ,我想用它来控制 winform 的 Opacity

我已将 TrackBar 上的 Value 属性绑定到 Opacity,并且可以很好地滑动 TrackBar会将变量从 TrackBar.Minimum (0) 更改为 TrackBar.Maximum (1)。

不过,我还将 winform 的 Opacity 属性绑定到该值,因为 TrackBar 的值仅在 +/-1 范围内从最小值到最大值,而不是+/- .1 左右(就像 Opacity 那样),它不能正确淡入 winform。相反,0 将使其不透明,1 将使其完全可见。

我需要一种在上述架构中工作的方法,但让 TrackBar 以小于 1 的定义增量将其值从 0 更改为 1。

I've got a winform with a BindingSource that has an int property named Opacity in its DataSource. I also have a TrackBar on the winform that I want to use to control the Opacity of the winform.

I've bound the Value property on the TrackBar to the Opacity and that functions just fine, sliding the TrackBar will change the variable from TrackBar.Minimum (0) to TrackBar.Maximum (1).

I've also bound the Opacity property of the winform to this value, however, since the TrackBar's values only go from Minimum to Maximum in +/-1 rather than +/- .1 or so (like Opacity does), it doesn't properly fade the winform. Instead, 0 will turn it opaque and 1 will turn it fully visible.

I need a way to work within the architecture described above, but get the TrackBar to change its value from 0 to 1 in defined increments smaller than 1.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃→可爱长大的 2024-09-03 08:05:45

我认为最简洁的方法是创建一个直接继承自 TrackBarUserControl,它隐藏 ValueSmallChangeMinimumMaximumTickFrequency 属性以及同名的 double 属性:

public partial class DoubleTrackBar : TrackBar
{
    public DoubleTrackBar()
    {
        InitializeComponent();
    }

    private int _multiplier = 100;

    [Browsable(true)]
    public new double TickFrequency
    {
        get
        {
            return (double)base.TickFrequency / _multiplier;
        }
        set
        {
            base.TickFrequency = (int)(value * _multiplier);
        }
    }

    [Browsable(true)]
    public new double Minimum
    {
        get
        {
            return (double)base.Minimum / _multiplier;
        }
        set
        {
            base.Minimum = (int)(value * _multiplier);
        }
    }

    [Browsable(true)]
    public new double Maximum
    {
        get
        {
            return (double)base.Maximum / _multiplier;
        }
        set
        {
            base.Maximum = (int)(value * _multiplier);
        }
    }

    [Browsable(true)]
    public new double Value
    {
        get
        {
            return (double)base.Value / _multiplier;
        }
        set
        {
            base.Value = (int)(value * _multiplier);
        }
    }

    [Browsable(true)]
    public new double SmallChange
    {
        get
        {
            return (double)base.SmallChange / _multiplier;
        }
        set
        {
            base.SmallChange = (int)(value * _multiplier);
        }
    }
}

I think the cleanest way to do this would be to create a UserControl that inherits directly from TrackBar, that hides the Value, SmallChange, Minimum, Maximum and TickFrequency properties with same-named double properties:

public partial class DoubleTrackBar : TrackBar
{
    public DoubleTrackBar()
    {
        InitializeComponent();
    }

    private int _multiplier = 100;

    [Browsable(true)]
    public new double TickFrequency
    {
        get
        {
            return (double)base.TickFrequency / _multiplier;
        }
        set
        {
            base.TickFrequency = (int)(value * _multiplier);
        }
    }

    [Browsable(true)]
    public new double Minimum
    {
        get
        {
            return (double)base.Minimum / _multiplier;
        }
        set
        {
            base.Minimum = (int)(value * _multiplier);
        }
    }

    [Browsable(true)]
    public new double Maximum
    {
        get
        {
            return (double)base.Maximum / _multiplier;
        }
        set
        {
            base.Maximum = (int)(value * _multiplier);
        }
    }

    [Browsable(true)]
    public new double Value
    {
        get
        {
            return (double)base.Value / _multiplier;
        }
        set
        {
            base.Value = (int)(value * _multiplier);
        }
    }

    [Browsable(true)]
    public new double SmallChange
    {
        get
        {
            return (double)base.SmallChange / _multiplier;
        }
        set
        {
            base.SmallChange = (int)(value * _multiplier);
        }
    }
}
坏尐絯℡ 2024-09-03 08:05:45

如果您进行数据绑定,那么您有一个数据类,该数据类具有一个 double OpacityForSlider (或类似的东西),您已绑定了 TrackBarValue代码> 到.

将滑块最小值和最大值设置为 0-100 并将其添加到您的数据类中

public double OpacityForForm
{
    get { return this.OpacityForSlider / 100; } 
}

现在将您的 winforms Opacity 绑定到它,而不是 OpacityForSlider

If you're databinding then you have a data class that has a double OpacityForSlider (or something similar) that you've bound the Value of the TrackBar to.

Set the sliders min and max to 0-100 and add this to your data class

public double OpacityForForm
{
    get { return this.OpacityForSlider / 100; } 
}

Now bind your winforms Opacity to that, rather than OpacityForSlider

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文