SHOW:
|
|
- or go back to the newest paste.
1 | public class BasicButton : Control | |
2 | { | |
3 | ||
4 | private MouseState _mouseState = MouseState.Normal; | |
5 | protected override void CreateHandle() | |
6 | { | |
7 | base.CreateHandle(); | |
8 | ||
9 | } | |
10 | protected override void OnPaint(PaintEventArgs e) | |
11 | { | |
12 | var g = e.Graphics; | |
13 | g.Clear(Color.White); | |
14 | g.DrawString(Text, Font, new SolidBrush(ForeColor), new Point(0, 0)); | |
15 | ||
16 | switch (_mouseState) | |
17 | { | |
18 | case MouseState.Normal: g.FillRectangle(Brushes.Orange, ClientRectangle); | |
19 | break; | |
20 | case MouseState.Down: | |
21 | g.FillRectangle(Brushes.DarkOrange, ClientRectangle); | |
22 | break; | |
23 | ||
24 | } | |
25 | base.OnPaint(e); | |
26 | - | g.DrawString(Text, Font, new SolidBrush(ForeColor), new Rectangle(StringAlignment.Center));//Butchered it |
26 | + | StringFormat sf = new StringFormat(); |
27 | sf.LineAlignment = StringAlignment.Center; | |
28 | sf.Alignment = StringAlignment.Center; | |
29 | g.DrawString(Text, Font, new SolidBrush(ForeColor), new Rectangle(0, 0, Width, Height), sf);//Butchered it | |
30 | } | |
31 | private void SwitchMouseState(MouseState state) | |
32 | { | |
33 | _mouseState = state; | |
34 | Invalidate(); | |
35 | } | |
36 | protected override void OnMouseUp(MouseEventArgs e) | |
37 | { | |
38 | SwitchMouseState(MouseState.Normal); | |
39 | base.OnMouseUp(e); | |
40 | } | |
41 | protected override void OnMouseDown(MouseEventArgs e) | |
42 | { | |
43 | SwitchMouseState(MouseState.Down); | |
44 | base.OnMouseDown(e); | |
45 | } | |
46 | ||
47 | } |