Silverlight学习-创建一个信息系统中趋势曲线图库(二)

Silverlight学习-创建一个信息系统中趋势曲线图库(二),第1张

概述        上一篇中介绍了动态添加Silverlight绘图元素,由于这些绘图元素的使用并不简洁,接着就要构造基本图形库了。         工程应用中的图形库,基本有两种构建方式:一种是类似Win Form中的Graphics类,绘制图形简洁,但这种图形不是以“元素”的形式出现,绘制之后图形元素不能被索引,它自身没有属性或事件的概念,因此这种方式适用于“一次绘制好后,只供用户观看”的场合,例         上一篇中介绍了动态添加Silverlight绘图元素,由于这些绘图元素的使用并不简洁,接着就要构造基本图形库了。
        工程应用中的图形库,基本有两种构建方式:一种是类似Win Form中的Graphics类,绘制图形简洁,但这种图形不是以“元素”的形式出现,绘制之后图形元素不能被索引,它自身没有属性或事件的概念,因此这种方式适用于“一次绘制好后,只供用户观看”的场合,例如背景图的绘制、示意曲线的绘制等;另一种是类似Silverlight中的图形绘制方式,各图形元素是一个真正的类,自身有属性或事件,可以被索引,相比第一种情况,这是一种“重量级”的绘图方式,适用于“随时捕捉图形,并对其施加 *** 作”的场合,例如绘制工作流图等。本文称第一种库为图形绘制库,称第二种库为图形元素库。本文只关注图形绘制库的建立。
       本文还有一个目的,尽量消除在WinForm和Silverlight中绘制图形的不同,同一套代码,几乎不用修改就能在WinForm和Silverlight中绘制出相同的图形。由于Silverlight应用中不能随便引用其他C#类库,所以Silverlight应用与其他应用中的通用功能,只能通过代码共用。

        Graphics类是一个“天然”的编制图形绘制库,本文将以它为基础,主要对Silverlight中的绘图元素进行改造。由于Silverlight独立部署的特性,使Silverlight与WinForm的绘图类之间无任何共有部分,但是为达到“同一代码,两处通用”的目的,两者的绘图类都不能使用,必须重新定义一套接口及相关辅助类。下图是本文图形绘制库中所涉及的类。

IGraphBase是一个接口类,它定义了类似Graphics类中的各类图形绘制方法,linePen、PaintBrush等类是图形绘制过程中用到的辅助类,例如linePen包装了Pen类等。WinFormGraphics和SilverlightGraphics实现了IGraphBase接口,分别用于在WinForm和Silverlight中绘制基本图形。

IGraphBase和辅助绘图类放在一个文件中(Helper.cs),以下是代码。

    public class Drawcolor    {        #region Member Variables        private Byte _r,_g,_b,_a;        #endregion Member Variables        #region PropertIEs        public Byte R        {            set { this._r = value; }            get { return this._r; }        }        public Byte G        {            set { this._g = value; }            get { return this._g; }        }        public Byte B        {            set { this._b = value; }            get { return this._b; }        }        public Byte A        {            set { this._a = value; }            get { return this._a; }        }        #endregion PropertIEs        #region Public Methods        public Drawcolor(Byte a=255,Byte r =0,Byte g=0,Byte b=0)        {            _r = r;            _g = g;            _b = b;            _a = a;        }        public Drawcolor(Byte r = 0,Byte g = 0,Byte b = 0)        {            _a = 255;            _g = g;            _b = b;            _r = r;        }        public UInt32 ToArgb()        {            return (((UInt32)_a) << 24) + (((UInt32)_r) << 16) + (((UInt32)_g) << 8) + ((UInt32)_b);         }        public static Drawcolor FromArgb(int red,int green,int blue)        {            return new Drawcolor((Byte)red,(Byte)green,(Byte)blue);                }        public static Drawcolor FromArgb(UInt32  cl)        {           return new Drawcolor((Byte)(cl>>24),(Byte)(cl>>16),(Byte)(cl>>8),(Byte)(cl&0xff));        }        #endregion Public Methods    }    public class Drawcolors    {        public static Drawcolor Black = Drawcolor.FromArgb(0xFF000000);        public static Drawcolor Blue = Drawcolor.FromArgb(0xFF0000FF);        public static Drawcolor brown = Drawcolor.FromArgb(0xFFA52A2A);        public static Drawcolor Cyan = Drawcolor.FromArgb(0xFF00FFFF);        public static Drawcolor DarkGray = Drawcolor.FromArgb(0xFFA9A9A9);        public static Drawcolor Gray = Drawcolor.FromArgb(0xFF808080);        public static Drawcolor Green = Drawcolor.FromArgb(0xFF008000);        public static Drawcolor lightGray = Drawcolor.FromArgb(0xFFD3D3D3);        public static Drawcolor magenta = Drawcolor.FromArgb(0xFFFF00FF);        public static Drawcolor Orange = Drawcolor.FromArgb(0xFFFFA500);        public static Drawcolor Purple = Drawcolor.FromArgb(0xFF800080);        public static Drawcolor Red = Drawcolor.FromArgb(0xFFFF0000);        public static Drawcolor transparent = Drawcolor.FromArgb(0x00FFFFFF);        public static Drawcolor White = Drawcolor.FromArgb(0xFFFFFFFF);        public static Drawcolor Yellow = Drawcolor.FromArgb(0xFFFFFF00);      }    public class linePen    {        public enum StyleEnum        {            SolID,DOT,DASH,DASHDOT,DASHDOTDOT,CUSTOM        }        #region Member Variables        private Single[] _customDashGapArray = null;        #endregion Member Variables                #region PropertIEs        public StyleEnum Style { get; set; }        public Drawcolor color { get; set; }        public Single WIDth { get; set; }        public Single[] CustomDashGapArray        {            get            {                return _customDashGapArray;            }            set             {                if (value != null)                {                    _customDashGapArray = new Single[value.Length];                    value.copyTo(_customDashGapArray,0);                }            }                }        #endregion PropertIEs        public linePen(Drawcolor color,Single wIDth = 1.0f,StyleEnum style = linePen.StyleEnum.soLID,Single[] customDashGap = null)        {            color = color;            WIDth = wIDth;            Style = style;            CustomDashGapArray = customDashGap;        }        public linePen Clone()        {            return new linePen(this.color,this.WIDth,this.Style,this._customDashGapArray);        }    }    public abstract class PaintBrush    {        #region Member Variables        #endregion Member Variables        #region PropertIEs        #endregion PropertIEs        public PaintBrush()        {        }    }    public class SolIDPaintBrush: PaintBrush     {        #region PropertIEs        public Drawcolor color { get; set; }        #endregion PropertIEs        public SolIDPaintBrush(Drawcolor color)        {            color = color;        }    }    public class TextFont    {        public enum StyleEnum        {            REGulAR,BolD,ITAliC        }        public enum AlignmentEnum        {            left,RIGHT,CENTER,JUSTIFY        }        #region Member Variables        private string _family;        #endregion Member Variables        #region PropertIEs        public bool Clip { get; set; }        public bool Wrap { get; set; }        public float Size {get; set; }        public StyleEnum Style { get; set; }        public AlignmentEnum Alignment { get; set; }        public string Family        {            set { this._family = value; }            get { return this._family; }        }        #endregion PropertIEs        #region Public Methods        public TextFont(string family="宋体",float size=10.0f,TextFont.StyleEnum style = StyleEnum.REGulAR,TextFont.AlignmentEnum alignment = AlignmentEnum.left,bool wrap= false,bool clip =false)        {            _family = family;            Size = size;            Style = style;            Alignment = alignment;            Wrap = wrap;            Clip = clip;         }        public TextFont(float size)        {            _family = "宋体";            Size = size;            Style = StyleEnum.REGulAR;            Alignment = AlignmentEnum.left;            Wrap = false;            Clip = false;         }        #endregion Public Methods    }    public class Dot    {       public Int32 X=0,Y=0;       public Dot(Int32 x,Int32 y)       {           this.X = x;           this.Y = y;       }    }    public class DotF    {       public Single X=0.0f,Y=0.0f;       public DotF(Single x,Single y)       {           this.X = x;           this.Y = y;       }    }    public class Area    {        public Int32 X1,Y1,X2,Y2;        public Area()        {            X1 = Y1 = X2 = Y2 = 0;        }        public Area(Int32 x1,Int32 y1,Int32 x2,Int32 y2)        {            this.X1 = x1;            this.Y1 = y1;            this.X2 = x2;            this.Y2 = y2;        }        public Int32 WIDth        {            get { return X2 - X1; }            set { X2 = X1 + value; }        }        public Int32 Height        {            get { return Y2 - Y1; }            set { Y2 = Y1 + value; }        }        public voID reset()        {            int s;            if (X2 < X1)            {                s = X2; X2 = X1; X1 = s;            }            if (Y2 < Y1)            {                s = Y2; Y2 = Y1; Y1 = s;            }        }        public static Area Fromrange(Int32 x,Int32  y,Int32 wIDth,Int32 height)        {            return new Area(x,y,x + wIDth,y + height);        }    }    public class AreaF    {        public Single X1,Y2;        public AreaF()        {            X1 = Y1 = X2 = Y2 = 0.0f;        }        public AreaF(Single x1,Single y1,Single x2,Single y2)        {            this.X1 = x1;            this.Y1 = y1;            this.X2 = x2;            this.Y2 = y2;        }        public Single WIDth        {            get { return X2 - X1; }            set { X2 = X1 + value; }        }        public Single Height        {            get { return Y2 - Y1; }            set { Y2 = Y1 + value; }                }        public voID reset()        {            Single s;            if (X2 < X1)             {                s = X2; X2 = X1; X1 = s;            }            if (Y2 < Y1)            {                s = Y2; Y2 = Y1; Y1 = s;                        }        }        public static AreaF Fromrange(Single x,Single y,Single wIDth,Single height)        {            return new AreaF(x,y + height);        }    }    public enum ImageStretch    {        NONE,FILL,UNIFORM,UNIFORMTOFILL    }        public interface IGraphBase    {        voID Clear(Drawcolor cl);        voID SetClip(AreaF area );        voID ClearClip();        voID SetlinePen(linePen pen);        voID Drawline(linePen pen,Int32 x1,Int32 y2);        voID Drawline(linePen pen,Single x1,Single y2);        voID Startpolyline(linePen pen);        voID AddpolylinePoint(Single x,Single y);        voID AddpolylinePoint(Int32 x,Int32 y);        voID Endpolyline();        voID DrawRectangle(linePen pen,PaintBrush brush,Area area);        voID DrawRectangle(linePen pen,AreaF area);        voID DrawRectangle(linePen pen,Int32 y2);        voID DrawRectangle(linePen pen,Single y2);        voID Drawpolygon(linePen pen,ICollection<Dot> edges);        voID Drawpolygon(linePen pen,ICollection<DotF> edges);        voID DrawCircle(linePen pen,Int32 x,Int32 y,Int32 radius);        voID DrawCircle(linePen pen,Single x,Single radius);        voID DrawEllipse(linePen pen,Area area);        voID DrawEllipse(linePen pen,AreaF area);        voID DrawEllipse(linePen pen,Int32 y2);        voID DrawEllipse(linePen pen,Single y2);        voID DrawString(Single x,string text,Drawcolor Fontcolor,float Fontsize,string Fontname);        voID DrawString(string text,TextFont Font,AreaF area);        voID DrawImage(Area area,string sourceUrl,ImageStretch st);        voID DrawImage(AreaF area,ImageStretch st);    }}
         SilverlightGraphics的源代码。

  public class SilverlightGraphics : IGraphBase    {        #region Consts & Enum        #endregion Consts & Enum        #region Member Variables        private Canvas _slCanvas;        private UInt32 _elementCount = 0;        private linePen _defaultPen = new linePen(Drawcolors.Black);        private linePen _polylinePen = null;        private PointCollection _polylinePoints = null;        private DoubleCollection _dotlinestrokeArray = new DoubleCollection();        private DoubleCollection _dashlinestrokeArray = new DoubleCollection();         private DoubleCollection _dashdotlinestrokeArray = new DoubleCollection();        private DoubleCollection _dashdotdotlinestrokeArray = new DoubleCollection();        private Brush _defaultTextBrush = new SolIDcolorBrush(colors.Black);        private System.windows.FrameworkElement _lastAddedShape = null;        #endregion Member Variables        #region PropertIEs        public Canvas GraphObject        {            get { return _slCanvas; }        }        public System.windows.FrameworkElement LastAddedShape        {            get { return _lastAddedShape; }        }        #endregion PropertIEs        #region Pulbic Methods        public SilverlightGraphics(Canvas cs)        {            _slCanvas = cs;            _dotlinestrokeArray.Add(1.0);        _dotlinestrokeArray.Add(1.0);            _dashlinestrokeArray.Add(3.0);       _dashlinestrokeArray.Add(1.0);            _dashdotlinestrokeArray.Add(3.0);   _dashdotlinestrokeArray.Add(1.0);     _dashdotlinestrokeArray.Add(1.0); _dashdotlinestrokeArray.Add(1.0);            _dashdotdotlinestrokeArray.Add(3.0); _dashdotdotlinestrokeArray.Add(1.0); _dashdotdotlinestrokeArray.Add(1.0); _dashdotdotlinestrokeArray.Add(1.0); _dashdotdotlinestrokeArray.Add(1.0); _dashdotdotlinestrokeArray.Add(1.0);        }        public voID SetlinePen(linePen pen)        {            _defaultPen = pen.Clone();        }        public voID Clear(Drawcolor cl)        {            _slCanvas.Children.Clear();            _slCanvas.Background = new SolIDcolorBrush(converttocolor(cl));        }        public voID SetClip(AreaF area = null)        {            Rect rt = (area == null) ? new Rect(0,_slCanvas.WIDth,_slCanvas.Height) : new Rect(area.X1,area.Y1,area.WIDth,area.Height);            _slCanvas.Clip = new RectangleGeometry() { Rect = rt };        }       public voID ClearClip()        {            _slCanvas.Clip = null;        }        public voID Drawline(linePen pen,Int32 y2)        {            Drawline(pen,(Single)x1,(Single)y1,(Single)x2,(Single)y2);          }        public voID Drawline(linePen pen,Single y2)        {            line shp = new System.windows.Shapes.line();            preparelinePen(shp,(pen==null)?_defaultPen.Clone() :pen);            shp.X1 = x1;            shp.Y1 = y1;            shp.X2 = x2;            shp.Y2 = y2;            addShape(shp,"line");        }        public voID Startpolyline(linePen pen)        {             if(_polylinePoints!=null) _polylinePoints.Clear();                           // need new ??????            _polylinePen = (pen == null) ? _defaultPen.Clone() : pen;                }        public voID AddpolylinePoint(Single x,Single y)        {            if (_polylinePoints == null)            {                _polylinePoints = new PointCollection();            }            _polylinePoints.Add(new Point(x,y));        }        public voID AddpolylinePoint(Int32 x,Int32 y)        {            AddpolylinePoint((Single)x,(Single)y);        }        public voID Endpolyline()        {            polyline shp = new System.windows.Shapes.polyline();            preparelinePen(shp,_polylinePen);            shp.Points = _polylinePoints;            addShape(shp,"Pyln");        }                public voID DrawRectangle(linePen pen,Area area)        {             DrawRectangle(pen,brush,(Single)area.X1,(Single)area.Y1,(Single)area.WIDth,(Single)area.Height);               }        public voID DrawRectangle(linePen pen,AreaF area)        {            DrawRectangle(pen,area.X1,area.Height);        }        public voID DrawRectangle(linePen pen,Int32 height)        {            DrawRectangle(pen,(Single)x,(Single)y,(Single)wIDth,(Single)height);        }        public voID DrawRectangle(linePen pen,Single height)        {            Rectangle shp = new System.windows.Shapes.Rectangle();            preparelinePen(shp,(pen == null) ? _defaultPen.Clone() : pen);            prepareFillBrush(shp,brush);            setRectValue(shp,x,wIDth,height);            addShape(shp,"Rect");        }        public voID Drawpolygon(linePen pen,ICollection<Dot> edges)        {            polygon tmpgon = new System.windows.Shapes.polygon();            preparelinePen(tmpgon,(pen == null) ? _defaultPen.Clone() : pen);            prepareFillBrush(tmpgon,brush);            PointCollection points = new PointCollection();            foreach (Dot dt in edges)            {                points.Add(new Point(dt.X,dt.Y));            }            tmpgon.Points = points;            addShape(tmpgon,"Pygn");        }        public voID Drawpolygon(linePen pen,ICollection<DotF> edges)        {            polygon tmpgon = new System.windows.Shapes.polygon();            preparelinePen(tmpgon,brush);            PointCollection points = new PointCollection();            foreach (DotF dot in edges)            {                points.Add(new Point(dot.X,dot.Y));            }            tmpgon.Points = points;            addShape(tmpgon,"Pygn");        }        public voID DrawCircle(linePen pen,Int32 radius)        {             DrawCircle(pen,(Single)radius);        }        public voID  DrawCircle(linePen pen,Single radius)        {            Ellipse shp = new System.windows.Shapes.Ellipse();            preparelinePen(shp,brush);            setRectValue(shp,x - radius,y - radius,radius,radius);            addShape(shp,"Crcl");        }        public voID DrawEllipse(linePen pen,Area area)        {            DrawEllipse(pen,(Single)area.Height);           }        public voID DrawEllipse(linePen pen,AreaF area)        {            DrawEllipse(pen,area.Height);        }        public voID DrawEllipse(linePen pen,Int32 height)         {             DrawEllipse(pen,(Single)height);        }        public voID  DrawEllipse(linePen pen,Single height)        {            Ellipse shp = new System.windows.Shapes.Ellipse();            preparelinePen(shp,height);            addShape(shp,"Elps");        }        public voID DrawString(string text,PaintBrush  brush,AreaF area)       {           System.windows.Controls.TextBlock shp = new TextBlock();           switch (Font.Alignment)           {                case TextFont.AlignmentEnum.left:                   shp.TextAlignment = TextAlignment.left;                   break;               case TextFont.AlignmentEnum.RIGHT:                   shp.TextAlignment = TextAlignment.Right;                   break;               case TextFont.AlignmentEnum.CENTER:                   shp.TextAlignment = TextAlignment.Center;                   break;               case TextFont.AlignmentEnum.JUSTIFY:                   shp.TextAlignment = TextAlignment.Justify;                   break;           }           switch (Font.Style)           {               case TextFont.StyleEnum.REGulAR:                   break;               case TextFont.StyleEnum.BolD:                    shp.FontWeight = FontWeights.Bold;                   break;               case TextFont.StyleEnum.ITAliC:                   shp.FontStyle = FontStyles.Italic;                   break;           }           if (Font.Clip)           {               shp.Clip = new RectangleGeometry() { Rect = new Rect(0,area.Height) };           }           shp.textwrapPing = (Font.Wrap) ? textwrapPing.Wrap : textwrapPing.nowrap;           shp.Foreground = (brush == null) ? _defaultTextBrush : converttoBrush(brush);           shp.Text = text;           setRectValue(shp,area.Height);           shp.FontSize = Font.Size;           shp.FontFamily = new FontFamily(Font.Family);           addShape(shp,"Text");         }        public voID DrawString(Single x,float Fontsize=10.0f,string Fontname = "宋体")        {            AreaF area = new AreaF(){ X1 = x,Y1= y,X2=x+10,Y2= y+10};            TextFont Font = new TextFont(Fontname,Fontsize);            SolIDPaintBrush  brush = new SolIDPaintBrush (Fontcolor);            DrawString(text,Font,area);        }        public voID DrawImage(Area area,ImageStretch st)        {            DrawImage(area.X1,area.Height,sourceUrl,st);        }        public voID DrawImage(AreaF area,st);        }        public voID DrawImage(Single x,Single height,ImageStretch st)        {            Image shp = new System.windows.Controls.Image();            setRectValue(shp,height);            shp.Stretch = converttoStretch(st);            shp.source = new BitmAPImage(new Uri(sourceUrl,UriKind.relativeOrabsolute));            addShape(shp,"Imag");                }         #endregion Pulbic Methods        #region Private Methods        private color converttocolor(Drawcolor cl)        {            color clr= new color();            clr.A = cl.A;            clr.B = cl.B;            clr.G = cl.G;            clr.R = cl.R;            return clr;        }        private Brush converttoBrush(PaintBrush pbr)        {              if (pbr is SolIDPaintBrush)            {                return new SolIDcolorBrush(converttocolor((((SolIDPaintBrush)pbr).color)));                        }             return null;        }        private Stretch converttoStretch(ImageStretch st)        {             switch(st)            {                case ImageStretch.NONE:                    return Stretch.None;                case ImageStretch.FILL:                    return Stretch.Fill;                case ImageStretch.UNIFORM:                    return Stretch.Uniform;                case ImageStretch.UNIFORMTOFILL:                   return Stretch.UniformToFill;            }            return Stretch.None;                }        private voID preparelinePen(System.windows.Shapes.Shape shp,linePen pen)        {            switch (pen.Style)            {                case linePen.StyleEnum.soLID:                    shp.stroke = new SolIDcolorBrush(converttocolor(pen.color));                    break;                case linePen.StyleEnum.DOT:                    shp.stroke = new SolIDcolorBrush(converttocolor(pen.color));                    shp.strokeDashArray = _dotlinestrokeArray;                    break;                case linePen.StyleEnum.DASH:                    shp.stroke = new SolIDcolorBrush(converttocolor(pen.color));                    shp.strokeDashArray = _dashlinestrokeArray;                    break;                case linePen.StyleEnum.DASHDOT:                    shp.stroke = new SolIDcolorBrush(converttocolor(pen.color));                    shp.strokeDashArray = _dashdotdotlinestrokeArray;                    break;                case linePen.StyleEnum.DASHDOTDOT:                    shp.stroke = new SolIDcolorBrush(converttocolor(pen.color));                    shp.strokeDashArray = _dashdotdotlinestrokeArray;                    break;                case linePen.StyleEnum.CUSTOM:                    shp.stroke = new SolIDcolorBrush(converttocolor(pen.color));                    Single[] dashgapArray = pen.CustomDashGapArray;                    DoubleCollection customlinestrokeArray = new DoubleCollection();                    foreach (Single i in dashgapArray)                    {                        customlinestrokeArray.Add(i);                    }                    shp.strokeDashArray = customlinestrokeArray;                    break;            }            shp.strokeThickness = pen.WIDth;        }        private voID prepareFillBrush(System.windows.Shapes.Shape shp,PaintBrush brh)        {            if (brh != null)            {                shp.Fill = converttoBrush(brh);            }                  }        private voID setRectValue(System.windows.FrameworkElement shp,double x,double y,double wIDth,double height)        {            shp.WIDth = wIDth;            shp.Height = height;            shp.SetValue(Canvas.leftProperty,x);            shp.SetValue(Canvas.topProperty,y);        }        private voID addShape(System.windows.FrameworkElement tmpshape,string namePrefix)        {            tmpshape.name = namePrefix + _elementCount.ToString();            _slCanvas.Children.Add(tmpshape);            _elementCount++;            _lastAddedShape = tmpshape;        }        #endregion Private Methods    }

        WinFormGraphics的源代码。

  public class WinFormGraphics:IGraphBase    {        #region Consts & Enum        #endregion Consts & Enum        #region Member Variables        private Graphics _canvas;        private UInt32 _elementCount = 0;        private Pen _defaultPen = new Pen(color.Black);        private Pen _polylinePen = null;        private List<PointF> _polylinePoints = null;        //private Brush _defaultTextBrush = new SolIDcolorBrush(colors.Black);        //private System.windows.FrameworkElement _lastAddedShape = null;        #endregion Member Variables        #region PropertIEs        public Graphics GraphObject        {            get { return _canvas; }        }        //public System.windows.FrameworkElement LastAddedShape        //{        //    get { return _lastAddedShape; }        //}        #endregion PropertIEs        #region Pulbic Methods        public WinFormGraphics(Graphics cs)        {            _canvas = cs;        }        public voID SetlinePen(linePen pen)        {            _defaultPen = (pen==null)?null: converttopen(pen);        }       public voID Clear(Drawcolor cl)        {            _canvas.Clear(converttocolor(cl));        }       public voID SetClip(AreaF area)        {            if(area != null)            {               _canvas.SetClip(new RectangleF(area.X1,area.Height));            }        }        public voID ClearClip()        {            _canvas.resetClip();        }        public voID Drawline(linePen pen,Single y2)        {            Pen pp = converttopen(pen);            _canvas.Drawline(pp,x1,y1,x2,y2);            _elementCount++;        }        public voID Startpolyline(linePen pen)        {             if(_polylinePoints!=null) _polylinePoints.Clear();                           // need new ??????            _polylinePen = (pen == null) ? _defaultPen : converttopen(pen);                }        public voID AddpolylinePoint(Single x,Single y)        {            if (_polylinePoints == null)            {                _polylinePoints = new List<PointF>();            }            _polylinePoints.Add(new PointF(x,(Single)y);        }        public voID Endpolyline()        {            _canvas.Drawlines(_polylinePen,_polylinePoints.ToArray());            _elementCount++;        }        public voID DrawRectangle(linePen pen,Int32 height)        {            if (brush == null)            {                Pen pp = (pen == null) ? _defaultPen : converttopen(pen);                _canvas.DrawRectangle(pp,height);            }            else            {                Brush bb = converttoBrush(brush);                _canvas.FillRectangle(bb,height);            }            _elementCount++;        }        public voID DrawRectangle(linePen pen,Single height)        {            if (brush == null)            {                Pen pp = (pen == null) ? _defaultPen : converttopen(pen);                _canvas.DrawRectangle(pp,height);            }            _elementCount++;        }        public voID Drawpolygon(linePen pen,ICollection<Dot> edges)        {            Point[] points = new Point[edges.Count];            int i=0;            foreach(Dot dot in edges)            {                points[i].X = dot.X;                points[i].Y = dot.Y;                i++;            }            if (brush == null)            {                Pen pp = (pen == null) ? _defaultPen : converttopen(pen);                _canvas.Drawpolygon(pp,points);            }            else            {                Brush bb = converttoBrush(brush);                _canvas.Fillpolygon (bb,points);            }            _elementCount++;        }        public voID Drawpolygon(linePen pen,ICollection<DotF> edges)        {            PointF[] points = new PointF[edges.Count];            int i = 0;            foreach (DotF dot in edges)            {                points[i].X = dot.X;                points[i].Y = dot.Y;                i++;            }            if (brush == null)            {                Pen pp = (pen == null) ? _defaultPen : converttopen(pen);                _canvas.Drawpolygon(pp,points);            }            else            {                Brush bb = converttoBrush(brush);                _canvas.Fillpolygon(bb,points);            }            _elementCount++;        }        public voID DrawCircle(linePen pen,Single radius)        {            DrawEllipse(pen,radius);        }        public voID DrawEllipse(linePen pen,Int32 height)         {            if (brush == null)            {                Pen pp = (pen == null) ? _defaultPen : converttopen(pen);                _canvas.DrawEllipse(pp,height);            }            else            {                Brush bb = converttoBrush(brush);                _canvas.FillEllipse(bb,height);            }            _elementCount++;        }        public voID  DrawEllipse(linePen pen,Single height)        {            if (brush == null)            {                Pen pp = (pen == null) ? _defaultPen : converttopen(pen);                _canvas.DrawEllipse(pp,height);            }            _elementCount++;        }        public voID DrawString(string text,AreaF area)       {           StringFormat ft = new StringFormat();           if (!Font.Wrap) ft.FormatFlags = StringFormatFlags.nowrap;           if (!Font.Clip) ft.FormatFlags = ft.FormatFlags|StringFormatFlags.NoClip ;            switch (Font.Alignment)           {                case TextFont.AlignmentEnum.left:                   ft.Alignment = Stringalignment.Near;                   break;               case TextFont.AlignmentEnum.RIGHT:                   ft.Alignment = Stringalignment.Far;                   break;               case TextFont.AlignmentEnum.CENTER:                   ft.Alignment = Stringalignment.Center;                   break;               case TextFont.AlignmentEnum.JUSTIFY:                   ft.Alignment = Stringalignment.Near;                   break;           }           FontStyle ftstyle = FontStyle.Regular;           switch (Font.Style)           {               case TextFont.StyleEnum.REGulAR:                   break;               case TextFont.StyleEnum.BolD:                   ftstyle = FontStyle.Bold;                   break;               case TextFont.StyleEnum.ITAliC:                   ftstyle = FontStyle.Italic;                   break;           }           Font fot = new Font(new FontFamily(Font.Family),Font.Size,ftstyle);           RectangleF rt = new RectangleF(area.X1,area.Height);           if (!Font.Clip)  //it seems that StringFormatFlags.NoClip does not work! here donot use that flag.            {               Sizef stringSize = new Sizef();               if (Font.Wrap)               {                   stringSize = _canvas.MeasureString(text,fot,(int)area.WIDth,ft);               }               else               {                   stringSize = _canvas.MeasureString(text,new PointF(area.X1,area.Y1),ft);               }               rt.WIDth = (area.WIDth > stringSize.WIDth) ? area.WIDth : stringSize.WIDth;               rt.Height= (area.Height>stringSize.Height)?area.Height:stringSize.Height;           }           _canvas.DrawString(text,converttoBrush(brush),rt,ft);            _elementCount++;         }        public voID DrawString(Single x,string Fontname = "宋体")        {            Font fot = new Font(new FontFamily(Fontname),Fontsize,FontStyle.Regular);            _canvas.DrawString(text,new SolIDBrush(converttocolor(Fontcolor)),new PointF(x,y));            _elementCount++;        }        public voID DrawImage(Area area,ImageStretch st)        {            Image newImage = Image.Fromfile(sourceUrl);            switch (st)            {                case ImageStretch.UNIFORMTOFILL:                case ImageStretch.NONE:                    _canvas.DrawImage(newImage,y);                    break;                case ImageStretch.FILL:            //to be @R_68_4041@ to the size of the holder                    _canvas.DrawImage(newImage,new RectangleF(x,height));                    break;                case ImageStretch.UNIFORM:       //to be @R_68_4041@ according to the proportion of the image                    if (height / newImage.Size.Height > wIDth / newImage.Size.WIDth)                    {                        _canvas.DrawImage(newImage,newImage.Size.Height * wIDth / newImage.Size.WIDth));                    }                    else                    {                        _canvas.DrawImage(newImage,newImage.Size.WIDth * height / newImage.Size.Height,height));                                        }                    break;            }            _elementCount++;              }         #endregion Pulbic Methods        #region Private Methods        private color converttocolor(Drawcolor cl)        {            return color.FromArgb(cl.A,cl.R,cl.G,cl.B);        }        private Pen converttopen(linePen pen)        {            Pen pp = new Pen(converttocolor(pen.color),pen.WIDth);            switch (pen.Style)            {                case linePen.StyleEnum.soLID:                    pp.DashStyle = System.Drawing.drawing2d.DashStyle.solID;                    break;                case linePen.StyleEnum.DOT:                    pp.DashStyle = System.Drawing.drawing2d.DashStyle.Dot;                    break;                case linePen.StyleEnum.DASH:                    pp.DashStyle = System.Drawing.drawing2d.DashStyle.Dash;                    break;                case linePen.StyleEnum.DASHDOT:                    pp.DashStyle = System.Drawing.drawing2d.DashStyle.DashDot;                    break;                case linePen.StyleEnum.DASHDOTDOT:                    pp.DashStyle = System.Drawing.drawing2d.DashStyle.DashDotDot;                    break;                case linePen.StyleEnum.CUSTOM:                    pp.DashPattern = pen.CustomDashGapArray;                    break;            }            return pp;        }        private Brush converttoBrush(PaintBrush pbr)        {              if (pbr is SolIDPaintBrush)            {                return new SolIDBrush(converttocolor((((SolIDPaintBrush)pbr).color)));                        }             return null;        }         #endregion Private Methods    }
总结

以上是内存溢出为你收集整理的Silverlight学习-创建一个信息系统趋势曲线图库(二)全部内容,希望文章能够帮你解决Silverlight学习-创建一个信息系统中趋势曲线图库(二)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1077199.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存