QuickImage – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 // Derived from https://stackoverflow.com/questions/63917294/custom-paint-in-toolstriptextbox
2  
3 using System.ComponentModel;
4 using System.Drawing;
5 using System.Windows.Forms;
6 using System.Windows.Forms.Design;
7  
8 namespace QuickImage.Utilities.Controls
9 {
10 [DesignerCategory("ToolStrip")]
11 [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All)]
12 public class ScrollableToolStripComboBox : ToolStripComboBox
13 {
14 public ScrollableToolStripComboBox()
15 {
16 ComboBox.MouseWheel += ComboBox_MouseWheel;
17 }
18  
19 /// <summary>
20 /// The image that will be displayed on the item.
21 /// </summary>
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 }
32  
33 public new ComboBoxStyle DropDownStyle
34 {
35 get => base.DropDownStyle;
36 set => base.DropDownStyle = value;
37 }
38  
39 [Browsable(true)]
40 [EditorBrowsable(EditorBrowsableState.Always)]
41 [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
42 [Description("Mouse scrolling")]
43 public event MouseEventHandler MouseWheel;
44  
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 {
56 newParent.Paint -= OnParentPaint;
57 newParent.Paint += OnParentPaint;
58 }
59  
60 if (oldParent != null)
61 oldParent.Paint -= OnParentPaint;
62 }
63  
64 private void OnParentPaint(object sender, PaintEventArgs e)
65 {
66 if (Image is null ||
67 (sender is ContextMenuStrip cmnu &&
68 !cmnu.ShowImageMargin && !cmnu.ShowCheckMargin))
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 }
81 }