首页 新闻 会员 周边 捐助

在winform中,给panel设置背景图片之后,将label的背景色设置为透明,会导致软件巨卡

0
[待解决问题]

如题,如果使用picturebox的话,在其之上的label控件的透明设置反而不会生效

用panel的话会起到作用,软件和界面会变得巨卡

已经不知道怎么做了T^T

CyanAutumn的主页 CyanAutumn | 菜鸟二级 | 园豆:202
提问于:2024-08-06 10:38
< >
分享
所有回答(3)
0

界面上的内容比较多吧,我也遇到过,但是我界面上内容比较少。但是似乎是有办法明显减少卡顿的办法的,我晚上到我电脑上找找再回复你

echo_lovely | 园豆:1440 (小虾三级) | 2024-08-06 13:36

		public Main()
		{
			InitializeComponent();
			EnableDoubleBuffering(this.panelFile); // panelFile 为有背景图片的控件
		}

		private void EnableDoubleBuffering(Control control)
		{
			typeof(Control).InvokeMember("DoubleBuffered",
				System.Reflection.BindingFlags.SetProperty |
				System.Reflection.BindingFlags.Instance |
				System.Reflection.BindingFlags.NonPublic,
				null, control, new object[] { true });
		}
支持(0) 反对(0) echo_lovely | 园豆:1440 (小虾三级) | 2024-08-06 19:56
0

那是因为picturebox不是容器控件.你把picturebox移开看看,label位置应该不随picturebox改变,它仍属于Form,透明是针对它所在的容器(Form)透明.
panel是容器控件,所以label可以对它透明.
如果label要对picturebox透明,你必须手写代码把label添加到picturebox的子控件集合中
Label label = new Label();
label.Text = "背景透明";
label.Location = new Point(10, 10);
label.BackColor = Color.Transparent;
pictureBox1.Controls.Add(label);

泡沫游走 | 园豆:273 (菜鸟二级) | 2024-08-06 19:53
0

那就不使用WINFORM提供的picturebox控件,而是直接重写OnPaint方法:
public override void OnPaint(PaintEventArgs e)
{

e.Graphics.DrawImage(this.GetBitmap(), this.ClientRectangle);

}

    Bitmap GetBitmap(float Value=50)
    {
        var source = new Bitmap(this.Image);
        if (Value < 1 || Value > 100)
            return source;
        var effect = new Bitmap(this.Image.Width, this.Image.Height);
        var _effect = Graphics.FromImage(effect);
        float[][] matrixItems ={new float[]{1,0,0,0,0},
                              new float [] {0,1,0,0,0},
                              new float []{0,0,1,0,0},
                              new float []{0,0,0,0,0},
                              new float[]{0,0,0, Value / 255f,1}};
        var imgMatrix = new ColorMatrix(matrixItems);
        var imgEffect = new ImageAttributes();
        imgEffect.SetColorMatrix(imgMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
        _effect.DrawImage(this.Image, new Rectangle(0, 0, this.Image.Width, this.Image.Height), 0, 0, this.Image.Width, this.Image.Height, GraphicsUnit.Pixel, imgEffect);
        return effect;
    }
Cai,Jimi | 园豆:406 (菜鸟二级) | 2024-08-07 11:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册