Пример #1
0
        /// <summary>
        /// 画指针
        /// </summary>
        /// <param name="Gr"></param>
        /// <param name="rc"></param>
        /// <returns></returns>
        public virtual bool DrawNeedle(Graphics Gr, RectangleF rc)
        {
            if (this.AnalogMeter == null)
            {
                return(false);
            }

            float w, h;

            w = rc.Width;
            h = rc.Height;

            double minValue   = this.AnalogMeter.MinValue;
            double maxValue   = this.AnalogMeter.MaxValue;
            double currValue  = this.AnalogMeter.Value;
            float  startAngle = this.AnalogMeter.GetStartAngle();
            float  endAngle   = this.AnalogMeter.GetEndAngle();

            float radius = (float)(w / 2 - (w * 0.12));
            float val    = (float)(maxValue - minValue);

            val  = (float)((100 * (currValue - minValue)) / val);
            val  = ((endAngle - startAngle) * val) / 100;
            val += startAngle;

            float angle = HMIMath.GetRadian(val);

            float cx = needleCenter.X;
            float cy = needleCenter.Y;

            PointF ptStart = new PointF(0, 0);
            PointF ptEnd   = new PointF(0, 0);

            GraphicsPath pth1 = new GraphicsPath();

            ptStart.X = cx;
            ptStart.Y = cy;
            angle     = HMIMath.GetRadian(val + 10);
            ptEnd.X   = (float)(cx + (w * .09F) * Math.Cos(angle));
            ptEnd.Y   = (float)(cy + (w * .09F) * Math.Sin(angle));
            pth1.AddLine(ptStart, ptEnd);

            ptStart = ptEnd;
            angle   = HMIMath.GetRadian(val);
            ptEnd.X = (float)(cx + radius * Math.Cos(angle));
            ptEnd.Y = (float)(cy + radius * Math.Sin(angle));
            pth1.AddLine(ptStart, ptEnd);

            ptStart = ptEnd;
            angle   = HMIMath.GetRadian(val - 10);
            ptEnd.X = (float)(cx + (w * .09F) * Math.Cos(angle));
            ptEnd.Y = (float)(cy + (w * .09F) * Math.Sin(angle));
            pth1.AddLine(ptStart, ptEnd);

            pth1.CloseFigure();
            Color clr = this.AnalogMeter.NeedleColor;

            if (this.AnalogMeter.Value < this.AnalogMeter.MinNValueNor ||
                this.AnalogMeter.Value > this.AnalogMeter.MaxValueNor)
            {
                clr = this.AnalogMeter.UnNormalColor;
            }
            SolidBrush br  = new SolidBrush(clr);
            Pen        pen = new Pen(clr);

            Gr.DrawPath(pen, pth1);
            Gr.FillPath(br, pth1);

            pth1.Dispose();
            br.Dispose();
            pen.Dispose();
            return(true);
        }
Пример #2
0
        /// <summary>
        /// 画刻度
        /// </summary>
        /// <param name="Gr"></param>
        /// <param name="rc"></param>
        /// <returns></returns>
        public virtual bool DrawDivisions(Graphics Gr, RectangleF rc)
        {
            if (this.AnalogMeter == null)
            {
                return(false);
            }

            float  startAngle        = this.AnalogMeter.GetStartAngle();
            float  endAngle          = this.AnalogMeter.GetEndAngle();
            float  scaleDivisions    = this.AnalogMeter.ScaleDivisions;
            float  scaleSubDivisions = this.AnalogMeter.ScaleSubDivisions;
            double minValue          = this.AnalogMeter.MinValue;
            double maxValue          = this.AnalogMeter.MaxValue;
            Color  scaleColor        = this.AnalogMeter.ScaleColor;

            float cx = needleCenter.X;
            float cy = needleCenter.Y;
            float w  = rc.Width;
            float h  = rc.Height;

            float incr         = HMIMath.GetRadian((endAngle - startAngle) / ((scaleDivisions - 1) * (scaleSubDivisions + 1)));
            float currentAngle = HMIMath.GetRadian(startAngle);
            float radius       = (float)(w / 2 - (w * 0.08));//半径
            float rulerValue   = (float)minValue;

            Pen        pen = new Pen(scaleColor, (1 * drawRatio));
            SolidBrush br  = new SolidBrush(scaleColor);

            PointF ptStart = new PointF(0, 0);
            PointF ptEnd   = new PointF(0, 0);
            int    n       = 0;

            for (; n < scaleDivisions; n++)
            {
                //画大刻度
                ptStart.X = (float)(cx + radius * Math.Cos(currentAngle));
                ptStart.Y = (float)(cy + radius * Math.Sin(currentAngle));
                ptEnd.X   = (float)(cx + (radius - w / 20) * Math.Cos(currentAngle));
                ptEnd.Y   = (float)(cy + (radius - w / 20) * Math.Sin(currentAngle));
                Gr.DrawLine(pen, ptStart, ptEnd);

                //画文本
                Font font = new Font(this.AnalogMeter.Font.FontFamily, (float)(6F * drawRatio));

                float  tx  = (float)(cx + (radius - (20 * drawRatio)) * Math.Cos(currentAngle));
                float  ty  = (float)(cy + (radius - (20 * drawRatio)) * Math.Sin(currentAngle));
                double val = Math.Round(rulerValue);
                String str = String.Format("{0,0:D}", (int)val);

                SizeF size = Gr.MeasureString(str, font);
                Gr.DrawString(str,
                              font,
                              br,
                              tx - (float)(size.Width * 0.5),
                              ty - (float)(size.Height * 0.5));

                rulerValue += (float)((maxValue - minValue) / (scaleDivisions - 1));

                if (n == scaleDivisions - 1)
                {
                    font.Dispose();
                    break;
                }

                if (scaleDivisions <= 0)
                {
                    currentAngle += incr;
                }
                else
                {
                    for (int j = 0; j <= scaleSubDivisions; j++)
                    {
                        currentAngle += incr;
                        ptStart.X     = (float)(cx + radius * Math.Cos(currentAngle));
                        ptStart.Y     = (float)(cy + radius * Math.Sin(currentAngle));
                        ptEnd.X       = (float)(cx + (radius - w / 50) * Math.Cos(currentAngle));
                        ptEnd.Y       = (float)(cy + (radius - w / 50) * Math.Sin(currentAngle));
                        Gr.DrawLine(pen, ptStart, ptEnd);
                    }
                }

                font.Dispose();
            }

            pen.Dispose();
            br.Dispose();
            return(true);
        }