Devexpress navbarControl 修改带有Skin的样式及背景色

Devexpress navbarControl 修改带有Skin的样式及背景色_第1张图片

选择了皮肤之后,修改background失效了

只好通过代码来修改


 

        private void navBarControl1_CustomDrawBackground(object sender, DevExpress.XtraNavBar.ViewInfo.CustomDrawObjectEventArgs e)
        {
            e.Cache.FillRectangle(new SolidBrush(Color.FromArgb(45,45,46)),e.RealBounds);
            e.Handled = true;
        }

CustomDrawBackground事件只是修改的菜单下面空着的区域的背景色,如下图所示

Devexpress navbarControl 修改带有Skin的样式及背景色_第2张图片

所以还需要修改

CustomDrawGroupCaption是修改一级菜单的
 

  private void navBarControl1_CustomDrawGroupCaption(object sender, CustomDrawNavBarElementEventArgs e)
        {
            NavGroupInfoArgs info = e.ObjectInfo as NavGroupInfoArgs;

            Rectangle rect = e.RealBounds;
            int halfHeight = e.RealBounds.Y + rect.Height / 2 -9;
            // painting the borders
            Rectangle outerRect = new Rectangle(e.RealBounds.X-1,e.RealBounds.Y-1,e.RealBounds.Width+2,e.RealBounds.Height);
            SolidBrush outerBrush = new SolidBrush(Color.FromArgb(80, 80, 80));
            using (outerBrush)
            {
                e.Graphics.FillRectangle(outerBrush, outerRect);
            }

            // painting the background
            Rectangle innerRect = outerRect;
            innerRect.Inflate(-1, -1);
            LinearGradientBrush innerBrush;
            if (e.ObjectInfo.State == ObjectState.Hot)
                innerBrush = new LinearGradientBrush(innerRect,Color.FromArgb(80, 80, 80), Color.FromArgb(80, 80, 80),
                  LinearGradientMode.Vertical);
            else if (e.ObjectInfo.State == ObjectState.Normal)
                innerBrush = new LinearGradientBrush(innerRect, Color.FromArgb(45,45,46), Color.FromArgb(45,45,46),//Color.FromArgb(60, 60, 60), Color.FromArgb(60, 60, 60),
                  LinearGradientMode.Horizontal);
            else
            {
                innerBrush = new LinearGradientBrush(innerRect, Color.FromArgb(80, 80, 80), Color.FromArgb(80, 80, 80),
                  LinearGradientMode.Vertical);
        
            }
            using (innerBrush)
            {
                e.Graphics.FillRectangle(innerBrush, innerRect);
            }

            // painting the caption
            StringFormat outStringFormat = new StringFormat();
            outStringFormat.Alignment = StringAlignment.Near;
            outStringFormat.LineAlignment = StringAlignment.Center;
            SolidBrush textBrush = new SolidBrush(Color.White);
           
            Rectangle stringRect = new Rectangle(e.RealBounds.X+20,e.RealBounds.Y,e.RealBounds.Width-20,e.RealBounds.Height);
            using (textBrush)
            {
                e.Graphics.DrawString(info.Group.Caption, e.Appearance.Font, textBrush,
                  stringRect, outStringFormat);
            }
            e.Graphics.DrawImage(info.Group.SmallImage, new Point(3, halfHeight));
            // prohibiting default painting
            e.Handled = true;
        }

边框,背景色,文字,及图标都可以自定义(SmallImage是添加NavBarGroup时添加的)

DevExpress.XtraNavBar.NavBarGroup nbg = new NavBarGroup();
                nbg.Caption = dr["name"].ToString();               
                nbg.SmallImage = ((System.Drawing.Image)(resources.GetObject(dr["icon"].ToString())));
                nbc.Groups.Add(nbg);

 

 

CustomDrawGroupClientBackground 是修改子菜单样式
 

private void navBarControl1_CustomDrawGroupClientBackground(object sender, DevExpress.XtraNavBar.ViewInfo.CustomDrawObjectEventArgs e)
        {
            //计算顶部和底部的矩形
            Rectangle rect = e.RealBounds;
            int halfHeight = rect.Height / 2;
            Rectangle topRect = new Rectangle(rect.Left,rect.Top,rect.Width,halfHeight);
            Rectangle bottomRect = new Rectangle(rect.Left,rect.Top + halfHeight,
     rect.Width,halfHeight);

            //为顶部和底部创建画笔
            Color outerColor = Color.FromArgb(45,45,46);//Color.FromArgb(60,60,60);
            Color innerColor = Color.FromArgb(45,45,46);//Color.FromArgb(60, 60, 60);
            LinearGradientBrush topBrush = new LinearGradientBrush(topRect, outerColor, innerColor,
              LinearGradientMode.Vertical);
            LinearGradientBrush bottomBrush = new LinearGradientBrush(bottomRect, innerColor,
              outerColor, LinearGradientMode.Vertical);

            //用创建的画笔填充顶部和底部矩形
            e.Graphics.FillRectangle(topBrush, topRect);
            e.Graphics.FillRectangle(bottomBrush, bottomRect);

            //禁止默认绘画      
            e.Handled = true;
        }

点击一级菜单后,展开子菜单

 

private void navBarControl1_Click(object sender, EventArgs e)
        {
            if(sender is NavBarControl)
            {
                NavBarControl nbc = sender as NavBarControl;
                DevExpress.XtraNavBar.NavBarGroup g = nbc.PressedGroup;
                if (g == null) return;
                if (g.Expanded)
                {
                    g.Expanded = false;
                }
                else
                {
                    g.Expanded = true;
                }
            }
        }

 

效果图

Devexpress navbarControl 修改带有Skin的样式及背景色_第3张图片

你可能感兴趣的:(c#)