HamBook – Blame information for rev 29

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 [Browsable(true),
19 EditorBrowsable(EditorBrowsableState.Always),
20 DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
21 Description("Mouse scrolling")]
22 public event MouseEventHandler MouseWheel;
23  
24 /// <summary>
25 /// The image that will be displayed on the item.
26 /// </summary>
27 [Browsable(true),
28 EditorBrowsable(EditorBrowsableState.Always),
29 DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
30 Category("Appearance"),
31 Description("The image associated with the object.")]
32 public override Image Image { get => base.Image; set => base.Image = value; }
33  
34 public new ComboBoxStyle DropDownStyle { get => base.DropDownStyle; set => base.DropDownStyle = value; }
35  
36 public ScrollableToolStripComboBox() : base()
37 {
38 ComboBox.MouseWheel += ComboBox_MouseWheel;
39 }
40  
41 private void ComboBox_MouseWheel(object sender, MouseEventArgs e)
42 {
43 MouseWheel?.Invoke(this, e);
44 }
45  
46 protected override void OnParentChanged(ToolStrip oldParent, ToolStrip newParent)
47 {
48 base.OnParentChanged(oldParent, newParent);
49  
50 if (newParent != null && newParent is ToolStripDropDownMenu)
51 {
52 newParent.Paint -= new PaintEventHandler(OnParentPaint);
53 newParent.Paint += new PaintEventHandler(OnParentPaint);
54 }
55  
56 if (oldParent != null)
57 oldParent.Paint -= new PaintEventHandler(OnParentPaint);
58 }
59  
60 private void OnParentPaint(object sender, PaintEventArgs e)
61 {
62 if (Image is null ||
63 (sender is ContextMenuStrip cmnu &&
64 !cmnu.ShowImageMargin && !cmnu.ShowCheckMargin))
65 return;
66  
67 var sz = Image.Size;
68 var r = new Rectangle((Bounds.X - sz.Width - 7) / 2,
69 (Bounds.Height - sz.Height) / 2 + Bounds.Y, sz.Width, sz.Height);
70  
71 if (RightToLeft == RightToLeft.Yes)
72 r.X = Bounds.Right + r.Width - ContentRectangle.X;
73  
74 e.Graphics.DrawImage(Image, r);
75 }
76 }
77 }