QuickImage – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System.ComponentModel;
2 using System.Drawing;
3 using System.Windows.Forms;
4 using System.Windows.Forms.Design;
5  
6 namespace QuickImage.Utilities.Controls
7 {
8 /// <summary>
9 /// https://stackoverflow.com/questions/63917294/custom-paint-in-toolstriptextbox
10 /// </summary>
11 [DesignerCategory("ToolStrip"),
12 ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All)]
13 public class IconToolStripTextBox : ToolStripTextBox
14 {
15 /// <summary>
16 /// The image that will be displayed on the item.
17 /// </summary>
18 [Browsable(true),
19 EditorBrowsable(EditorBrowsableState.Always),
20 DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
21 Category("Appearance"),
22 Description("The image associated with the object.")]
23 public override Image Image { get => base.Image; set => base.Image = value; }
24  
25 protected override void OnParentChanged(ToolStrip oldParent, ToolStrip newParent)
26 {
27 base.OnParentChanged(oldParent, newParent);
28  
29 if (newParent != null && newParent is ToolStripDropDownMenu)
30 {
31 newParent.Paint -= OnParentPaint;
32 newParent.Paint += OnParentPaint;
33 }
34  
35 if (oldParent != null)
36 oldParent.Paint -= OnParentPaint;
37 }
38  
39 private void OnParentPaint(object sender, PaintEventArgs e)
40 {
41 if (Image is null ||
42 (sender is ContextMenuStrip cmnu &&
43 !cmnu.ShowImageMargin && !cmnu.ShowCheckMargin))
44 return;
45  
46 var sz = Image.Size;
47 var r = new Rectangle((Bounds.X - sz.Width - 7) / 2,
48 (Bounds.Height - sz.Height) / 2 + Bounds.Y, sz.Width, sz.Height);
49  
50 if (RightToLeft == RightToLeft.Yes)
51 r.X = Bounds.Right + r.Width - ContentRectangle.X;
52  
53 e.Graphics.DrawImage(Image, r);
54 }
55 }
56 }