to LorenLiu: 你好,我按你的代码跑了一下,确实和你说的一样,右边和下边都没画出来,请问这是怎么回事呢?我需要的效果是整个Rectangle的边框要画,可是你的代码和我的代码刚好相反,我的是多进去了一点,你的是右边和下边没画出来。
实在想不明白,我如果把整个矩形画成曲线的,这个问题就不存在的,但是如果是直角的,就右边和下边的线都不画出来,只有当按下时才显示,下面是画曲线的代码,有望高手帮忙看下为什么画曲线就不会有那个问题 private GraphicsPath RoundRect(RectangleF rt) { float x = rt.X, y = rt.Y, w = rt.Width, h = rt.Height; float r = 7; GraphicsPath rr = new GraphicsPath(); rr.AddBezier(x, y + r, x, y, x + r, y, x + r, y); rr.AddLine(x + r, y, x + w - r, y); rr.AddBezier(x + w - r, y, x + w, y, x + w, y + r, x + w, y + r); rr.AddLine(x + w, y + r, x + w, y + h - r); rr.AddBezier(x + w, y + h - r, x + w, y + h, x + w - r, y + h, x + w - r, y + h); rr.AddLine(x + w - r, y + h, x + r,y + h); rr.AddBezier(x + r, y + h, x, y + h, x, y + h - r, x, y + h - r); rr.AddLine(x, y + h - r, x, y + r); return rr; }
to LorenLiu: 是的,我用你的代码执行过之后,显示就是右边和下边的线没显示,然后按下才出来,我的代码是显示是正常的,按下就右边和下边的线多进去了一点,不过你看我7楼的代码,我用这段重绘曲线圆角的代码,然后把弧度改为0就实现了,7是有弧度的
to LorenLiu: 呵呵,我重新改成你的代码,这回又好了,不明白为什么昨天不行,昨晚我调了一晚就是出不来效果,估计是IDE当时秀逗了~~~先结贴,谢谢你的帮助~~~
当我把最前面的注释的rt.Inflate(0, 0);改为rt.Inflate(1, 1);就可以了,但是这样的按钮却失去了立体效果,只有按下按钮的时候才能看出效果来
//默认为未按
bool down = false;
protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
try
{
Graphics gfx = pevent.Graphics;
Rectangle rt = this.ClientRectangle;
//rt.Inflate(0, 0); GraphicsPath gp = new GraphicsPath();
gp.AddRectangle(new Rectangle(rt.X, rt.Y, rt.Width - 1, rt.Height - 1)); //填充按钮背景色
using (LinearGradientBrush lgb = new LinearGradientBrush(rt, Color.FromArgb(102, 102, 102), Color.FromArgb(42, 42, 42), 90f))
{
gfx.SmoothingMode = SmoothingMode.HighQuality;
gfx.CompositingQuality = CompositingQuality.HighQuality;
gfx.FillPath(lgb, gp);
} //这里开始绘制按钮按下松开效果
Rectangle rtBorder = rt;
rtBorder.Inflate(1, 1); //放大1
if (down)
{
rtBorder.Inflate(-2, -2); //缩小2
}
else
{
rtBorder.Inflate(-1, -1); //缩小1,即不变化
} //rtBorder.Inflate(1,1);
using (Pen pen = new Pen(Color.FromArgb(29, 29, 29)), penOuter = new Pen(Color.FromArgb(158, 158, 158)))
{
//画外边框
using (GraphicsPath outerBorder = new GraphicsPath())
{
outerBorder.AddRectangle(new Rectangle(rtBorder.X, rtBorder.Y, rtBorder.Width - 1, rtBorder.Height - 1));
gfx.DrawPath(pen, outerBorder);
} rtBorder.Inflate(-1, -1);
//画内边框
using (GraphicsPath innerBorder = new GraphicsPath())
{
innerBorder.AddRectangle(new Rectangle(rtBorder.X, rtBorder.Y, rtBorder.Width - 1, rtBorder.Height - 1));
gfx.DrawPath(penOuter, innerBorder);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
protected override void OnMouseDown(MouseEventArgs mevent)
{
base.OnMouseDown(mevent);
down = true;
Invalidate();
}
protected override void OnMouseUp(MouseEventArgs mevent)
{
base.OnMouseUp(mevent);
down = false;
Invalidate();
}
你好,我按你的代码跑了一下,确实和你说的一样,右边和下边都没画出来,请问这是怎么回事呢?我需要的效果是整个Rectangle的边框要画,可是你的代码和我的代码刚好相反,我的是多进去了一点,你的是右边和下边没画出来。
private GraphicsPath RoundRect(RectangleF rt)
{
float x = rt.X, y = rt.Y, w = rt.Width, h = rt.Height;
float r = 7;
GraphicsPath rr = new GraphicsPath(); rr.AddBezier(x, y + r, x, y, x + r, y, x + r, y);
rr.AddLine(x + r, y, x + w - r, y);
rr.AddBezier(x + w - r, y, x + w, y, x + w, y + r, x + w, y + r);
rr.AddLine(x + w, y + r, x + w, y + h - r);
rr.AddBezier(x + w, y + h - r, x + w, y + h, x + w - r, y + h, x + w - r, y + h);
rr.AddLine(x + w - r, y + h, x + r,y + h);
rr.AddBezier(x + r, y + h, x, y + h, x, y + h - r, x, y + h - r);
rr.AddLine(x, y + h - r, x, y + r); return rr;
}
是的,我用你的代码执行过之后,显示就是右边和下边的线没显示,然后按下才出来,我的代码是显示是正常的,按下就右边和下边的线多进去了一点,不过你看我7楼的代码,我用这段重绘曲线圆角的代码,然后把弧度改为0就实现了,7是有弧度的
呵呵,我重新改成你的代码,这回又好了,不明白为什么昨天不行,昨晚我调了一晚就是出不来效果,估计是IDE当时秀逗了~~~先结贴,谢谢你的帮助~~~