HamBook – Blame information for rev 27

Subversion Repositories:
Rev:
Rev Author Line No. Line
27 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 new ComboBoxStyle DropDownStyle { get => base.DropDownStyle; set => base.DropDownStyle = value; }
36  
37 public object Value
38 {
39 get
40 {
41 if (Items.Count == 0)
42 {
43 return null;
44 }
45  
46 return Items[0];
47 }
48 set
49 {
50  
51 if (value == null)
52 {
53 return;
54 }
55  
56 switch (Items.Count)
57 {
58 case 0:
59 Items.Add(value);
60 break;
61 default:
62 Items[0] = value;
63 break;
64 }
65 SelectedItem = Items[0];
66 }
67 }
68  
69 public ScrollableToolStripComboBox() : base()
70 {
71 ComboBox.MouseWheel += ComboBox_MouseWheel;
72 }
73  
74 private void ComboBox_MouseWheel(object sender, MouseEventArgs e)
75 {
76 MouseWheel?.Invoke(this, e);
77 }
78  
79 protected override void OnParentChanged(ToolStrip oldParent, ToolStrip newParent)
80 {
81 base.OnParentChanged(oldParent, newParent);
82  
83 if (newParent != null && newParent is ToolStripDropDownMenu)
84 {
85 newParent.Paint -= new PaintEventHandler(OnParentPaint);
86 newParent.Paint += new PaintEventHandler(OnParentPaint);
87 }
88  
89 if (oldParent != null)
90 oldParent.Paint -= new PaintEventHandler(OnParentPaint);
91 }
92  
93 private void OnParentPaint(object sender, PaintEventArgs e)
94 {
95 if (Image is null ||
96 (sender is ContextMenuStrip cmnu &&
97 !cmnu.ShowImageMargin && !cmnu.ShowCheckMargin))
98 return;
99  
100 var sz = Image.Size;
101 var r = new Rectangle((Bounds.X - sz.Width - 7) / 2,
102 (Bounds.Height - sz.Height) / 2 + Bounds.Y, sz.Width, sz.Height);
103  
104 if (RightToLeft == RightToLeft.Yes)
105 r.X = Bounds.Right + r.Width - ContentRectangle.X;
106  
107 e.Graphics.DrawImage(Image, r);
108 }
109 }
110 }