[英]Changing opacity in form with trackbar in C# don't works, why?
I have (for fun) make a form in C#
, with a trackbar
.我(为了好玩)用C#
制作了一个带有trackbar
的表单。 I want to change the Opacity
of the form with it, so I wrote this:我想用它改变表单的Opacity
,所以我写了这个:
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.当我启动程序时,如果trackbar
的值在 1 到 100 之间,不透明度将为 100%,如果我将trackbar
拖动到 0,表单将变得完全透明。
can you only get 100% Opacity
or 0% Opacity
when a form is started, or is what i want also possible?当表单启动时,您只能获得 100% Opacity
或 0% Opacity
,还是我想要的也可能?
The value of System.Windows.Forms.Form.Opacity
is between 0.0
and 1.0
, to get the percentage of the opacity you can multiply it with 100
, so 1
means fully opaque
and 0
means fully transparent
. System.Windows.Forms.Form.Opacity
的值介于0.0
和1.0
之间,要获得不透明度的百分比,您可以将其乘以100
,因此1
表示fully opaque
, 0
表示fully transparent
。
For the trackbar, you should convert its Value
to the corresponding value between 0.0
and 1.0
, so you should do something like this:对于轨迹栏,您应该将其Value
转换为0.0
和1.0
之间的相应值,因此您应该执行以下操作:
yourForm.Opacity = (double)trackBar1.Value/trackBar1.Maximum;
将数字除以 100。它应该是介于 0 和 1 之间的双精度数
((double)trackBar1.Value) / 100
Use this:用这个:
System.Windows.Forms.Form.ActiveForm.Opacity = ((double)(trackBar1.Value) /100.0)
You can have different degrees of opacity.你可以有不同程度的不透明度。 For example 0.5 will give you 50% opacity.例如 0.5 会给你 50% 的不透明度。
private void trackBar1_Scroll(object sender, EventArgs e) { label1.Text =trackBar1.Value.ToString()+ "%"; private void trackBar1_Scroll(object sender, EventArgs e) { label1.Text =trackBar1.Value.ToString()+ "%";
Opacity=trackBar1.Value / 100.00;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.