|
回答ありがとうございます。これでうまくいきそうです。
せっかくなので完成したソースを載せていきたいと思います。
public partial class CustomControl2 : PictureBox
{
[DefaultValue(1.0)]
public double Opacity { get; set; }
public Color CoverBackColor { get; set; }
[DefaultValue("")]
public string strTest { get; set; }
public CustomControl2()
{
InitializeComponent();
strTest = "0";
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.BackColor = Color.Transparent;
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
int alpha = Math.Min(Math.Max((int)Math.Round(255 * Opacity), 0), 255);
System.Drawing.Font font = new System.Drawing.Font("MS UI Gothic", 20);
SizeF sz = pe.Graphics.MeasureString(strTest, font);
Point point = new Point((ClientRectangle.Width - (int)sz.Width) / 2, (ClientRectangle.Height - (int)sz.Height) / 2);
using (var brush = new SolidBrush(Color.FromArgb(alpha, CoverBackColor)))
{
pe.Graphics.FillRectangle(brush, new Rectangle(point.X, point.Y, (int)sz.Width, (int)sz.Height));
}
using (var brush = new SolidBrush(Color.Blue))
{
pe.Graphics.DrawString(strTest, font, brush, point);
}
}
}
|