gpt4 book ai didi

c# - 窗体 : How to implement button with correct background colors

转载 作者:太空宇宙 更新时间:2023-11-03 23:40:24 24 4
gpt4 key购买 nike

我正在使用 C# 开发 winform 应用程序。我创建了 UserControl 固有的自定义按钮,如下所示:

public partial class UserButton : UserControl
{
public UserButton(string UserID)
{
this.Size = new Size(32, 50);
this.BackColor = Color.Transparent;
}

protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
Img = WaseelaMonitoring.Properties.Resources.T;
g.DrawImage(Img, 0, 0, this.Size.Width, this.Size.Height);
}
}

注意:这是按钮 png 图像 ( Click here )

现在,我想使用以下代码在图片框上显示一些按钮:

UserButton TagButton1 = new UserButton("Button1");
TagButton1.Location = Points[0];
UserButton TagButton2 = new UserButton("Button2");
TagButton2.Location = Points[1];
UserButton TagButton3 = new UserButton("Button3");
TagButton1.Location = Points[2];
Picturebox1.Controls.Add(TagButton1);
Picturebox1.Controls.Add(TagButton2);
Picturebox1.Controls.Add(TagButton2);
Picturebox1.Invalidate();

好的,当图片框上只显示一个按钮时,背景按钮是透明的(如我所愿),如下所示:

enter image description here

但是如果我想同时显示两个或更多按钮,背景按钮是白色的,而不是像这样透明的:

enter image description here

我正在使用无效图片框并尝试使按钮无效,但没有解决该问题。

最佳答案

WinForms 不支持组件的真正 Z 顺序;窗口控件(例如 ButtonUserControl)不能有真正的 alpha channel 支持,而 this.Background - Color.Transparent 技巧实际上是一种特殊情况,控件将首先为其自身重新绘制其父级的背景图像或颜色。

如果您追求更灵活的用户体验,我建议您切换到 WPF,或者在单个 WinForms Control 中完成所有绘制工作。

关于c# - 窗体 : How to implement button with correct background colors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29245214/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com