Exemplo n.º 1
0
        public PFlatButton()
        {
            Width         = 65;
            Height        = 30;
            stroke        = false;
            strokeColor   = Color.Gray;
            inactive1     = Color.FromArgb(44, 188, 210);
            inactive2     = Color.FromArgb(33, 167, 188);
            active1       = Color.FromArgb(64, 168, 183);
            active2       = Color.FromArgb(36, 164, 183);
            gradientAngle = 180f;
            _autoSizeFont = true;
            _multiLine    = false;
            _StyleButton  = EStyleButton.Custom;
            _iconPosition = Position.Left;
            _textAlign    = ContentAlignment.MiddleCenter;

            pbBox             = new PictureBox();
            pbBox.MouseEnter += MouseEnterAct;
            pbBox.MouseLeave += MouseLeaveAct;
            pbBox.MouseDown  += MouseDownAct;
            pbBox.MouseUp    += MouseUpAct;
            pbBox.MouseClick += MouseClickAct;
            radius            = 5;
            roundedRect       = new RoundedBorder(Width, Height, radius);

            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer |
                     ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor |
                     ControlStyles.UserPaint, true);
            BackColor = Color.Transparent;
            ForeColor = Color.Black;
            Font      = new Font("Verdana", 10, FontStyle.Bold);
            state     = MouseState.Leave;
        }
Exemplo n.º 2
0
        /*public Color BackColor
         * {
         *  get { return panelBox.BackColor; }
         *  set { panelBox.BackColor = value;Invalidate(); }
         * }*/

        protected override void OnPaint(PaintEventArgs e)
        {
            int yP = 0, xT = _radius, yT = 0;

            if (_textAlignment.ToString().Contains("Top"))
            {
                yP = title_lbl.Height + _textMargin.Bottom;
            }

            if (_textAlignment.ToString().Contains("Mid"))
            {
                yP = title_lbl.Height / 2 + _textMargin.Bottom;
            }

            if (_textAlignment.ToString().Contains("Bottom"))
            {
                yT = 2 + _textMargin.Top;
            }

            if (_textAlignment.ToString().Contains("Cent"))
            {
                xT = (Width / 2) - (title_lbl.Width / 2);
            }

            if (_textAlignment.ToString().Contains("Right"))
            {
                xT = Width - title_lbl.Width - _radius;
            }
            title_lbl.Location = new Point(xT, yT);

            //Helper.DrawBorder(e,(Control)sender,Color.Red,2,ButtonBorderStyle.Dashed);
            GraphicsPath shape     = new RoundedBorder(Width, Height, _radius, 0, yP).Path;
            GraphicsPath innerRect = new RoundedBorder(Width - 0.5f, Height - 0.5f, _radius, 0.5f, yP + 0.5f).Path;

            //resizeTextBox();

            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            Bitmap   bmp = new Bitmap(Width, Height);
            Graphics grp = Graphics.FromImage(bmp);

            float[] dashValues = { 5, 2, 15, 4 };
            Pen     pen        = new Pen(_borderColor, 3);

            pen.DashStyle = _BorderType;
            //pen.DashPattern = dashValues;
            e.Graphics.DrawPath(pen, shape);
            using (SolidBrush brush = new SolidBrush(_bgColor))
                e.Graphics.FillPath(brush, innerRect);
            //Transparenter.MakeTransparent(panelBox, e.Graphics);

            if (_textAlignment.ToString().Contains("Mid"))
            {
                yP  = title_lbl.Height / 2 + _textMargin.Bottom;
                pen = new Pen(_bgColor, 3);
                e.Graphics.DrawLine(pen, title_lbl.Location.X - 3, yP, title_lbl.Location.X + title_lbl.Width + 3, yP);
            }
//            e.Graphics.DrawArc(pen,10,10,150,150,180,90);
            base.OnPaint(e);
        }
Exemplo n.º 3
0
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            roundedRect = new RoundedBorder(Width, Height, radius);
            e.Graphics.FillRectangle(Brushes.Transparent, ClientRectangle);

            int R1 = (active1.R + inactive1.R) / 2;
            int G1 = (active1.G + inactive1.G) / 2;
            int B1 = (active1.B + inactive1.B) / 2;

            int R2 = (active2.R + inactive2.R) / 2;
            int G2 = (active2.G + inactive2.G) / 2;
            int B2 = (active2.B + inactive2.B) / 2;

            Rectangle rect = new Rectangle(0, 0, Width, Height);

            if (this.Enabled)
            {
                if (state == MouseState.Leave)
                {
                    using (LinearGradientBrush inactiveGB = new LinearGradientBrush(rect, inactive1, inactive2, gradientAngle))
                        e.Graphics.FillPath(inactiveGB, roundedRect.Path);
                }
                else if (state == MouseState.Enter)
                {
                    using (LinearGradientBrush activeGB = new LinearGradientBrush(rect, active1, active2, gradientAngle))
                        e.Graphics.FillPath(activeGB, roundedRect.Path);
                }
                else if (state == MouseState.Down)
                {
                    using (LinearGradientBrush downGB = new LinearGradientBrush(rect, Color.FromArgb(R1, G1, B1), Color.FromArgb(R2, G2, B2), gradientAngle))
                        e.Graphics.FillPath(downGB, roundedRect.Path);
                }
                if (stroke)
                {
                    using (Pen pen = new Pen(strokeColor, 1))
                        using (GraphicsPath path = new RoundedBorder(Width - (radius > 0 ? 0 : 1), Height - (radius > 0 ? 0 : 1), radius).Path)
                            e.Graphics.DrawPath(pen, path);
                }
            }
            else
            {
                Color linear1 = Color.FromArgb(190, 190, 190);
                Color linear2 = Color.FromArgb(210, 210, 210);
                using (LinearGradientBrush inactiveGB = new LinearGradientBrush(rect, linear1, linear2, 90f))
                {
                    e.Graphics.FillPath(inactiveGB, roundedRect.Path);
                    e.Graphics.DrawPath(new Pen(inactiveGB), roundedRect.Path);
                }
            }

            Size newSize = Size;

            //newSize.Width = newSize.Width - Padding.Left - Padding.Right;
            float[] getSize  = NewFontSize(e.Graphics, newSize, Font, Text);
            float   fontSize = getSize[0];
            Font    newFont  = Font;

            if (_autoSizeFont)
            {
                newFont    = new Font(Font.FontFamily, fontSize, Font.Style);
                getSize[1] = measureSizeF(e.Graphics, newFont, Text).Width;
                getSize[2] = measureSizeF(e.Graphics, newFont, Text).Height;
            }

            Rectangle       rrRectangle = ClientRectangle;
            StringAlignment horAlgnmt   = StringAlignment.Center;
            StringAlignment verAlgnmt   = StringAlignment.Center;
            int             selisihH    = (int)((ClientRectangle.Width - getSize[1]) / 2);
            int             selisihV    = (int)((ClientRectangle.Height - getSize[2]) / 2);

            if (_textAlign.ToString().Contains("Left"))
            {
                horAlgnmt = StringAlignment.Near;
            }

            if (_textAlign.ToString().Contains("Right"))
            {
                horAlgnmt = StringAlignment.Far;
            }
            if (_textAlign.ToString().Contains("Top"))
            {
                verAlgnmt = StringAlignment.Near;
            }

            if (_textAlign.ToString().Contains("Bottom"))
            {
                verAlgnmt = StringAlignment.Far;
            }
            if (_Picon != null)
            {
                int locX = ClientRectangle.Width / 2 - _Picon.Width / 2;
                int locY = ClientRectangle.Height / 2 - _Picon.Height / 2;
                if (_iconPosition == Position.FollowLeft && _textAlign.ToString().Contains("Center"))
                {
                    locX = (int)(ClientRectangle.Width - getSize[1]) / 2 - _Picon.Width;
                    locX = locX > Padding.Left + 5 ? locX : Padding.Left + 5;
                }
                else if (_iconPosition == Position.FollowLeft && _textAlign.ToString().Contains("Right"))
                {
                    locX = (int)(ClientRectangle.Width - getSize[1] - _Picon.Width);
                    locX = locX > Padding.Left + 5 ? locX : Padding.Left + 5;
                }
                else if (_iconPosition == Position.FollowRight && _textAlign.ToString().Contains("Center"))
                {
                    locX = (int)((ClientRectangle.Width + getSize[1]) / 2);
                    int tempX = ClientRectangle.Width - _Picon.Width - 5 - Padding.Right;
                    locX = locX > tempX ? tempX:locX;
                }
                else if (_iconPosition == Position.FollowRight && _textAlign.ToString().Contains("Left"))
                {
                    locX = (int)(getSize[1] + 5);
                    int tempX = ClientRectangle.Width - _Picon.Width - 5 - Padding.Right;
                    locX = locX > tempX ? tempX : locX;
                }
                else if (_iconPosition == Position.FollowTop && _textAlign.ToString().Contains("Middle"))
                {
                    locY = (int)((ClientRectangle.Height - getSize[2]) / 2) - _Picon.Height;
                    locY = locY > Padding.Top + 5 ? locY : Padding.Top + 5;
                }
                else if (_iconPosition == Position.FollowTop && _textAlign.ToString().Contains("Bottom"))
                {
                    locY = (int)(ClientRectangle.Height - getSize[2] - _Picon.Height);
                    int tempY = ClientRectangle.Width - _Picon.Width - 5 - Padding.Right;
                    locY = locY > Padding.Top + 5 ? locY : Padding.Top + 5;
                }
                else if (_iconPosition == Position.FollowBottom && _textAlign.ToString().Contains("Middle"))
                {
                    locY = (int)((ClientRectangle.Height + getSize[2]) / 2);
                    int tempY = ClientRectangle.Height - _Picon.Width - 5 - Padding.Right;
                    locY = locY > tempY ? tempY:locY;
                }
                else if (_iconPosition == Position.FollowBottom && _textAlign.ToString().Contains("Top"))
                {
                    locY = (int)(getSize[2] + 5);
                    int tempY = ClientRectangle.Height - _Picon.Width - 5 - Padding.Right;
                    locY = locY > tempY ? tempY : locY;
                }
                else if (_iconPosition.ToString().Contains("Left"))
                {
                    locX = 5 + Padding.Left;
                    if ((ClientRectangle.Width - getSize[1]) / 2 < _Picon.Width + 5 + Padding.Left)
                    {
                        rrRectangle.X = _Picon.Width + Padding.Left + 5 - selisihH;
                    }
                    if (_textAlign.ToString().Contains("Left"))
                    {
                        rrRectangle.X = _Picon.Width + Padding.Left + 5;
                    }
                }
                else if (_iconPosition.ToString().Contains("Right"))
                {
                    locX = ClientRectangle.Width - _Picon.Width - 5 - Padding.Right;
                    if ((ClientRectangle.Width - getSize[1]) / 2 < _Picon.Width + 5 + Padding.Right ||
                        _textAlign.ToString().Contains("Right"))
                    {
                        rrRectangle.X = -_Picon.Width - Padding.Right + 5;
                    }
                }
                else if (_iconPosition.ToString().Contains("Top"))
                {
                    locY = 5 + Padding.Top;
                    if (selisihV < _Picon.Height + 5 + Padding.Top)
                    {
                        rrRectangle.Y = _Picon.Height + Padding.Top - selisihV;
                    }
                    if (_textAlign.ToString().Contains("Top"))
                    {
                        rrRectangle.Y = _Picon.Height + 5 + Padding.Top;
                    }
                }
                else if (_iconPosition.ToString().Contains("Bottom"))
                {
                    locY = ClientRectangle.Height - _Picon.Width - 5 - Padding.Bottom;
                    if (selisihV < _Picon.Width + 5 + Padding.Right ||
                        _textAlign.ToString().Contains("Bottom"))
                    {
                        rrRectangle.Y = -_Picon.Width - Padding.Right + 5;
                    }
                }

                pbBox.Location              = new Point(locX, locY);
                pbBox.Size                  = new Size(_Picon.Width, _Picon.Height);
                pbBox.BackgroundImage       = _Picon;
                pbBox.BackgroundImageLayout = ImageLayout.Stretch;
                Controls.Add(pbBox);
                pbBox.BringToFront();
            }

            using (StringFormat sf = new StringFormat()
            {
                LineAlignment = verAlgnmt,
                Alignment = horAlgnmt
            })
                using (Brush brush = new SolidBrush(ForeColor))
                    e.Graphics.DrawString(Text, newFont, brush, rrRectangle, sf);
            base.OnPaint(e);
        }