Example #1
0
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            Graphics  grfx      = e.Graphics;
            Rectangle rectColor = new Rectangle(e.Bounds.Left, e.Bounds.Top, 2 * e.Bounds.Height, e.Bounds.Height);

            rectColor.Inflate(-1, -1);

            Rectangle rectText = new Rectangle(e.Bounds.Left + 2 * e.Bounds.Height,
                                               e.Bounds.Top,
                                               e.Bounds.Width - 2 * e.Bounds.Height,
                                               e.Bounds.Height);

            if (this.Enabled)
            {
                e.DrawBackground();
            }

            //grfx.DrawRectangle(new Pen(e.ForeColor), rectColor);

            DashStyleEx item           = e.Index >= 0?(DashStyleEx)Items[e.Index] : DashStyleEx.Solid;
            SolidBrush  foreColorBrush = new SolidBrush(e.ForeColor);

            Pen linePen = new Pen(foreColorBrush, (float)Math.Ceiling(0.125 * e.Bounds.Height));

            item.SetPenDash(linePen);
            grfx.DrawLine(linePen,
                          rectColor.Left, 0.5f * (rectColor.Top + rectColor.Bottom),
                          rectColor.Right, 0.5f * (rectColor.Top + rectColor.Bottom));
            grfx.DrawString(item.ToString(), Font, foreColorBrush, rectText);
        }
Example #2
0
        /// <summary>
        /// Equivalence between the types <see cref="DashStyleEx"/> and <see cref="DashStyle"/>.
        /// </summary>
        /// <param name="style">One of the values in the enumeration <see cref="DashStyleEx"/> that represents style of dashed lines.</param>
        /// <returns>
        /// Equivalent value.
        /// </returns>
        public static DashStyle ToDashStyle(this DashStyleEx style)
        {
            DashStyle dashstyle = DashStyle.Solid;

            switch (style)
            {
            case DashStyleEx.Dash:
                dashstyle = DashStyle.Dash;
                break;

            case DashStyleEx.DashDot:
                dashstyle = DashStyle.DashDot;
                break;

            case DashStyleEx.DashDotDot:
                dashstyle = DashStyle.DashDotDot;
                break;

            case DashStyleEx.Dot:
                dashstyle = DashStyle.Dot;
                break;
            }

            return(dashstyle);
        }
Example #3
0
        void SetDataSource(DashStyleEx selected)
        {
            if (_dashStyles == null)
            {
                SetDefaultValues();
            }
            if (!_dashStyles.Contains(selected))
            {
                _dashStyles.Add(selected.Clone());
            }

            this.BeginUpdate();

            Items.Clear();

            foreach (DashStyleEx o in _dashStyles)
            {
                Items.Add(o);
            }

            SelectedItem = selected;

            this.EndUpdate();
        }
Example #4
0
        public static ImageSource GetImage(DashStyleEx val)
        {
            const double height    = 1;
            const double width     = 2;
            const double lineWidth = height / 5;

            DashStyle dashStyle = DashStyles.Solid;

            if (val.IsKnownStyle)
            {
                if (val == DashStyleEx.Solid)
                {
                    dashStyle = DashStyles.Solid;
                }
                else if (val == DashStyleEx.Dash)
                {
                    dashStyle = DashStyles.Dash;
                }
                else if (val == DashStyleEx.Dot)
                {
                    dashStyle = DashStyles.Dot;
                }
                else if (val == DashStyleEx.DashDot)
                {
                    dashStyle = DashStyles.DashDot;
                }
                else if (val == DashStyleEx.DashDotDot)
                {
                    dashStyle = DashStyles.DashDotDot;
                }
                else if (val == DashStyleEx.LongDash)
                {
                    dashStyle = new DashStyle(new double[] { 5, 2 }, 0);
                }
            }
            else if (val.IsCustomStyle)
            {
                var list = new List <double>();
                foreach (var e in val.CustomStyle)
                {
                    list.Add(e);
                }
                dashStyle = new DashStyle(list, 0);
            }

            // draws a transparent outline to fix the borders
            var drawingGroup = new DrawingGroup();

            var geometryDrawing = new GeometryDrawing
            {
                Geometry = new RectangleGeometry(new Rect(0, 0, width, height)),
                Pen      = new Pen(Brushes.Transparent, 0)
            };

            drawingGroup.Children.Add(geometryDrawing);

            geometryDrawing = new GeometryDrawing()
            {
                Geometry = new LineGeometry(new Point(0, height / 2), new Point(width, height / 2))
            };
            geometryDrawing.Pen = new Pen(Brushes.Black, lineWidth)
            {
                DashStyle = dashStyle
            };
            drawingGroup.Children.Add(geometryDrawing);

            var geometryImage = new DrawingImage(drawingGroup);

            // Freeze the DrawingImage for performance benefits.
            geometryImage.Freeze();
            return(geometryImage);
        }
Example #5
0
 public DashStyleComboBox(DashStyleEx selected)
     : this()
 {
     SetDataSource(selected);
 }
Example #6
0
        /// <summary>
        /// Draw a centered line inside a rectangle with a specified color and style.
        /// </summary>
        /// <param name="rect"><see cref="RectangleF"/> structure that represents the source rectangle.</param>
        /// <param name="graphics">A <see cref="Graphics"/> object used to draw.</param>
        /// <param name="color">Line color.</param>
        /// <param name="style">A value of the enumeration <see cref="DashStyleEx"/> that represents the style of the line</param>
        /// <param name="width">Thickness of the line</param>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="graphics"/> is <b>null</b>.</exception>
        public static void DrawCenterLine(this RectangleF rect, Graphics graphics, Color color, DashStyleEx style, int width)
        {
            SentinelHelper.ArgumentNull(graphics, nameof(graphics));

            bool isValid = rect.IsValid();

            if (!isValid)
            {
                return;
            }

            float y = rect.Y + rect.Height / 2;

            using (Pen p = new Pen(color, width))
            {
                p.DashStyle = style.ToDashStyle();
                graphics.DrawLine(p, rect.Left + 1, y, rect.Right - 1, y);
            }
        }