Пример #1
0
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics  g = e.Graphics;
            Rectangle r = new Rectangle(ClientSize.Width - 16, 1, 16, 18);

            ControlPaint.DrawComboButton(g, r, Drop ? ButtonState.Pushed : ButtonState.Normal);
        }
        /// <summary>
        /// Paints the control.
        /// </summary>
        /// <param name="e">The <see cref="PaintEventArgs"/> instance containing the event data.</param>
        protected virtual void PaintControl(PaintEventArgs e)
        {
            var cbi = NativeMethods.COMBOBOXINFO.FromComboBox(this);

            var       itemText = SelectedIndex >= 0 ? GetItemText(SelectedItem) : string.Empty;
            var       state    = Enabled ? currentState : ComboBoxState.Disabled;
            Rectangle tr       = cbi.rcItem;

            /*Rectangle tr = this.ClientRectangle;
             * tr.Width -= (SystemInformation.VerticalScrollBarWidth + 2);
             * tr.Inflate(0, -2);
             * tr.Offset(1, 0);*/
            Rectangle br        = cbi.rcButton;
            var       vsSuccess = false;

            if (VisualStyleRenderer.IsSupported && Application.RenderWithVisualStyles)
            {
                /*Rectangle r = Rectangle.Inflate(this.ClientRectangle, 1, 1);
                 * if (this.DropDownStyle != ComboBoxStyle.DropDownList)
                 * {
                 *      e.Graphics.Clear(this.BackColor);
                 *      ComboBoxRenderer.DrawTextBox(e.Graphics, r, itemText, this.Font, tr, tff, state);
                 *      ComboBoxRenderer.DrawDropDownButton(e.Graphics, br, state);
                 * }
                 * else*/
                {
                    try
                    {
                        var vr = new VisualStyleRenderer("Combobox", DropDownStyle == ComboBoxStyle.DropDownList ? 5 : 4, (int)state);
                        vr.DrawParentBackground(e.Graphics, ClientRectangle, this);
                        vr.DrawBackground(e.Graphics, ClientRectangle);
                        if (DropDownStyle != ComboBoxStyle.DropDownList)
                        {
                            br.Inflate(1, 1);
                        }
                        var cr = DropDownStyle == ComboBoxStyle.DropDownList ? Rectangle.Inflate(br, -1, -1) : br;
                        vr.SetParameters("Combobox", 7, (int)(br.Contains(PointToClient(Cursor.Position)) ? state : ComboBoxState.Normal));
                        vr.DrawBackground(e.Graphics, br, cr);
                        if (Focused && State != ComboBoxState.Pressed)
                        {
                            var sz = TextRenderer.MeasureText(e.Graphics, "Wg", Font, tr.Size, TextFormatFlags.Default);
                            var fr = Rectangle.Inflate(tr, 0, (sz.Height - tr.Height) / 2 + 1);
                            ControlPaint.DrawFocusRectangle(e.Graphics, fr);
                        }
                        var fgc = Enabled ? ForeColor : SystemColors.GrayText;
                        TextRenderer.DrawText(e.Graphics, itemText, Font, tr, fgc, tff);
                        vsSuccess = true;
                    }
                    catch { }
                }
            }

            if (!vsSuccess)
            {
                Diagnostics.Debug.WriteLine($"CR:{ClientRectangle};ClR:{e.ClipRectangle};Foc:{Focused};St:{state};Tx:{itemText}");
                var focusedNotPressed = Focused && state != ComboBoxState.Pressed;
                var bgc = Enabled ? (focusedNotPressed ? SystemColors.Highlight : BackColor) : SystemColors.Control;
                var fgc = Enabled ? (focusedNotPressed ? SystemColors.HighlightText : ForeColor) : SystemColors.GrayText;
                e.Graphics.Clear(bgc);
                ControlPaint.DrawBorder3D(e.Graphics, ClientRectangle, Border3DStyle.Sunken);
                ControlPaint.DrawComboButton(e.Graphics, br, Enabled ? (state == ComboBoxState.Pressed ? ButtonState.Pushed : ButtonState.Normal) : ButtonState.Inactive);
                tr = new Rectangle(tr.X + 3, tr.Y + 1, tr.Width - 4, tr.Height - 2);
                TextRenderer.DrawText(e.Graphics, itemText, Font, tr, fgc, tff);
                if (focusedNotPressed)
                {
                    var fr = Rectangle.Inflate(cbi.rcItem, -1, -1);
                    fr.Height++;
                    fr.Width++;
                    ControlPaint.DrawFocusRectangle(e.Graphics, fr, fgc, bgc);
                    e.Graphics.DrawRectangle(SystemPens.Window, cbi.rcItem);
                }
            }
        }
        /// <summary>
        /// Paints the control.
        /// </summary>
        /// <param name="e">The <see cref="PaintEventArgs"/> instance containing the event data.</param>
        protected virtual void PaintControl(PaintEventArgs e)
        {
            var cbi = NativeMethods.COMBOBOXINFO.FromComboBox(this);

            string        itemText = this.SelectedIndex >= 0 ? this.GetItemText(this.SelectedItem) : string.Empty;
            ComboBoxState state    = Enabled ? currentState : ComboBoxState.Disabled;
            Rectangle     tr       = cbi.rcItem;

            /*Rectangle tr = this.ClientRectangle;
             * tr.Width -= (SystemInformation.VerticalScrollBarWidth + 2);
             * tr.Inflate(0, -2);
             * tr.Offset(1, 0);*/
            Rectangle br        = cbi.rcButton;
            bool      vsSuccess = false;

            if (VisualStyleRenderer.IsSupported && Application.RenderWithVisualStyles)
            {
                /*Rectangle r = Rectangle.Inflate(this.ClientRectangle, 1, 1);
                 * if (this.DropDownStyle != ComboBoxStyle.DropDownList)
                 * {
                 *      e.Graphics.Clear(this.BackColor);
                 *      ComboBoxRenderer.DrawTextBox(e.Graphics, r, itemText, this.Font, tr, tff, state);
                 *      ComboBoxRenderer.DrawDropDownButton(e.Graphics, br, state);
                 * }
                 * else*/
                {
                    try
                    {
                        var vr = new VisualStyleRenderer("Combobox", this.DropDownStyle == ComboBoxStyle.DropDownList ? 5 : 4, (int)state);
                        vr.DrawParentBackground(e.Graphics, this.ClientRectangle, this);
                        vr.DrawBackground(e.Graphics, this.ClientRectangle);
                        if (this.DropDownStyle != ComboBoxStyle.DropDownList)
                        {
                            br.Inflate(1, 1);
                        }
                        Rectangle cr = this.DropDownStyle == ComboBoxStyle.DropDownList ? Rectangle.Inflate(br, -1, -1) : br;
                        vr.SetParameters("Combobox", 7, (int)(br.Contains(this.PointToClient(Cursor.Position)) ? state : ComboBoxState.Normal));
                        vr.DrawBackground(e.Graphics, br, cr);
                        if (this.Focused && State != ComboBoxState.Pressed)
                        {
                            Size      sz = TextRenderer.MeasureText(e.Graphics, "Wg", this.Font, tr.Size, TextFormatFlags.Default);
                            Rectangle fr = Rectangle.Inflate(tr, 0, ((sz.Height - tr.Height) / 2) + 1);
                            ControlPaint.DrawFocusRectangle(e.Graphics, fr);
                        }
                        TextRenderer.DrawText(e.Graphics, itemText, this.Font, tr, this.ForeColor, tff);
                        vsSuccess = true;
                    }
                    catch { }
                }
            }

            if (!vsSuccess)
            {
                System.Diagnostics.Debug.WriteLine(string.Format("CR:{0};ClR:{1};Foc:{2};St:{3};Tx:{4}", ClientRectangle, e.ClipRectangle, this.Focused, state, itemText));
                e.Graphics.Clear(this.BackColor);
                ControlPaint.DrawBorder3D(e.Graphics, this.ClientRectangle, Border3DStyle.Sunken);
                ControlPaint.DrawComboButton(e.Graphics, br, this.Enabled ? (state == ComboBoxState.Pressed ? ButtonState.Pushed : ButtonState.Normal) : ButtonState.Inactive);
                //using (var bb = new SolidBrush(this.BackColor))
                //	e.Graphics.FillRectangle(bb, tr);
                if (this.Focused)
                {
                    Size      sz = TextRenderer.MeasureText(e.Graphics, "Wg", this.Font, tr.Size, TextFormatFlags.Default);
                    Rectangle fr = Rectangle.Inflate(tr, 0, ((sz.Height - tr.Height) / 2) + 1);
                    e.Graphics.FillRectangle(SystemBrushes.Highlight, fr);
                    ControlPaint.DrawFocusRectangle(e.Graphics, fr);                     //, this.ForeColor, SystemColors.Highlight);
                }
                TextRenderer.DrawText(e.Graphics, itemText, this.Font, tr, this.Focused ? SystemColors.HighlightText : this.ForeColor, tff);
            }
        }
Пример #4
0
 private void DrawButton(Graphics g)
 {
     ControlPaint.DrawComboButton(g, ClientSize.Width - ButtonSize.Width, 0, ButtonSize.Width, ButtonSize.Height, buttonState);
 }