|
| 1 | +using System; |
| 2 | +using System.ComponentModel; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Drawing; |
| 5 | +using System.Windows.Forms; |
| 6 | +using Video_Encoder__NET_Core_Version_.Properties; |
| 7 | + |
| 8 | +namespace Video_Encoder__NET_Core_Version_ { |
| 9 | + /// <summary> |
| 10 | + /// Allows to change the border of ComboBox. Code inspired by: https://stackoverflow.com/q/52541869/8233240 |
| 11 | + /// </summary> |
| 12 | + public class CustomComboBox : ComboBox { |
| 13 | + private const int WM_PAINT = 0xF; |
| 14 | + private readonly int buttonWidth = SystemInformation.HorizontalScrollBarArrowWidth; |
| 15 | + protected override void WndProc(ref Message m) { |
| 16 | + base.WndProc(ref m); |
| 17 | + if (m.Msg == WM_PAINT) { |
| 18 | + using (var g = Graphics.FromHwnd(Handle)) { |
| 19 | + using var b = new SolidBrush(Color.FromArgb(48, 48, 48)); |
| 20 | + /// Fills arrow box |
| 21 | + g.FillRectangle(b, Width - buttonWidth, 0, buttonWidth, Height); |
| 22 | + |
| 23 | + using (var p = new Pen(this.BorderColor, 2)) { |
| 24 | + /// Draws border around control |
| 25 | + g.DrawRectangle(p, 0, 0, Width, Height); |
| 26 | + |
| 27 | + /// Draws additional border around button |
| 28 | + p.Width = 1; |
| 29 | + g.DrawRectangle(p, Width - buttonWidth, 0, buttonWidth, Height); |
| 30 | + |
| 31 | + /// Fixes white line on border of button and text box |
| 32 | + p.Color = this.BackColor; |
| 33 | + g.DrawLine(p, new Point(Width - buttonWidth - 1, 1), new Point(Width - buttonWidth - 1, Height - 2)); |
| 34 | + } |
| 35 | + |
| 36 | + /// Draw Icon |
| 37 | + var iconPadding = 3; |
| 38 | + if (buttonWidth < Height) { |
| 39 | + g.DrawIcon(Resources.dropdownIcon, |
| 40 | + new Rectangle( |
| 41 | + Width - buttonWidth + iconPadding, |
| 42 | + ((Height / 2) - (buttonWidth / 2)) + iconPadding, |
| 43 | + buttonWidth - (iconPadding * 2), |
| 44 | + buttonWidth - (iconPadding * 2))); |
| 45 | + } else { |
| 46 | + g.DrawIcon(Resources.dropdownIcon, |
| 47 | + new Rectangle( |
| 48 | + Width - buttonWidth + iconPadding, |
| 49 | + ((buttonWidth / 2) - (Height / 2)) + iconPadding, |
| 50 | + Height - (iconPadding * 2), |
| 51 | + Height - (iconPadding * 2))); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public CustomComboBox() { |
| 58 | + |
| 59 | + BorderColor = Color.DimGray; |
| 60 | + //DoubleBuffered = true; |
| 61 | + this.Enter += PlaceholderChanger; |
| 62 | + this.Leave += PlaceholderChanger; |
| 63 | + } |
| 64 | + |
| 65 | + [Browsable(true)] |
| 66 | + [Category("Appearance")] |
| 67 | + [DefaultValue(typeof(Color), "DimGray")] |
| 68 | + public Color BorderColor { get; set; } |
| 69 | + |
| 70 | + |
| 71 | + #region /// Control Attributes /// |
| 72 | + [Browsable(true)] |
| 73 | + [Category("Appearance")] |
| 74 | + [DefaultValue(AppearanceTypes.Active)] |
| 75 | + [Description("Changes the style of the text from foreground to background. " + |
| 76 | + "Is automatically adjusted if you have a value in PlaceholderText")] |
| 77 | + #endregion |
| 78 | + public enum AppearanceTypes { Active, Passive }; |
| 79 | + private AppearanceTypes _TextAppearance = AppearanceTypes.Active; |
| 80 | + public AppearanceTypes TextAppearance { |
| 81 | + get { return _TextAppearance; } |
| 82 | + set { |
| 83 | + _TextAppearance = value; |
| 84 | + ChangeAppearance(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private void ChangeAppearance() { |
| 89 | + switch (_TextAppearance) { |
| 90 | + case AppearanceTypes.Active: |
| 91 | + this.ForeColor = SystemColors.Control; |
| 92 | + this.Font = new Font(this.Font, FontStyle.Regular); |
| 93 | + break; |
| 94 | + case AppearanceTypes.Passive: |
| 95 | + this.ForeColor = Color.FromArgb(110, 110, 110); |
| 96 | + //this.Font = new Font(this.Font, FontStyle.Italic); |
| 97 | + break; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /// Very dirty solution but it's too much work to render text through the control |
| 102 | + private void PlaceholderChanger(object sender = null, EventArgs e = null) { |
| 103 | + if (!(string.IsNullOrWhiteSpace(this._PlaceholderText))) { /// If the PlaceholderText string isn't empty |
| 104 | + if (this.Text == this.renderedText) { /// If the current text and text to be rendered are the same |
| 105 | + this.Text = ""; /// Set the text to blank |
| 106 | + this.TextAppearance = AppearanceTypes.Active; /// And the Appearance Type to active |
| 107 | + } else if (string.IsNullOrWhiteSpace(this.Text)) { /// OR if the current Text is empty |
| 108 | + this.TextAppearance = AppearanceTypes.Passive; /// Set the Appearance type to passive |
| 109 | + this.Text = this.renderedText; /// And set the text to the text to be rendered |
| 110 | + } else { |
| 111 | + this.TextAppearance = AppearanceTypes.Active; |
| 112 | + } |
| 113 | + } else { /// If the PlaceholderText IS empty |
| 114 | + this.Text = ""; /// Set the text to empty |
| 115 | + this.TextAppearance = AppearanceTypes.Active; /// And Appearance type to active |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + public void CalculatePlaceholderText(object sender = null, EventArgs e = null) { |
| 120 | + using (Graphics g = Graphics.FromHwnd(this.Handle)) { |
| 121 | + int controlSize = this.Width; |
| 122 | + ///ControlSize will return a false value when it first runs, before the form has the correct size |
| 123 | + ///∴ This function will fail on its first execution |
| 124 | + |
| 125 | + /// Measuring space width this way ↓ is inaccurate as it does not account for character kerning |
| 126 | + //float spaceSize = g.MeasureString(" ", this.Font).Width; |
| 127 | + |
| 128 | + /// Using this different method, kerning is accounted for and the space width is more accurate |
| 129 | + float spaceSize = g.MeasureString("| |", this.Font).Width - g.MeasureString("||", this.Font).Width; |
| 130 | + |
| 131 | + float stringSize = g.MeasureString(_PlaceholderText, this.Font).Width; |
| 132 | + double numberOfSpaces = Math.Round(((controlSize - stringSize) / 2) / spaceSize, 0); |
| 133 | + if (numberOfSpaces > 0) { |
| 134 | + renderedText = new string(' ', (int)numberOfSpaces) + _PlaceholderText; |
| 135 | + Debug.WriteLine(this.Name + ": Text Rendering Calculated!"); |
| 136 | + } else { |
| 137 | + renderedText = ""; |
| 138 | + Debug.WriteLine(this.Name + ": Text Rendering Unsuccessful. Rendered text is blank"); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + #region /// Control Attributes /// |
| 144 | + [Browsable(true)] |
| 145 | + [Category("Misc")] |
| 146 | + [DefaultValue("")] |
| 147 | + [Description("Specifies the PlaceholderText of the CustomComboBox control. " + |
| 148 | + "The PlaceholderText is displayed in the control when the Text property is null or empty " + |
| 149 | + "and can be used to guide the user what input is expected by the control.")] |
| 150 | + #endregion |
| 151 | + private string renderedText; |
| 152 | + private string _PlaceholderText; |
| 153 | + public string PlaceholderText { |
| 154 | + get { return _PlaceholderText; } |
| 155 | + set { |
| 156 | + _PlaceholderText = value; |
| 157 | + this.Text = ""; |
| 158 | + CalculatePlaceholderText(); |
| 159 | + PlaceholderChanger(); |
| 160 | + } |
| 161 | + |
| 162 | + } |
| 163 | + |
| 164 | + } |
| 165 | +} |
0 commit comments