Exemplo n.º 1
0
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
//          BufferedGraphicsContext currentContext = BufferedGraphicsManager.Current;
//          BufferedGraphics myBuffer = currentContext.Allocate(e.Graphics, e.ClipRectangle);
//          Graphics g = myBuffer.Graphics;

            Graphics g = e.Graphics;

            g.SmoothingMode      = SmoothingMode.HighQuality;
            g.PixelOffsetMode    = PixelOffsetMode.HighQuality;          //高像素偏移质量
            g.InterpolationMode  = InterpolationMode.HighQualityBicubic;
            g.CompositingQuality = CompositingQuality.HighQuality;


            g.Clear(this.BackColor);

            Rectangle rect = ClientRectangle;

            if (_totalPath == null)
            {
                _totalPath = CommonGraphic.CreateRoundRectPath(rect, OffsetX, OffsetY);
            }

            Color colorDown = BackColor;
            Color colorUp   = BackColor;

            //绘制控件背景色
            SolidBrush lgb = new SolidBrush(BackColor);

            g.FillPath(lgb, _totalPath);

            if (_gpBtnArr.Count > 0)
            {
                Pen splitPenMain = new Pen(CommonGraphic.ColorOffset(BackColor, -0.2f), 1f);
                Pen splitPenSub  = new Pen(CommonGraphic.ColorOffset(BackColor, 0.2f), 1f);

                Color pressedColor = CommonGraphic.ColorOffset(BackColor, -0.2f);

                Image bkImage = null;

                for (int i = 0; i < _gpBtnArr.Count; i++)
                {
                    bkImage = _gpBtnArr[i].BackgroundImage;

                    if (_gpBtnArr[i].Pressed)
                    {
                        using (SolidBrush btnbsh = new SolidBrush(pressedColor))
                        {
                            g.FillPath(btnbsh, _gpBtnArr[i].BtnPath);
                        }

                        if (_gpBtnArr[i].SelectedBKImage != null)
                        {
                            bkImage = _gpBtnArr[i].SelectedBKImage;
                        }
                    }
                    else if (_gpBtnArr[i].MouseHover)
                    {
                        using (SolidBrush btnbsh = new SolidBrush(CommonGraphic.ColorOffset(this.BackColor, -0.05f) /*CommonGraphic.ColorOffset(Color.White, -240, 0, 0, 0)*/))
                        {
                            g.FillPath(btnbsh, _gpBtnArr[i].BtnPath);
                        }
                    }

                    if (i < _gpBtnArr.Count - 1 &&
                        i != this.MouseHoverIndex - 1 &&
                        i != this.MouseHoverIndex &&
                        i != this.PressedIndex - 1 &&
                        i != this.PressedIndex)
                    {
                        CommonGraphic.DrawLine(g, splitPenMain,
                                               _gpBtnArr[i].BtnRect.Right,
                                               _gpBtnArr[i].BtnRect.Top + 6f,
                                               _gpBtnArr[i].BtnRect.Right,
                                               _gpBtnArr[i].BtnRect.Bottom - 6f);

                        CommonGraphic.DrawLine(g, splitPenSub,
                                               _gpBtnArr[i].BtnRect.Right + 1f,
                                               _gpBtnArr[i].BtnRect.Top + 6f,
                                               _gpBtnArr[i].BtnRect.Right + 1f,
                                               _gpBtnArr[i].BtnRect.Bottom - 6f);
                    }

                    Rectangle txtRect = Rectangle.Round(_gpBtnArr[i].BtnRect);

                    if (bkImage != null)
                    {
                        SizeF fontSize = g.MeasureString(_gpBtnArr[i].Text, this.Font);

                        float height    = (float)Math.Round(_gpBtnArr[i].BtnRect.Height * 0.76f, 1);                                    //图片高度;
                        float width     = (float)Math.Round(height * bkImage.Width / bkImage.Height);                                   //图片宽度;
                        float textWidth = fontSize.Width * 1.3f;                                                                        //文字宽度;

                        float imageLeftOffset = (_gpBtnArr[i].BtnRect.Width - (width + textWidth)) / 2f;                                //图片相对于按钮左边偏移

                        g.DrawImage(bkImage,
                                    _gpBtnArr[i].BtnRect.Left + imageLeftOffset,
                                    _gpBtnArr[i].BtnRect.Top + (_gpBtnArr[i].BtnRect.Height - height) / 2.0f,
                                    width, height);

                        txtRect.Offset((int)(imageLeftOffset + width), 0);
                        txtRect.Width = (int)textWidth;
                    }

                    TextRenderer.DrawText(g,
                                          _gpBtnArr[i].Text,
                                          this.Font,
                                          txtRect,
                                          this.ForeColor,
                                          TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
                }
            }

//          myBuffer.Render(e.Graphics);  //呈现图像至关联的Graphics
//          myBuffer.Dispose();
//          g.Dispose();
        }
Exemplo n.º 2
0
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Graphics g = e.Graphics;

            g.SmoothingMode      = SmoothingMode.HighQuality;
            g.PixelOffsetMode    = PixelOffsetMode.HighQuality;          //高像素偏移质量
            g.InterpolationMode  = InterpolationMode.HighQualityBicubic;
            g.CompositingQuality = CompositingQuality.HighQuality;

            g.Clear(this.BackColor);

            Rectangle rect = ClientRectangle;

            if (_totalPath == null)
            {
                _totalPath = new GraphicsPath();

                _totalPath.Reset();
                _totalPath.AddRectangle(ClientRectangle);
            }

            SolidBrush bsh = new SolidBrush(BackColor);

            g.FillPath(bsh, _totalPath);

            if (_gpBtnArr.Count > 0)
            {
                Pen splitPenUp   = new Pen(CommonGraphic.ColorOffset(BackColor, -0.3f), 1.5f);
                Pen splitPenDown = new Pen(CommonGraphic.ColorOffset(BackColor, 0.2f), 1.5f);

                Image bkImage = null;

                for (int i = 0; i < _gpBtnArr.Count; i++)
                {
                    Color     txtColor = Color.White;
                    Rectangle txtRect  = Rectangle.Round(_gpBtnArr[i].BtnRect);

                    bkImage = _gpBtnArr[i].BackgroundImage;

                    if (_gpBtnArr[i].Selected)
                    {
                        txtColor = Color.White;

                        if (_gpBtnArr[i].SelectedBKImage != null)
                        {
                            bkImage = _gpBtnArr[i].SelectedBKImage;
                        }

                        SolidBrush btnbsh = new SolidBrush(SelectedColor);
                        g.FillPath(btnbsh, _gpBtnArr[i].BtnPath);

                        Color spiltLine = CommonGraphic.ColorOffset(SelectedColor, -0.3f);

                        Pen linePen = new Pen(spiltLine);
                        g.DrawLine(linePen,
                                   _gpBtnArr[i].BtnRect.Right,
                                   _gpBtnArr[i].BtnRect.Top + 10.0f,
                                   _gpBtnArr[i].BtnRect.Right,
                                   _gpBtnArr[i].BtnRect.Bottom - 10.0f);
                    }
                    else if (_gpBtnArr[i].MouseHover)
                    {
                        txtRect.Inflate(-5, -5);
                        txtRect.Offset(5, 5);
                    }

                    if (i != this.SelectedIndex - 1 &&
                        i != this.SelectedIndex)
                    {
                        CommonGraphic.DrawLine(g, splitPenUp,
                                               _gpBtnArr[i].BtnRect.Left + 2f,
                                               _gpBtnArr[i].BtnRect.Bottom,
                                               _gpBtnArr[i].BtnRect.Right - 2f,
                                               _gpBtnArr[i].BtnRect.Bottom);

                        CommonGraphic.DrawLine(g, splitPenDown,
                                               _gpBtnArr[i].BtnRect.Left + 2f,
                                               _gpBtnArr[i].BtnRect.Bottom + 1,
                                               _gpBtnArr[i].BtnRect.Right - 2f,
                                               _gpBtnArr[i].BtnRect.Bottom + 1);
                    }

                    if (bkImage != null)
                    {
                        float height = _gpBtnArr[i].BtnRect.Height * 0.5f;
                        float width  = height * bkImage.Width / bkImage.Height;

                        g.DrawImage(bkImage,
                                    _gpBtnArr[i].BtnRect.Left + (_gpBtnArr[i].BtnRect.Height - width) / 2.0f,
                                    _gpBtnArr[i].BtnRect.Top + (_gpBtnArr[i].BtnRect.Height - height) / 2.0f,
                                    width, height);

                        txtRect.Offset((int)_gpBtnArr[i].BtnRect.Height, 0);
                        txtRect.Width -= (int)_gpBtnArr[i].BtnRect.Height;
                    }

                    TextRenderer.DrawText(g,
                                          _gpBtnArr[i].Text,
                                          this.Font,
                                          txtRect,
                                          txtColor,
                                          /*TextFormatFlags.HorizontalCenter | */ TextFormatFlags.VerticalCenter);

                    if (_highLightDic.ContainsKey(i))
                    {
                        if (_highLightDic[i] > 0)
                        {
                            Rectangle numRect = new Rectangle();
                            numRect.X      = (int)_gpBtnArr[i].BtnRect.Right - 35;
                            numRect.Y      = (int)_gpBtnArr[i].BtnRect.Top + (int)_gpBtnArr[i].BtnRect.Height / 2 - 15;
                            numRect.Width  = 30;
                            numRect.Height = 30;

                            bsh.Color = Color.Red;
                            g.FillEllipse(bsh, numRect);

                            numRect.Offset(0, -2);
                            TextRenderer.DrawText(g,
                                                  _highLightDic[i].ToString(),
                                                  _numFont,
                                                  numRect,
                                                  Color.White,
                                                  TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
                        }
                    }
                }

                if (EnableDisplayTime)
                {
                    TextRenderer.DrawText(g,
                                          DateTime.Now.ToString(TimeFormat),
                                          this.TimeFont,
                                          _timeRect,
                                          Color.White,
                                          TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
                }
            }
        }