Example #1
0
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            base.OnPaintBackground(e);



            Graphics  g = e.Graphics;
            Rectangle imageRect;
            Rectangle textRect;

            CalculateRect(out imageRect, out textRect);
            g.SmoothingMode = SmoothingMode.AntiAlias;

            Color textColor = ControlFaceSchema.TextNormalStyle.Color1;

            if (!ImageMode)//文本模式
            {
                if (Enabled)
                {
                    if (Checked)
                    {
                        switch (ControlState)
                        {
                        case ControlState.Hover:
                            RenderBackgroundInternal(g
                                                     , ClientRectangle
                                                     , ControlFaceSchema.BackHoverStyle
                                                     , ControlFaceSchema.BorderHoverStyle
                                                     , RoundStyle
                                                     , Radius);
                            textColor = ControlFaceSchema.TextHoverStyle.Color1;
                            break;

                        default:
                            RenderBackgroundInternal(g
                                                     , ClientRectangle
                                                     , ControlFaceSchema.BackCheckedStyle
                                                     , ControlFaceSchema.BorderCheckedStyle
                                                     , RoundStyle
                                                     , Radius);
                            textColor = ControlFaceSchema.TextCheckedStyle.Color1;
                            break;
                        }
                    }
                    else
                    {
                        switch (ControlState)
                        {
                        case ControlState.Hover:
                            RenderBackgroundInternal(g
                                                     , ClientRectangle
                                                     , ControlFaceSchema.BackHoverStyle
                                                     , ControlFaceSchema.BorderHoverStyle
                                                     , RoundStyle
                                                     , Radius);
                            textColor = ControlFaceSchema.TextHoverStyle.Color1;
                            break;

                        case ControlState.Pressed:
                            RenderBackgroundInternal(g
                                                     , ClientRectangle
                                                     , ControlFaceSchema.BackCheckedStyle
                                                     , ControlFaceSchema.BorderCheckedStyle
                                                     , RoundStyle
                                                     , Radius);
                            textColor = ControlFaceSchema.TextCheckedStyle.Color1;
                            break;

                        default:
                            RenderBackgroundInternal(g
                                                     , ClientRectangle
                                                     , ControlFaceSchema.BackNormalStyle
                                                     , ControlFaceSchema.BorderNormalStyle
                                                     , RoundStyle
                                                     , Radius);
                            textColor = ControlFaceSchema.TextNormalStyle.Color1;
                            break;
                        }
                    }
                }
                else
                {
                    RenderBackgroundInternal(g
                                             , ClientRectangle
                                             , ControlFaceSchema.BackDisabledStyle
                                             , ControlFaceSchema.BorderDisabledStyle
                                             , RoundStyle
                                             , Radius);
                    textColor = ControlFaceSchema.TextDisabledStyle.Color1;
                }



                if (Image != null)
                {
                    g.InterpolationMode = InterpolationMode.HighQualityBilinear;
                    g.DrawImage(
                        Image,
                        imageRect,
                        0,
                        0,
                        Image.Width,
                        Image.Height,
                        GraphicsUnit.Pixel);
                }
                //画玻璃效果
                if (GlassEnable && Enabled)
                {
                    Rectangle r = ClientRectangle;
                    r.Height = r.Height / 2;
                    if (r.Height > 0 && r.Width > 0)
                    {
                        using (GraphicsPath path =
                                   GraphicsPathHelper.CreatePathHalf(ClientRectangle, Radius, RoundStyle, true))
                        {
                            //255:表示完全不透明,color1是上面。
                            using (LinearGradientBrush br = new LinearGradientBrush(r, Color.FromArgb(255, Color.White), Color.FromArgb(70, Color.White), 90))
                            {
                                g.FillPath(br, path);
                            }
                        }
                    }
                }
                //TextRenderer.DrawText(
                //    g,
                //    Text,
                //    Font,
                //    textRect,
                //    textColor,
                //    GetTextFormatFlags(TextAlign, RightToLeft == RightToLeft.Yes));

                using (StringFormat sf = new StringFormat())
                {
                    sf.Alignment     = StringAlignment.Center;
                    sf.LineAlignment = StringAlignment.Center;
                    sf.Trimming      = StringTrimming.EllipsisCharacter;

                    using (SolidBrush br = new SolidBrush(textColor))
                    {
                        g.DrawString(Text, Font, br, textRect, sf);
                    }
                }
            }
            else//图片模式,直接根据状态贴图即可
            {
                Image img = ImageTable.ImageNormal;
                if (Checked)
                {
                    img = ImageTable.ImageChecked;
                }
                else
                {
                    if (Enabled)
                    {
                        switch (ControlState)
                        {
                        case Ants.Controls.ControlState.Hover:
                            img = ImageTable.ImageHover;
                            break;

                        case Ants.Controls.ControlState.Normal:
                            img = ImageTable.ImageNormal;
                            break;

                        case Ants.Controls.ControlState.Pressed:
                            img = ImageTable.ImageChecked;
                            break;
                        }
                    }
                    else
                    {
                        img = ImageTable.ImageDisalbed;
                    }
                }
                if (img != null)
                {
                    this.Text            = string.Empty;
                    this.BackgroundImage = img;
                    base.OnPaint(e);


                    //this.Width = img.Width;
                    //this.Height = img.Height;
                    //g.DrawImage(
                    //    img,
                    //    e.ClipRectangle,
                    //    0,
                    //    0,
                    //    img.Width,
                    //    img.Height,
                    //    GraphicsUnit.Pixel);
                }

                if (this.DesignMode)
                {
                    using (Pen pen = new Pen(Color.Gray))
                    {
                        Rectangle r = ClientRectangle;
                        r.Height -= 1;
                        r.Width  -= 1;
                        g.DrawRectangle(pen, r);
                    }
                }
            }
        }