CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2014
    Posts
    2

    Flat Style Dropdown Menu Not Drawing Correctly In Windows XP

    I'm having issues with drawing a flat style dropdown menu in XP. Below is the user control element code I'm using:

    Code:
        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
        }
    The problem I am having is that in Windows XP, the border colors aren't rendering properly. Here is what it looks like:

    Name:  aLv6L.png
Views: 237
Size:  319 Bytes

    And here is what it *should* look like (this is how it renders in Vista and greater):

    Name:  rny68.png
Views: 238
Size:  455 Bytes

    I looked at this documentation about building a custom combobox and focused on the WM_CTLCOLORLISTBOX message; however, I am not sure if this is the proper message to use to paint the combobox...and if it is, how to properly use it to draw the combobox.

    If someone can point me in the right direction, I'd appreciate it.

  2. #2
    Join Date
    Apr 2014
    Posts
    2

    Re: Flat Style Dropdown Menu Not Drawing Correctly In Windows XP

    .NET 4.0 is a prerequisite for this application; I'm using the latest .NET 4.5 on my development machine.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Flat Style Dropdown Menu Not Drawing Correctly In Windows XP

    Does .net 4.5 run on XP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured