こんなボタンばっかりでは味気ないので、スタイリッシュなボタンをつくってみました。やっていることはPictureBoxのうえにラベルを貼り付けているだけです。
MouseDown・MouseMove・MouseDown等のマウスイベントは、各コントロールで発生するため、PictureButtonで一括して受信できるように、明示的にイベントハンドラを設定しています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
public class PictureButton : PictureBox { public PictureButton() { this.SizeMode = PictureBoxSizeMode.StretchImage; label.Parent = this; label.Dock = DockStyle.Fill; label.TextAlign = ContentAlignment.MiddleCenter; label.Text = "Text"; label.BackColor = Color.Transparent; Font font = new Font("MS ゴシック", 10, FontStyle.Bold); label.Font = font; label.ForeColor = Color.White; SetEventHandler(); } private void SetEventHandler() { label.MouseDown += (sender, e) => this.OnMouseDown(e); label.MouseMove += (sender, e) => this.OnMouseMove(e); label.MouseUp += (sender, e) => this.OnMouseUp(e); label.MouseEnter += (sender, e) => this.OnMouseEnter(e); label.MouseHover += (sender, e) => this.OnMouseHover(e); label.MouseLeave += (sender, e) => this.OnMouseLeave(e); label.MouseClick += (sender, e) => this.OnMouseClick(e); label.MouseDoubleClick += (sender, e) => this.OnMouseDoubleClick(e); label.Click += (sender, e) => this.OnClick(e); label.DoubleClick += (sender, e) => this.OnDoubleClick(e); label.AllowDrop = true; label.DragOver += (sender, e) => this.OnDragOver(e); label.DragDrop += (sender, e) => this.OnDragDrop(e); label.DragEnter += (sender, e) => this.OnDragEnter(e); label.DragLeave += (sender, e) => this.OnDragLeave(e); } Label label = new Label(); [Browsable(true)] [EditorBrowsable(EditorBrowsableState.Always)] new public string Text { get { return label.Text; } set { label.Text = value; } } [Browsable(true)] [EditorBrowsable(EditorBrowsableState.Always)] new public Color ForeColor { get { return label.ForeColor; } set { label.ForeColor = value; } } [Browsable(true)] [EditorBrowsable(EditorBrowsableState.Always)] new public Font Font { get { return label.Font; } set { label.Font = value; } } } |