示例#1
0
        private void PaintCompass()
        {
            compass?.Dispose();     // ensure we are clean
            compass = null;

            pixelsperdegree = (double)this.Width / (double)WidthDegrees;

            int bitmapwidth  = (int)(360 * pixelsperdegree);       // size of bitmap
            int bitmapheight = Height * CompassHeightPercentage / 100;

            //System.Diagnostics.Debug.WriteLine("Compass width " + this.Width + " deg width " + WidthDegrees + " pix/deg " + pixelsperdegree);

            if (!DesignMode)        // for some reason, FromImage craps it out
            {
                compass = new Bitmap(bitmapwidth, bitmapheight);

                using (Graphics g = Graphics.FromImage(compass))
                {
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;

                    if (!BackColor.IsFullyTransparent())
                    {
                        using (Brush b = new SolidBrush(BackColor))
                            g.FillRectangle(b, new Rectangle(0, 0, compass.Width, compass.Height));

                        textsmoothingmode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    }
                    else
                    {
                        textsmoothingmode = System.Drawing.Drawing2D.SmoothingMode.None;
                    }

                    int yline = 0;

                    SizeF sz = g.MeasureString("360", Font);
                    fontline = bitmapheight - (int)(sz.Height + 1);
                    int bigtickdepth   = bitmapheight * TickHeightPercentage / 100;
                    int smalltickdepth = bigtickdepth / 2;

                    int stmajor = stencilmajortickat;
                    int stminor = stencilminortickat;

                    if (autosetstencilticks)
                    {
                        double minmajorticks = sz.Width / pixelsperdegree;
                        // System.Diagnostics.Debug.WriteLine("Major min ticks at {0} = {1}", sz.Width, minmajorticks);
                        if (minmajorticks >= 40)
                        {
                            stmajor = 80; stminor = 20;
                        }
                        else if (minmajorticks >= 20)
                        {
                            stmajor = 40; stminor = 10;
                        }
                        else if (minmajorticks >= 10)
                        {
                            stmajor = 20; stminor = 5;
                        }
                        else
                        {
                            stmajor = 10; stminor = 2;
                        }
                    }

                    Color sc    = Enabled ? StencilColor : StencilColor.Multiply(0.5F);
                    Pen   p1    = new Pen(sc, 1);
                    Pen   p2    = new Pen(sc, 2);
                    Brush textb = new SolidBrush(Enabled ? this.ForeColor : this.ForeColor.Multiply(0.5F));
                    var   fmt   = ControlHelpersStaticFunc.StringFormatFromContentAlignment(ContentAlignment.MiddleCenter);

                    for (int d = pixelstart; d < 360 + pixelstart; d++)
                    {
                        int x = (int)((d - pixelstart) * pixelsperdegree);

                        bool majortick = d % stmajor == 0;
                        bool minortick = (d % stminor == 0) && !majortick;

                        if (majortick)
                        {
                            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
                            g.DrawLine(p2, new Point(x, yline), new Point(x, yline + bigtickdepth));
                            g.SmoothingMode = textsmoothingmode;
                            g.DrawString(ToVisual(d).ToStringInvariant(), this.Font, textb, new Rectangle(x - 30, fontline, 60, compass.Height - fontline), fmt);

                            //DEBUG g.DrawLine(p1, x - sz.Width / 2, fontline, x + sz.Width / 2, fontline);
                        }

                        if (minortick)
                        {
                            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
                            g.DrawLine(p1, new Point(x, yline), new Point(x, yline + smalltickdepth));
                        }
                    }

                    p1.Dispose();
                    p2.Dispose();
                    textb.Dispose();
                    fmt.Dispose();
                }
            }
            else
            {           // FromImage above crashes the designer..
                emergencydesignlabel          = new Label();
                emergencydesignlabel.Location = new Point(10, 10);
                emergencydesignlabel.AutoSize = true;
                emergencydesignlabel.Text     = "Compass.. FromImage crashes designer";
                this.BackColor = Color.LightBlue;
                Controls.Add(emergencydesignlabel);
            }
        }