|
■No75824 (魔界の仮面弁士) に追記
>>角を丸くする方法を教えてください。
サンプル:
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
TrackBar tb;
PictureBox pb;
public Form1()
{
InitializeComponent();
this.tb = new TrackBar();
this.tb.Dock = DockStyle.Top;
this.tb.Minimum = 0;
this.tb.Maximum = 100;
this.tb.Value = 20;
this.pb = new PictureBox();
this.pb.Dock = DockStyle.Fill;
this.pb.BorderStyle = BorderStyle.Fixed3D;
this.pb.Padding = new Padding(8);
this.Controls.Add(this.pb);
this.Controls.Add(this.tb);
this.pb.Paint += this.pb_Paint;
this.tb.ValueChanged += delegate { this.pb.Invalidate(); };
this.Resize += delegate { this.pb.Invalidate(); };
this.MinimumSize = new Size(100, 200);
}
void pb_Paint(object sender, PaintEventArgs e)
{
Rectangle rc = new Rectangle(
this.pb.Padding.Left,
this.pb.Padding.Top,
this.pb.ClientSize.Width - this.pb.Padding.Horizontal,
this.pb.ClientSize.Height - this.pb.Padding.Vertical);
Size sz = new Size(this.tb.Value, this.tb.Value);
using(Pen pen = new Pen(Color.Black, 1))
{
FillRoundedRectangle(e.Graphics, pen, rc, sz);
}
}
void FillRoundedRectangle(Graphics g, Pen pen, Rectangle rect, Size size)
{
SmoothingMode sm = g.SmoothingMode;
PixelOffsetMode pom = g.PixelOffsetMode;
try
{
// 任意のレンダリング品質
g.SmoothingMode = SmoothingMode.None;
g.PixelOffsetMode = PixelOffsetMode.None;
using (GraphicsPath gp = new GraphicsPath())
{
gp.FillMode = FillMode.Winding;
if (size.Height > 0 && size.Width > 0)
{
// 角丸四角形
gp.AddArc(rect.Right - size.Width, rect.Top, size.Width, size.Height, 270, 90);
gp.AddArc(rect.Right - size.Width, rect.Bottom - size.Height, size.Width, size.Height, 0, 90);
gp.AddArc(rect.Left, rect.Bottom - size.Height, size.Width, size.Height, 90, 90);
gp.AddArc(rect.Left, rect.Top, size.Width, size.Height, 180, 90);
gp.AddArc(rect.Right - size.Width, rect.Top, size.Width, size.Height, 270, 90);
}
else
{
// 通常の四角形
gp.AddRectangle(rect);
}
g.DrawPath(pen, gp);
}
}
finally
{
g.SmoothingMode = sm;
g.PixelOffsetMode = pom;
}
}
}
}
|