【问题标题】:Windows forms button background transparentWindows窗体按钮背景透明
【发布时间】:2021-05-20 09:23:26
【问题描述】:

我有带有 ContentControl 的 WPF 应用程序,其中内容是 WindowsFormsHost 和带有自定义面板的 Child,它呈现 SDL 流。现在我添加了按钮来禁用/启用流的音频。一切正常,但我无法使按钮图标透明。我怎样才能做到这一点?有可能吗?

AudioButton = new System.Windows.Forms.Button()
{
   Enabled = AudioButtonEnabled,
   BackColor = Color.Transparent,
   Image = Image.FromFile(@".\Images\audioDisabled.png"),
   Width = 30,
   Height = 30,
   FlatStyle = System.Windows.Forms.FlatStyle.Flat
};
AudioButton.FlatAppearance.BorderSize = 0;
AudioButton.Click += (object sender, EventArgs e) =>
{
   
};
SDLRenderer.AddButton(AudioButton);

图像(图标)也是透明的。

【问题讨论】:

    标签: c# wpf winforms


    【解决方案1】:

    解决方法可以是创建自定义 WinForms 按钮,覆盖 OnPaint 事件并通过调用 Bitmap.MakeTransparent() 使指定颜色对位图透明

    public class CustomButton : Button
    {
        private Color TransparentColor;
    
        public CustomButton() : base()
        {
            TransparentColor = Color.FromArgb(192, 192, 192);
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            if (this.Image != null)
            {
                Bitmap bmp = ((Bitmap)this.Image);
                bmp.MakeTransparent(TransparentColor);
                int x = (this.Width - bmp.Width) / 2;
                int y = (this.Height - bmp.Height) / 2;
                e.Graphics.DrawImage(bmp, x, y);
            }
            base.OnPaint(e);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-05
      • 2011-05-06
      • 1970-01-01
      • 1970-01-01
      • 2012-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多