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