コード例 #1
0
        /// <summary>
        /// Set position of button inside its rectangle to insure that divisions will fit.
        /// </summary>
        private void SetDimensions()
        {
            Font font;

            // Rectangle
            float x, y, w, h;

            x = 0;
            y = 0;
            w = h = Width;

            // Calculate ratio
            _drawRatio = w / 150;
            if (_drawRatio == 0.0)
            {
                _drawRatio = 1;
            }


            if (_showLargeScale)
            {
                Graphics Gr        = this.CreateGraphics();
                string   strvalmax = _maximum.ToString();
                string   strvalmin = _minimum.ToString();
                string   strval    = strvalmax.Length > strvalmin.Length ? strvalmax : strvalmin;
                double   val       = Convert.ToDouble(strval);

                //double val = _maximum;
                String str = String.Format("{0,0:D}", (int)val);

                float fSize = _scaleTypeface.Size;

                if (_scaleTypefaceAutoSize)
                {
                    fSize = (float)(6F * _drawRatio);
                    if (fSize < 6)
                    {
                        fSize = 6;
                    }
                    font = new Font(_scaleTypeface.FontFamily, fSize);
                }
                else
                {
                    fSize = _scaleTypeface.Size;
                    font  = new Font(_scaleTypeface.FontFamily, _scaleTypeface.Size);
                }

                SizeF strsize = Gr.MeasureString(str, font);

                // Graduations outside
                _gradLength = 4 * _drawRatio;

                if (_drawDivInside)
                {
                    // Graduations inside : remove only 2*8 pixels
                    //x = y = 8;
                    x = y = _gradLength;
                    w = Width - 2 * x;
                }
                else
                {
                    // remove 2 * size of text and length of graduation
                    //_gradLength = 4 * _drawRatio;
                    int strw = (int)strsize.Width;
                    int strh = (int)strsize.Height;

                    int max = Math.Max(strw, strh);
                    x = max;
                    y = max;
                    w = (int)(Width - 2 * max - _gradLength);
                }

                if (w <= 0)
                {
                    w = 1;
                }
                h = w;

                // Rectangle of the rounded knob
                this._rKnob = new Rectangle((int)x, (int)y, (int)w, (int)h);

                Gr.Dispose();
            }
            else
            {
                this._rKnob = new Rectangle(0, 0, Width, Height);
            }


            // Center of knob
            this._pKnob = new Point(_rKnob.X + _rKnob.Width / 2, _rKnob.Y + _rKnob.Height / 2);

            // create offscreen image
            this._offScreenImage = new Bitmap(this.Width, this.Height);
            // create offscreen graphics
            this._gOffScreen = Graphics.FromImage(_offScreenImage);


            // Depends on retangle dimensions
            // create LinearGradientBrush for creating knob
            _brushKnob = new LinearGradientBrush(
                _rKnob, KryptonKnobUtilities.GetLightColour(_knobBackColour, 55), KryptonKnobUtilities.GetDarkColour(_knobBackColour, 55), LinearGradientMode.ForwardDiagonal);

            // create LinearGradientBrush for knobPointer
            _brushKnobPointer = new LinearGradientBrush(
                _rKnob, KryptonKnobUtilities.GetLightColour(_pointerColour, 55), KryptonKnobUtilities.GetDarkColour(_pointerColour, 55), LinearGradientMode.ForwardDiagonal);
        }
コード例 #2
0
        /// <summary>
        /// Draw the pointer of the knob (a small button inside the main button)
        /// </summary>
        /// <param name="Gr"></param>
        private void DrawPointer(Graphics Gr)
        {
            try
            {
                float radius = (float)(_rKnob.Width / 2);

                // Draw a line
                if (_pointerStyle == KnobPointerStyles.LINE)
                {
                    int     l  = (int)radius / 2;
                    int     w  = l / 4;
                    Point[] pt = GetKnobLine(Gr, l);

                    Gr.DrawLine(new Pen(_pointerColour, w), pt[0], pt[1]);
                }
                else
                {
                    // Draw a circle
                    int w = 0;
                    int h = 0;
                    int l = 0;

                    string strvalmax = _maximum.ToString();
                    string strvalmin = _minimum.ToString();
                    string strval    = strvalmax.Length > strvalmin.Length ? strvalmax : strvalmin;
                    double val       = Convert.ToDouble(strval);
                    String str       = String.Format("{0,0:D}", (int)val);

                    float fSize;
                    SizeF strsize;
                    if (_scaleTypefaceAutoSize)
                    {
                        // Use font family = _scaleTypeface, but size = automatic
                        fSize = (float)(6F * _drawRatio);
                        if (fSize < 6)
                        {
                            fSize = 6;
                        }
                        strsize = Gr.MeasureString(str, new Font(_scaleTypeface.FontFamily, fSize));
                    }
                    else
                    {
                        // Use font family = _scaleTypeface, but size = fixed
                        fSize   = _scaleTypeface.Size;
                        strsize = Gr.MeasureString(str, _scaleTypeface);
                    }

                    int strw = (int)strsize.Width;
                    int strh = (int)strsize.Height;
                    w = Math.Max(strw, strh);
                    // radius of small circle
                    l = (int)radius - w / 2;

                    h = w;

                    Point Arrow = this.GetKnobPosition(l - 2); // Remove 2 pixels to offset the small circle inside the knob

                    // Draw pointer arrow that shows knob position
                    Rectangle rPointer = new Rectangle(Arrow.X - w / 2, Arrow.Y - w / 2, w, h);


                    //KryptonKnobUtilities.DrawInsetCircle(ref Gr, rPointer, new Pen(_pointerColour));
                    KryptonKnobUtilities.DrawInsetCircle(ref Gr, rPointer, new Pen(KryptonKnobUtilities.GetLightColour(_pointerColour, 55)));

                    Gr.FillEllipse(_brushKnobPointer, rPointer);
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
        }