WInForm修改button形状为带圆角时,只有左上角是圆角

LUCKTT2322 2019-07-24 03:44:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D ;

namespace WindowsFormsApplication6.button._03
{

public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
protected override void OnPaint(PaintEventArgs pe)
{
GraphicsPath path = new GraphicsPath();
Rectangle rect = new Rectangle(0, 22, this.Width, this.Height-22);
path = GetRoundedRectPath(rect, 20);
Region reg = new Region(path);
this.button1.Region = reg;
base.OnPaint(pe);
}
private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
{
int diameter = radius;
Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
GraphicsPath path = new GraphicsPath();
// 左上角
path.AddArc(arcRect, 180, 90);
// 右上角
arcRect.X = rect.Right - diameter;
path.AddArc(arcRect, 270, 90);
// 右下角
arcRect.Y = rect.Bottom - 0;
path.AddArc(arcRect, 0, 90);
// 左下角
arcRect.X = rect.Left;
path.AddArc(arcRect, 90, 90);
path.CloseFigure();
return path;
}
private void SetWindowRegion(object sender, EventArgs e)
{
}
private void OnPaint(object sender, EventArgs e)
{
}
}

}
...全文
1581 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Bridge_go 2019-07-25
  • 打赏
  • 举报
回复
搞个图片完事,自己绘多麻烦
assky124 2019-07-25
  • 打赏
  • 举报
回复
自绘一个Button不是省事很多
LUCKTT2322 2019-07-25
  • 打赏
  • 举报
回复
改变时 四个角又有颜色了呀
LUCKTT2322 2019-07-25
  • 打赏
  • 举报
回复
这样的button怎么设置点击后背景颜色
desperaso 2019-07-25
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Gid_1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Draw(Rectangle rectangle, Graphics g, int _radius, bool cusp, Color begin_color, Color end_color)
{
int span = 2;
//抗锯齿
g.SmoothingMode = SmoothingMode.AntiAlias;
//渐变填充
LinearGradientBrush myLinearGradientBrush = new LinearGradientBrush(rectangle, begin_color, end_color, LinearGradientMode.Vertical);
//画尖角
if (cusp)
{
span = 10;
PointF p1 = new PointF(rectangle.Width - 12, rectangle.Y + 10);
PointF p2 = new PointF(rectangle.Width - 12, rectangle.Y + 30);
PointF p3 = new PointF(rectangle.Width, rectangle.Y + 20);
PointF[] ptsArray = { p1, p2, p3 };
g.FillPolygon(myLinearGradientBrush, ptsArray);
}
//填充
g.FillPath(myLinearGradientBrush, DrawRoundRect(rectangle.X, rectangle.Y, rectangle.Width - span, rectangle.Height-1, _radius));
}
public static GraphicsPath DrawRoundRect(int x, int y, int width, int height, int radius)
{
//四边圆角
GraphicsPath gp = new GraphicsPath();
gp.AddArc(x, y, radius, radius, 180, 90);
gp.AddArc(width - radius, y, radius, radius, 270, 90);
gp.AddArc(width - radius, height - radius, radius, radius, 0, 90);
gp.AddArc(x, height - radius, radius, radius, 90, 90);
gp.CloseAllFigures();
return gp;
}

private void panel1_Paint(object sender, PaintEventArgs e)
{
Draw(e.ClipRectangle, e.Graphics, 18,true, Color.FromArgb(90, 143, 0), Color.FromArgb(41, 67, 0));
base.OnPaint(e);
Graphics g = e.Graphics;
g.DrawString("其实我是个Panel", new Font("微软雅黑", 9, FontStyle.Regular), new SolidBrush(Color.White), new PointF(10, 10));
}

private void panel2_Paint(object sender, PaintEventArgs e)
{
Draw(e.ClipRectangle, e.Graphics, 18, false, Color.FromArgb(113, 113, 113), Color.FromArgb(0, 0, 0));
base.OnPaint(e);
Graphics g = e.Graphics;
g.DrawString("其实我是个Panel", new Font("微软雅黑", 9, FontStyle.Regular), new SolidBrush(Color.White), new PointF(10, 10));
}

private void button1_Paint(object sender, PaintEventArgs e)
{
Draw(e.ClipRectangle, e.Graphics, 18, false, Color.FromArgb(0, 122, 204), Color.FromArgb(8, 39, 57));
base.OnPaint(e);
Graphics g = e.Graphics;
g.DrawString("其实我是个按钮", new Font("微软雅黑", 9, FontStyle.Regular), new SolidBrush(Color.White), new PointF(10, 10));
}

private void label1_Paint(object sender, PaintEventArgs e)
{
Draw(e.ClipRectangle, e.Graphics, 18, false, Color.FromArgb(210, 210, 210), Color.FromArgb(242, 242, 242));
base.OnPaint(e);

Graphics g = e.Graphics;
g.DrawString("其实我是Label", new Font("微软雅黑", 9, FontStyle.Regular), new SolidBrush(Color.Black), new PointF(10, 10));
}
}
}


圆角按钮要设置

否则4个角还有颜色。
上帝的亲哥哥 2019-07-25
  • 打赏
  • 举报
回复
引用 8 楼 Bridge_go 的回复:
搞个图片完事,自己绘多麻烦


确实,这样一堆代码,还容易出错,要么你直接用WPF写一个ControlTemplate,直接搞定,做WinForm的,GDI+需要掌握其中的技巧,这项目需要改变外观的,太适合用WPF去做了WinForm毕竟,不适合做过多的外观修改!!!WPF是做C/S的WinForm下的一个完整补充
MichaelGLX 2019-07-24
  • 打赏
  • 举报
回复

引用 2 楼 MichaelGLX 的回复:
每次画圆角的时候都用Rectangle arcRect么? 位置不改变么?

看错了。这个怎么删除呀!!

是工作区域设置不对,你画的时候 rect长宽-1试试?还有 设置工作区域为线条 这样怎么点击呀。你可以填充起来!
MichaelGLX 2019-07-24
  • 打赏
  • 举报
回复
每次画圆角的时候都用Rectangle arcRect么? 位置不改变么?
MichaelGLX 2019-07-24
  • 打赏
  • 举报
回复
很明显设置 this.button1.Region 这个不对。

110,534

社区成员

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

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

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