一个问题搞了 一个星期了,大家给看下
winform Groupbox 边框颜色的改变网上很上的重写方法都可以实现,但是实现了边框的颜色改变,那透明背景就不能行了
好心的人能给解决下吗?
e.Graphics.Clear(groupBox1.BackColor);
透明背景不能用了,感谢大家了 --------------------编程问答-------------------- 有个方式看你适不适用
把 GroupBox 的背景色改成 比如红色
把 Form 的 TransparentKey 属性设成红色
这样子 GroupBox 的背景就变透明了~
--------------------编程问答-------------------- 对控件使用REGION就能解决所有控件的透明问题。
好好研究下,
如果不懂,
还可以再留言给我 --------------------编程问答-------------------- region这个属性好好研究下吧 --------------------编程问答-------------------- 三楼说的我真的不懂,可以说的详细点 ?
二楼仁兄说的是可以,不过背景不是纯色的! --------------------编程问答-------------------- 补充 代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace SNfox.Foundation.ControlEx
{
[ToolboxBitmap(typeof(GroupBoxEx))]
public class GroupBoxEx : GroupBox
{
private Font _titleFont = new Font("宋体", 10, FontStyle.Regular);
private Color _titleColor = Color.Green;
private Color _borderColor = Color.FromArgb(23, 169, 254);
private int _radius = 10;
private int _tiltePos = 10;
private const int WM_ERASEBKGND = 0x0014;
private const int WM_PAINT = 0xF;
public GroupBoxEx()
: base()
{
//this.BackColor = System.Drawing.Color.Transparent;
}
[DefaultValue(typeof(Color), "23, 169, 254"), Description("控件边框颜色")]
public Color BorderColor
{
get { return _borderColor; }
set
{
_borderColor = value;
base.Invalidate();
}
}
[DefaultValue(typeof(Color), "Green"), Description("标题颜色")]
public Color TitleColor
{
get { return _titleColor; }
set
{
_titleColor = value;
base.Invalidate();
}
}
[DefaultValue(typeof(Font), ""), Description("标题字体设置")]
public Font TitleFont
{
get { return _titleFont; }
set
{
_titleFont = value;
base.Invalidate();
}
}
[DefaultValue(typeof(int), "30"), Description("圆角弧度大小")]
public int Radius
{
get { return _radius; }
set
{
_radius = value;
base.Invalidate();
}
}
[DefaultValue(typeof(int), "10"), Description("标题位置")]
public int TiltePos
{
get { return _tiltePos; }
set
{
_tiltePos = value;
base.Invalidate();
}
}
protected override void WndProc(ref Message m)
{
try
{
base.WndProc(ref m);
if (m.Msg == WM_PAINT)
{
if (this.Radius > 0)
{
using (Graphics g = Graphics.FromHwnd(this.Handle))
{
Rectangle r = new Rectangle();
r.Width = this.Width;
r.Height = this.Height;
DrawBorder(g, r, this.Radius);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void DrawBorder(Graphics g, Rectangle rect, int radius)
{
rect.Width -= 1;
rect.Height -= 1;
using (Pen pen = new Pen(this.BorderColor))
{
g.Clear(this.BackColor);
g.DrawString(this.Text, this.TitleFont, new SolidBrush(this.TitleColor), radius + this.TiltePos, 0);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
GraphicsPath path = new GraphicsPath();
float height = g.MeasureString(this.Text, this.TitleFont).Height / 2;
float width = g.MeasureString(this.Text, this.TitleFont).Width;
path.AddArc(rect.X, rect.Y + height, radius, radius, 180, 90);//左上角弧线
path.AddLine(radius, rect.Y + height, radius + this.TiltePos, rect.Y + height);
path.StartFigure();
path.AddLine(radius + this.TiltePos + width, rect.Y + height, rect.Right - radius, rect.Y + height);
path.AddArc(rect.Right - radius, rect.Y + height, radius, radius, 270, 90);//右上角弧线
path.AddArc(rect.Right - radius, rect.Bottom - radius, radius, radius, 0, 90);
path.AddArc(rect.X, rect.Bottom - radius, radius, radius, 90, 90);
path.StartFigure();
path.AddArc(rect.X, rect.Y + height, radius, radius, -90, -90);//左上角弧线
path.AddArc(rect.X, rect.Bottom - radius, radius, radius, -180, -90);
g.DrawPath(pen, path);
}
}
}
}
--------------------编程问答-------------------- 透明
backcolor="Transparent" --------------------编程问答-------------------- --------------------编程问答-------------------- g.Clear(this.BackColor);
=>
g.Clear(this.BackColor == Color.Transparent ? Parent.BackColor : BackColor);
--------------------编程问答-------------------- 帮顶一下 --------------------编程问答--------------------
public class MyGroupBox : GroupBox
{
public MyGroupBox()
{
base.BackColor = Color.Transparent;
}
[Browsable(false)]
public override Color BackColor
{
get
{
return base.BackColor;
}
set
{
base.BackColor = value;
}
}
private Color backColor = Color.Transparent;
public Color ActualBackColor
{
get { return this.backColor; }
set { this.backColor = value; }
}
protected override void OnPaint(PaintEventArgs e)
{
Size tSize = TextRenderer.MeasureText(this.Text, this.Font);
Rectangle borderRect = e.ClipRectangle;
borderRect.Y += tSize.Height / 2;
borderRect.Height -= tSize.Height / 2;
GraphicsPath gPath = CreatePath(0, borderRect.Y, (float)(this.Width - 1), borderRect.Height - 1, 5, true, true, true, true);
e.Graphics.FillPath(new SolidBrush(ActualBackColor), gPath);
e.Graphics.DrawPath(new Pen(Color.Red), gPath);
borderRect.X += 6;
borderRect.Y -= 7;
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), borderRect);
}
public GraphicsPath CreatePath(float x, float y, float width, float height,
float radius, bool RoundTopLeft, bool RoundTopRight, bool RoundBottomRight, bool RoundBottomLeft)
{
float xw = x + width;
float yh = y + height;
float xwr = xw - radius;
float yhr = yh - radius;
float xr = x + radius;
float yr = y + radius;
float r2 = radius * 2;
float xwr2 = xw - r2;
float yhr2 = yh - r2;
GraphicsPath p = new GraphicsPath();
p.StartFigure();
if (RoundTopLeft)
{
p.AddArc(x, y, r2, r2, 180, 90);
}
else
{
p.AddLine(x, yr, x, y);
p.AddLine(x, y, xr, y);
}
p.AddLine(xr, y, xwr, y);
if (RoundTopRight)
{
p.AddArc(xwr2, y, r2, r2, 270, 90);
}
else
{
p.AddLine(xwr, y, xw, y);
p.AddLine(xw, y, xw, yr);
}
p.AddLine(xw, yr, xw, yhr);
if (RoundBottomRight)
{
p.AddArc(xwr2, yhr2, r2, r2, 0, 90);
}
else
{
p.AddLine(xw, yhr, xw, yh);
p.AddLine(xw, yh, xwr, yh);
}
p.AddLine(xwr, yh, xr, yh);
if (RoundBottomLeft)
{
p.AddArc(x, yhr2, r2, r2, 90, 90);
}
else
{
p.AddLine(xr, yh, x, yh);
p.AddLine(x, yh, x, yhr);
}
p.AddLine(x, yhr, x, yr);
p.CloseFigure();
return p;
}
}
Form1中放一个Panel,设置一张背景图片,然后在Page_Load中调用
MyGroupBox a = new MyGroupBox();
a.Text = "我是文本";
a.ForeColor = Color.White;
a.Location = new Point(10, 10);
panel1.Controls.Add(a);
可以看见边框是红色,背景是透明的,即Panel的背景图片
主要就是Override OnPaint事件,而不是写他的Paint事件,没什么东西,你自己再将园边框和文字位置处理一下
--------------------编程问答-------------------- 10楼的可以实现
不过
groupBox.text 文字下面有红色的线 --------------------编程问答-------------------- 學習一下。 --------------------编程问答-------------------- 才一个星期呀,小问题吧
下面的问题那位高手帮看看吧
高手们也请多多赐教
http://topic.csdn.net/u/20110913/13/59f92d11-1fb0-4b7e-9c4a-e93d8f19c689.html
http://topic.csdn.net/u/20110611/12/3258c959-4f28-46b7-b5d6-46135d73036b.html
http://topic.csdn.net/u/20110722/14/89f7440b-c4d7-4c9a-a4bb-a503f5135db2.html
http://topic.csdn.net/u/20110729/10/a7bfaf06-0cf9-4580-8e91-d4e0b92066c6.html
http://topic.csdn.net/u/20110811/16/e56e7cc1-d8c9-40af-92e3-c24ca103d17d.html
http://topic.csdn.net/u/20110830/13/dfae4ca5-d2b9-4889-8a3c-6f7fb61936c9.html
http://topic.csdn.net/u/20110905/12/a1161adb-8e5d-491a-b302-c9722edf2dab.html
http://topic.csdn.net/u/20110913/16/2dbcc9db-8f71-40c5-901f-afae9026f7c2.html
http://topic.csdn.net/u/20110913/13/59f92d11-1fb0-4b7e-9c4a-e93d8f19c689.html --------------------编程问答-------------------- 顶一下 --------------------编程问答--------------------
这个问题很好处理,实例只是偷懒而已,直接画了个椭圆+文字,你大可按照自己的需要,画成任何样子 --------------------编程问答-------------------- 楼上真不错 --------------------编程问答-------------------- 感谢你 wknight_IT
关键不是技术不太行吗?
能给个完整的就好了! --------------------编程问答--------------------
我记得这个是可以的. --------------------编程问答-------------------- 感谢你 wknight_IT
关键不是技术不太行吗?
能给个完整的就好了! --------------------编程问答-------------------- 好心人给解决下,不然项目就进行不下去了 --------------------编程问答-------------------- 高手给解决下
补充:.NET技术 , C#