刷新图片框会停止需要重绘的其他控件

时间:2015-06-03 16:00:14

标签: c# rotation

所以我有这个方向盘控制,当我用鼠标旋转并刷新图像时,它会阻止我的其他面板控件从更新/重绘中绘制图形。下面是旋转和重新绘制方向盘的代码。

private readonly Bitmap _originalHelmImage;
private Bitmap _newHelmImage;

private void HelmPb_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button != MouseButtons.Left) return;
        PictureBox p = (PictureBox) sender;
        Bitmap bmp = (Bitmap) p.Image;
        //GetPixel works on initial image, not stretched one.
        double ratio = (double) p.Image.Width/p.Width;
        //Ignore the non-helm portion of the helm.
        Color trans = bmp.GetPixel(Convert.ToInt32(ratio*e.X), Convert.ToInt32(ratio*e.Y));
        if (trans.A.Equals(0))
        {
            _dontTurn = true;
            return;
        }
        _offsetAngle = OffsetAngle() - _lastAngleTurn;
        _lastAngleTurn = 0;
        _dontTurn = false;
    }

    private double OffsetAngle()
    {
        int helmXMid = HelmPb.PointToScreen(Point.Empty).X + (HelmPb.Width / 2);
        int helmYMid = HelmPb.PointToScreen(Point.Empty).Y + (HelmPb.Height / 2);
        double angle = AngleFromPoints(MousePosition, new Point(helmXMid, helmYMid));
        return angle;
    }

private void HelmPb_MouseMove(object sender, MouseEventArgs e)
    {
        if ((e.Button != MouseButtons.Left) || _dontTurn) return;
        double angle = OffsetAngle();
        float degrees = Convert.ToSingle(angle - _offsetAngle);
        //float diff = ((360 - degrees) - _deltaHelmTurn%360)%360;
        //float diff = (_lastAngleTurn - (degrees - 360)) % 360;
        double diff = 90 - angle;
        _deltaHelmTurn += Convert.ToSingle(diff - _lastAngleTurn);
        if (Math.Abs(_deltaHelmTurn) >= (_maxHelmTurn*360.0))
        {
            _deltaHelmTurn = Convert.ToSingle(_maxHelmTurn*360.0);
            degrees = 0;
        }
        _lastAngleTurn = Convert.ToSingle(degrees);
        _newHelmImage = RotateImage(_originalHelmImage, -degrees);
        HelmPb.Image.Dispose();
        HelmPb.Image = _newHelmImage;
        WaterDepthPlot.Invalidate();
        HelmPb.Refresh();
    }

double AngleFromPoints(Point pt1, Point pt2)
    {
        Point p = new Point(pt1.X - pt2.X, pt1.Y - pt2.Y);
        double alpha;
        if (p.Y == 0) alpha = p.X > 0 ? 0d : 180d;
        else
        {
            double f = 1d * p.X / (Math.Sqrt(p.X * p.X + p.Y * p.Y));
            alpha = Math.Acos(f) * 180d / Math.PI;
            if (p.Y > 0) alpha = 360d - alpha;
        }
        return alpha;
    }

 private Bitmap RotateImage(Bitmap b, float angle)
    {
        //Create a new empty bitmap to hold rotated image.
        Bitmap returnBitmap = new Bitmap(b.Width, b.Height);
        //Make a graphics object from the empty bitmap.
        Graphics g = Graphics.FromImage(returnBitmap);
        //move rotation point to center of image.
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.TranslateTransform(b.Width/2F, b.Height/2F);
        //Rotate.
        g.RotateTransform(angle);
        //Move image back.
        g.TranslateTransform(-b.Width/2F, -b.Height/2F);
        //Draw passed in image onto graphics object.
        g.DrawImage(b, new PointF(0.0F, 0.0F));
        return returnBitmap;
    }

以下是更新图表的代码。当方向盘移动时,它永远不会运行。一旦用户停止移动,它将更新。有没有办法让它不断更新?我尝试将面板的刷新/无效放入mousemove事件中,因此每当刷新轮子时,图形也是如此;没运气。有什么建议吗?

private void WaterDepthPlot_Paint(object sender, PaintEventArgs e)
    {
        Pen pen = new Pen(Color.FromArgb(70, 75, 80));
        int xOffset = WaterDepthPlot.Location.X;
        e.Graphics.DrawLine(pen, new Point(0, 0), new Point(WaterDepthPlot.Width, 0));
        e.Graphics.DrawLine(pen, new Point(0, (WaterDepthPlot.Height/5)),
                            new Point(WaterDepthPlot.Width, (WaterDepthPlot.Height/5)));
        e.Graphics.DrawLine(pen, new Point(0, (2*WaterDepthPlot.Height/5)),
                            new Point(WaterDepthPlot.Width, (2*WaterDepthPlot.Height/5)));
        e.Graphics.DrawLine(pen, new Point(0, (3*WaterDepthPlot.Height/5)),
                            new Point(WaterDepthPlot.Width, (3*WaterDepthPlot.Height/5)));
        e.Graphics.DrawLine(pen, new Point(0, (4*WaterDepthPlot.Height/5)),
                            new Point(WaterDepthPlot.Width, (4*WaterDepthPlot.Height/5)));
        e.Graphics.DrawLine(pen, new Point((ThirdXScaleUnit.Location.X - xOffset), 0),
                            new Point((ThirdXScaleUnit.Location.X - xOffset),
                                      (WaterDepthPlot.Height + WaterDepthPlot.Location.Y)));
        e.Graphics.DrawLine(pen, new Point((SecondXScaleUnit.Location.X - xOffset), 0),
                            new Point((SecondXScaleUnit.Location.X - xOffset),
                                      (WaterDepthPlot.Height + WaterDepthPlot.Location.Y)));
        e.Graphics.DrawLine(pen, new Point((FirstXScaleUnit.Location.X - xOffset), 0),
                            new Point((FirstXScaleUnit.Location.X - xOffset),
                                      (WaterDepthPlot.Height + WaterDepthPlot.Location.Y)));
        pen = new Pen(Color.Firebrick);
        pen.DashStyle = DashStyle.Dash;
        float[] dash = {3, 3};
        pen.DashPattern = dash;
        double diff = (double) WaterDepthPlot.Height/(5*_depthYScale);
        int alertDiff = (int)(_alertDepth * diff);
        e.Graphics.DrawLine(pen, new Point(0, alertDiff), new Point(WaterDepthPlot.Width, alertDiff));
        pen = new Pen(Color.White);
        GraphicsPath gp = new GraphicsPath();
        if (_depthCurve.Count < 2) return;
        gp.AddCurve(_depthCurve.ToArray());
        e.Graphics.DrawPath(pen, gp);
    }

0 个答案:

没有答案