c# 关于panel的透明背景问题,在线等,高手进!谢谢!

dboy_2010 2010-05-20 11:57:26
我一个窗体Form里有个panel,
panel上面有许多动态添加的label,label透明,
我要显示Form的背景,所以设panel的背景色为透明。

可是滚动Panel的滚动条时,Panel里的窗体背景就变花了!怎么解决?
...全文
441 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
afeng124 2010-05-20
  • 打赏
  • 举报
回复
public partial class TransparentPanel : Panel
{
#region Local Variables
private System.Drawing.Color _backColor = Color.Black;
private System.Drawing.Color _borderColor = Color.White;
private int _radius = 8;
private int _opacity = 125;
private System.Drawing.Color _dimmedColor = Color.LightGray;
Rectangle _IconRect = new Rectangle(0, 7, 24, 24);
protected Rectangle iRect = new Rectangle();
#endregion
#region Properties
[DefaultValue(typeof(Color), "Black")]
public new Color BackColor
// Gets or sets the background color of the control.
{
get { return _backColor; }
set { _backColor = value; Invalidate(); }
}

[DefaultValue(typeof(Color), "White"), Category("Appearance"), Description("The border color of the control.")]
/// Gets or sets the outer border color of the control.
public Color BorderColor
{
get { return _borderColor; }
set { _borderColor = value; Invalidate(); }
}

[
Bindable(true),
Category("Appearance"),
DefaultValue(125),
Description("The alpha value used to blend the control's background. Valid values are 0 through 255.")
]
public int Opacity
{
get { return _opacity; }
set { _opacity = value; this.Invalidate(); }
}


[
Bindable(true),
Category("Layout"),
DefaultValue(20),
Description("Radius of rounded borders")
]
public int Radius
{
get { return _radius; }
set { _radius = value; this.Invalidate(); }
}

public override Font Font
{
get { return base.Font; }
set { base.Font = value; this.Invalidate(); }
}

public override Color ForeColor
{
get { return base.ForeColor; }
set { base.ForeColor = value; this.Invalidate(); }
}

#endregion
public TransparentPanel()
{
InitializeComponent();
base.BackColor = Color.Transparent;
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint, true);
SetStyle(ControlStyles.Opaque, false);
Margin = new Padding(1, 1, 1, 1);
Font = new Font("宋体", 10F);
UpdateStyles();
}

#region Methods
protected override void OnPaint(PaintEventArgs e)
{
SmoothingMode sm = e.Graphics.SmoothingMode;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
DrawBorder(e.Graphics);
DrawBackground(e.Graphics);
e.Graphics.SmoothingMode = sm;
}

protected void DrawBorder(Graphics g)
{
Rectangle rect = this.ClientRectangle;
rect.Width--;
rect.Height--;
using (GraphicsPath bp = GetPath(rect, _radius))
{
using (Pen p = new Pen(_borderColor))
{
g.DrawPath(p, bp);
}
}
}

protected void DrawBackground(Graphics g)
{
Rectangle rect = this.ClientRectangle;
iRect = rect;
rect.X++;
rect.Y++;
rect.Width -= 2;
rect.Height -= 2;
using (GraphicsPath bb = GetPath(rect, _radius))
{
using (Brush br = new SolidBrush(Color.FromArgb(_opacity, _backColor)))
{
g.FillPath(br, bb);
}
}
}

protected GraphicsPath GetPath(Rectangle rc, int r)
// Build the path with the round corners in the rectangle
// r is the radious of rounded corner.
{
int x = rc.X, y = rc.Y, w = rc.Width, h = rc.Height;
r = r << 1;
GraphicsPath path = new GraphicsPath();
if (r > 0)
// If the radious of rounded corner is greater than one side then
// do the side rounded
{
if (r > h) { r = h; }; //Rounded
if (r > w) { r = w; }; //Rounded
path.AddArc(x, y, r, r, 180, 90); //Upper left corner
path.AddArc(x + w - r, y, r, r, 270, 90); //Upper right corner
path.AddArc(x + w - r, y + h - r, r, r, 0, 90); //Lower right corner
path.AddArc(x, y + h - r, r, r, 90, 90); //Lower left corner
path.CloseFigure();
}
else
// If the radious of rounded corner is zero then the path is a rectangle
{
path.AddRectangle(rc);
}
return path;
}

#endregion
}
dboy_2010 2010-05-20
  • 打赏
  • 举报
回复
/// <summary>
/// 创建Lable
/// </summary>
/// <param name="dt"></param>
/// <param name="panel1"></param>
/// <param name="eh"></param>
public void CreateLable(DataTable dt, Panel panel1, EventHandler eh,int n)
{
int w = Convert.ToInt32(width)-200*4;

int i = 1;
int x =(int)w/5;
int y = 10;
if (dt != null && dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
string text = "",text1="";
string tag = "";
if (dt.Columns.Count > 2)
{
if (!(row["code"] is DBNull))
{
tag = row["code"].ToString();
}
}


if (!(row["brief"] is DBNull) && !(row["brief"].ToString().Equals("无")) && !(row["brief"].ToString().Equals("")))
{
text = row["brief"].ToString();
}
//else
//{
text1 = row["name"].ToString();
// }
Label b = new Label();

b.Text = text;
b.Tag = tag;

b.Height = 100;
b.Width = 200;
b.Click += eh;
b.TextAlign = ContentAlignment.MiddleCenter;

b.BackColor = Color.Transparent;
b.ForeColor = Color.White;


if (i % 4 == 1)
{
x = (int)w /5;
y = y + b.Height + 100;
if (i == 1)
{
y = 10;
}
}
else
{

x = x + (int)w / 5 + b.Width;

}


b.Location = new Point(x, y);

if (n == 1)
{
panel1.Controls.Add(b);
}
else
{
panel1.Controls.Add(b);

}
i++;
}
}
}
这是动态创建的label方法
maguowei19890708 2010-05-20
  • 打赏
  • 举报
回复
你的label透明是怎么实现的?贴下代码吧!交个朋友QQ:1018964115
水哥阿乐 2010-05-20
  • 打赏
  • 举报
回复
看看代码
yalan 2010-05-20
  • 打赏
  • 举报
回复
我知道需要用GDI重新绘图,可我刚学到这里,具体怎么操作不清楚,LZ可以看一下GDI部分
dboy_2010 2010-05-20
  • 打赏
  • 举报
回复
5楼,用了你的方法没用啊!
g394594141 2010-05-20
  • 打赏
  • 举报
回复
估计是你DrawBackground的问题,具体我也不清楚,没写过这样的代码

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧