3 Replies - 439 Views - Last Post: 23 April 2014 - 06:12 PM Rate Topic: -----

#1 dutchcoffee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-January 14

Flat style dropdown menu not drawing correctly in XP

Posted 23 April 2014 - 04:32 PM

I'm having issues with drawing a flat style dropdown menu in XP.

Here is the custom dropdown menu element:

    public class FlattenCombo : ComboBox
    {
        private Brush BorderBrush = new SolidBrush(SystemColors.WindowFrame);
        private Brush ArrowBrush = new SolidBrush(SystemColors.ControlText);
        private Brush DropButtonBrush = new SolidBrush(SystemColors.Control);
        public Color HighlightColor { get; set; }

        private Color _ButtonColor = SystemColors.Control;
        public Color ButtonColor
        {
            get { return _ButtonColor; }
            set
            {
                _ButtonColor = value;
                DropButtonBrush = new SolidBrush(this.ButtonColor);
                this.Invalidate();
            }
        }

        private Color _borderColor = Color.Black;
        private ButtonBorderStyle _borderStyle = ButtonBorderStyle.Solid;
        //private static int WM_PAINT = 0x000F;

        [Category("Appearance")]
        public Color BorderColor
        {
            get { return _borderColor; }
            set
            {
                _borderColor = value;
                this.Invalidate(); // causes control to be redrawn
            }
        }

        [Category("Appearance")]
        public ButtonBorderStyle BorderStyle
        {
            get { return _borderStyle; }
            set
            {
                _borderStyle = value;
                this.Invalidate();
            }
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            switch (m.Msg)
            {
                case 0xf:
                    //Paint the background. Only the borders will show up because the edit box will be overlayed
                    Graphics g = this.CreateGraphics();
                    Rectangle bounds = new Rectangle(0, 0, Width, Height);
                    ControlPaint.DrawBorder(g, bounds, _borderColor, _borderStyle);

                    //Draw the background of the dropdown button
                    Rectangle rect = new Rectangle(this.Width - 18, 0, 18, this.Height);
                    g.FillRectangle(DropButtonBrush, rect);

                    //Create the path for the arrow
                    System.Drawing.Drawing2D.GraphicsPath pth = new System.Drawing.Drawing2D.GraphicsPath();
                    PointF TopLeft = new PointF(this.Width - 13, (this.Height - 5) / 2);
                    PointF TopRight = new PointF(this.Width - 6, (this.Height - 5) / 2);
                    PointF Bottom = new PointF(this.Width - 9, (this.Height + 2) / 2);
                    pth.AddLine(TopLeft, TopRight);
                    pth.AddLine(TopRight, Bottom);

                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                    //Determine the arrow's color.
                    if (this.DroppedDown)
                        ArrowBrush = new SolidBrush(SystemColors.HighlightText);
                    else
                        ArrowBrush = new SolidBrush(SystemColors.ControlText);

                    //Draw the arrow
                    g.FillPath(ArrowBrush, pth);

                    break;
            }
        }

        //Override mouse and focus events to draw
        //proper borders. Basically, set the color and Invalidate(),
        //In general, Invalidate causes a control to redraw itself.
        //#region "Mouse and focus Overrides"
        protected override void onmouseenter(System.EventArgs e)
        {
            base.onmouseenter(e);
            //BorderBrush = new SolidBrush(SystemColors.Highlight);
            //EnableDoubleBuffering();
            this.Invalidate();
        }

        protected override void onmouseleave(System.EventArgs e)
        {
            base.onmouseleave(e);
            /*if (this.Focused)
                return;*/
            BorderBrush = new SolidBrush(SystemColors.WindowFrame);
            //EnableDoubleBuffering();
            this.Invalidate();
        }

        protected override void OnLostFocus(System.EventArgs e)
        {
            base.OnLostFocus(e);
            //BorderBrush = new SolidBrush(SystemColors.Window);
            //EnableDoubleBuffering();
            this.Invalidate();
        }

        protected override void OnGotFocus(System.EventArgs e)
        {
            base.OnGotFocus(e);
            BorderBrush = new SolidBrush(SystemColors.WindowFrame);
            //EnableDoubleBuffering();
            this.Invalidate();
        }

        protected override void OnMouseHover(System.EventArgs e)
        {
            base.OnMouseHover(e);
            BorderBrush = new SolidBrush(SystemColors.WindowFrame);
            //EnableDoubleBuffering();
            this.Invalidate();
        }
        //#endregion
    }


Here is what it looks like in XP:

Posted Image

And here's what it should look like (this is how it's rendered in Windows Vista/7):

Posted Image

Any idea on how to fix this?

Is This A Good Question/Topic? 0
  • +

Replies To: Flat style dropdown menu not drawing correctly in XP

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3631
  • View blog
  • Posts: 11,327
  • Joined: 05-May 12

Re: Flat style dropdown menu not drawing correctly in XP

Posted 23 April 2014 - 05:44 PM

Hmmm... Why go through all this trouble, when on XP if you don't call EnableVisualStyles(), the OS is supposed to respect the FlatStyle property?
Was This Post Helpful? 0
  • +
  • -

#3 dutchcoffee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-January 14

Re: Flat style dropdown menu not drawing correctly in XP

Posted 23 April 2014 - 05:52 PM

I want to at least apply a different border style other than the white...is this possible?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3631
  • View blog
  • Posts: 11,327
  • Joined: 05-May 12

Re: Flat style dropdown menu not drawing correctly in XP

Posted 23 April 2014 - 06:12 PM

It should be possible. It's just a matter of figuring out what the rendering order for the control(s) are and being the last one to paint so that you overwrite the others hard work.

Are you sure that SystemColors.WindowFrame is not white on XP?

Also, have you looked at ensuring your are passing the correct color to WM_CTLCOLORLISTBOX? This article may help: Building a Better ComboBox
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1