1

I have (for fun) make a form in C#, with a trackbar. I want to change the Opacity of the form with it, so I wrote this:

private void trackBar1_Scroll(object sender, EventArgs e)
{
    progressBar1.Value = trackBar1.Value;
    System.Windows.Forms.Form.ActiveForm.Opacity = trackBar1.Value;
    label2.Text = trackBar1.Value.ToString();
}

When I start the program, the opacity will be 100% if the trackbar is in value 1 to 100, and if i drag the trackbar to 0, the form becomes fully transparant.

can you only get 100% Opacity or 0% Opacity when a form is started, or is what i want also possible?

4

4 回答 4

4

的值System.Windows.Forms.Form.Opacity介于0.0和之间1.0,要获得不透明度的百分比,您可以将其乘以100,所以1意味着fully opaque0意味着fully transparent

对于 trackbar,您应该将其转换为和Value之间的相应值,因此您应该执行以下操作:0.01.0

yourForm.Opacity = (double)trackBar1.Value/trackBar1.Maximum;
于 2013-08-20T19:17:46.717 回答
3

将数字除以 100。它应该是 0 和 1 之间的双精度数

((double)trackBar1.Value) / 100
于 2013-08-20T19:10:06.483 回答
3

用这个:

System.Windows.Forms.Form.ActiveForm.Opacity = ((double)(trackBar1.Value) /100.0)

您可以有不同程度的不透明度。例如 0.5 会给你 50% 的不透明度。

于 2013-08-20T19:11:16.237 回答
-1

private void trackBar1_Scroll(object sender, EventArgs e) { label1.Text =trackBar1.Value.ToString()+ "%";

        Opacity=trackBar1.Value / 100.00;
        
    }
于 2021-05-20T12:42:05.093 回答