HamBook – Blame information for rev
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
3 | office | 1 | // Derived from https://stackoverflow.com/questions/63917294/custom-paint-in-toolstriptextbox |
2 | using System; |
||
3 | using System.Collections.Generic; |
||
4 | using System.ComponentModel; |
||
5 | using System.Drawing; |
||
6 | using System.Linq; |
||
7 | using System.Text; |
||
8 | using System.Threading.Tasks; |
||
9 | using System.Windows.Forms; |
||
10 | using System.Windows.Forms.Design; |
||
11 | |||
12 | namespace HamBook.Utilities.Controls |
||
13 | { |
||
14 | [DesignerCategory("ToolStrip"), |
||
15 | ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All)] |
||
16 | public class ScrollableToolStripComboBox : ToolStripComboBox |
||
17 | { |
||
18 | |||
19 | [Browsable(true), |
||
20 | EditorBrowsable(EditorBrowsableState.Always), |
||
21 | DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), |
||
22 | Description("Mouse scrolling")] |
||
23 | public event MouseEventHandler MouseWheel; |
||
24 | |||
25 | /// <summary> |
||
26 | /// The image that will be displayed on the item. |
||
27 | /// </summary> |
||
28 | [Browsable(true), |
||
29 | EditorBrowsable(EditorBrowsableState.Always), |
||
30 | DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), |
||
31 | Category("Appearance"), |
||
32 | Description("The image associated with the object.")] |
||
33 | public override Image Image { get => base.Image; set => base.Image = value; } |
||
34 | |||
35 | public ScrollableToolStripComboBox() : base() |
||
36 | { |
||
37 | this.ComboBox.MouseWheel += ComboBox_MouseWheel; |
||
38 | } |
||
39 | |||
40 | private void ComboBox_MouseWheel(object sender, MouseEventArgs e) |
||
41 | { |
||
42 | MouseWheel?.Invoke(this, e); |
||
43 | } |
||
44 | |||
45 | protected override void OnParentChanged(ToolStrip oldParent, ToolStrip newParent) |
||
46 | { |
||
47 | base.OnParentChanged(oldParent, newParent); |
||
48 | |||
49 | if (newParent != null && newParent is ToolStripDropDownMenu) |
||
50 | { |
||
51 | newParent.Paint -= new PaintEventHandler(OnParentPaint); |
||
52 | newParent.Paint += new PaintEventHandler(OnParentPaint); |
||
53 | } |
||
54 | |||
55 | if (oldParent != null) |
||
56 | oldParent.Paint -= new PaintEventHandler(OnParentPaint); |
||
57 | } |
||
58 | |||
59 | private void OnParentPaint(object sender, PaintEventArgs e) |
||
60 | { |
||
61 | if (Image is null || |
||
62 | (sender is ContextMenuStrip cmnu && |
||
63 | !cmnu.ShowImageMargin && !cmnu.ShowCheckMargin)) |
||
64 | return; |
||
65 | |||
66 | var sz = Image.Size; |
||
67 | var r = new Rectangle((Bounds.X - sz.Width - 7) / 2, |
||
68 | (Bounds.Height - sz.Height) / 2 + Bounds.Y, sz.Width, sz.Height); |
||
69 | |||
70 | if (RightToLeft == RightToLeft.Yes) |
||
71 | r.X = Bounds.Right + r.Width - ContentRectangle.X; |
||
72 | |||
73 | e.Graphics.DrawImage(Image, r); |
||
74 | } |
||
75 | } |
||
76 | } |