[C# Tutorial] Making Custom Button
[ 동영상 추가 ] - 오류
프로젝트 새항목 추가 - 클래스 추가 (클래스 이름이 버튼이름이 됨)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/* 추가 참조 */
using System.Windows.Forms;
using System.Drawing;
namespace Bitmap_Button_Test1
{
class FlatButton : Control
{
private SolidBrush borderBrush, textBrush;
private Rectangle borderRectangle;
private bool active = false;
private StringFormat stringFormat = new StringFormat();
/* properties */
public override Cursor Cursor { get; set; } = Cursors.Hand;
public float BorderThickness { get; set; } = 2;
/* constructor */
public FlatButton()
{
borderBrush = new SolidBrush(ColorTranslator.FromHtml("#31302b"));
textBrush = new SolidBrush(ColorTranslator.FromHtml("#FFF"));
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
this.Paint += FlatButton_Paint;
}
/* events */
private void FlatButton_Paint(object sender, PaintEventArgs e)
{
borderRectangle = new Rectangle(0, 0, Width, Height);
e.Graphics.DrawRectangle(new Pen(borderBrush, BorderThickness), borderRectangle);
e.Graphics.DrawString(this.Text, this.Font, (active) ? textBrush : borderBrush, borderRectangle, stringFormat);
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
base.BackColor = ColorTranslator.FromHtml("#31302b");
active = true;
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
base.BackColor = ColorTranslator.FromHtml("#FFF");
active = false;
}
}
}