C# Windows窗体按钮背景透明
我有一个带有C# Windows窗体按钮背景透明,c#,wpf,winforms,C#,Wpf,Winforms,我有一个带有ContentControl的WPF应用程序,其中的内容是WindowsFormsHost,它有一个Child自定义面板,可以呈现SDL流。现在我添加了禁用/启用流音频的按钮。一切正常,但我无法使按钮图标透明。我该怎么做?有可能吗 图像(图标)也是透明的。解决方法可以是创建自定义WinForms按钮,重写OnPaint事件,并通过调用 AudioButton = new System.Windows.Forms.Button() { Enabled = AudioButton
ContentControl
的WPF应用程序,其中的内容是WindowsFormsHost
,它有一个Child
自定义面板,可以呈现SDL
流。现在我添加了禁用/启用流音频的按钮。一切正常,但我无法使按钮图标透明。我该怎么做?有可能吗
图像(图标)也是透明的。解决方法可以是创建自定义WinForms按钮,重写
OnPaint
事件,并通过调用
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);
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);
}
}