Skip to content

Instantly share code, notes, and snippets.

@hirosof
Created June 1, 2014 13:02
Show Gist options
  • Save hirosof/d903654ba45c4ec31588 to your computer and use it in GitHub Desktop.
Save hirosof/d903654ba45c4ec31588 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MyControlTest
{
public enum ETextAlign
{
Left = 0,
Center,
Right
}
public enum ETextAlignV
{
Top = 0,
Center,
Bottom
}
public partial class UserControl1 : UserControl
{
private string _Caption = "DisplayName";
private bool _EnableBackGradation = false;
private Color _ToGradationColor = Color.Black;
private ETextAlign _TA = ETextAlign.Center;
private ETextAlignV _TAVertical = ETextAlignV.Top;
private LinearGradientMode _GradationMode = LinearGradientMode.Vertical;
private bool _InverseColorMode = false;
public UserControl1()
{
InitializeComponent();
}
[Description("ここに指定した文字列が表示されます。")]
public string Caption
{
get { return _Caption; }
set { _Caption = value; Invalidate(); }
}
[Description("グラデーションを有効にするかを指定します。")]
public bool EnableBackGradation
{
get { return _EnableBackGradation; }
set { _EnableBackGradation = value; Invalidate(); }
}
[Description("グラデーション先の色を指定します。")]
public Color ToGradationColor
{
get { return _ToGradationColor; }
set { _ToGradationColor = value; Invalidate(); }
}
[Description("表示文字列の横位置を指定します。")]
public ETextAlign TextAlign
{
get { return _TA; }
set { _TA = value; Invalidate(); }
}
[Description("表示文字列の縦位置を指定します。")]
public ETextAlignV TextAlignVertical
{
get { return _TAVertical; }
set { _TAVertical = value; Invalidate(); }
}
[Description("グラデーションのモードを指定します。")]
public LinearGradientMode GradationMode
{
get { return _GradationMode;}
set { _GradationMode = value; Invalidate(); }
}
[Description("グラデーションの開始色(BackColorプロパティ)と終了色(ToGradationColorプロパティ)の関係を入れ替えます")]
public bool InverseColorMode
{
get { return _InverseColorMode; }
set { _InverseColorMode = value; Invalidate(); }
}
private void UserControl1_Paint(object sender, PaintEventArgs e)
{
Brush bgBrush;
Graphics g = e.Graphics;
Rectangle rect = new Rectangle();
rect.Size = this.Size;
//背景を描画
if (_EnableBackGradation)
{
Color[] color = new Color[2];
if (_InverseColorMode)
{
color[1] = this.BackColor;
color[0] = ToGradationColor;
}
else
{
color[0] = this.BackColor;
color[1] = ToGradationColor;
}
bgBrush = new LinearGradientBrush(rect, color[0], color[1], GradationMode);
}
else
{
bgBrush = new SolidBrush(this.BackColor);
}
g.FillRectangle(bgBrush, rect);
//文字列を描画
SolidBrush textBrush = new SolidBrush(this.ForeColor);
Font font = this.Font;
RectangleF textRect = new RectangleF();
textRect.Size = g.MeasureString(_Caption, font); ;
//横位置を設定
switch (_TA)
{
case ETextAlign.Left:
textRect.X = 0;
break;
case ETextAlign.Center:
textRect.X = (this.Size.Width - textRect.Size.Width) / 2;
break;
case ETextAlign.Right:
textRect.X = this.Size.Width - textRect.Size.Width;
break;
}
switch (_TAVertical)
{
case ETextAlignV.Top:
textRect.Y = 0;
break;
case ETextAlignV.Center:
textRect.Y = (this.Size.Height - textRect.Size.Height) / 2;
break;
case ETextAlignV.Bottom:
textRect.Y = this.Size.Height - textRect.Size.Height;
break;
}
g.DrawString(_Caption, font, textBrush, textRect);
}
private void UserControl1_SizeChanged(object sender, EventArgs e)
{
Invalidate();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment