如何在C#中使用背景图像在面板上绘制线条

时间:2016-02-21 07:38:31

标签: c#

我有一个带有背景图像的双缓冲面板。现在当我在面板上绘制线条时它很好但是当我在面板区域中移动该线时,它会在它首次定位时拍摄快照或背景。我认为双缓冲面板将解决这个问题,但它不起作用,我也尝试在鼠标按下事件中添加刷新面板代码,但随后排队不断闪烁所以请各位帮助我。

 public void LineMouseDown(object sender, MouseEventArgs e)
{
    var l = (Line)sender;

    if (GetDistance(e.X, e.Y, l.StartPoint.X, l.StartPoint.Y) < 30 ||
        GetDistance(e.X, e.Y, l.EndPoint.X, l.EndPoint.Y) < 30)
    {
        _rotating = true;
    }
    else
    {
        _dragging = true;
       panel5.Refresh();
    }

    _clickX = e.X;
    _clickY = e.Y;

    panel5.Refresh();
    _clickCoords = new Point(l.Left + e.X, l.Top + e.Y);

    UpdateCircle(l);
    UpdateMiddle(l);
}

public void LineMouseUp(object sender, MouseEventArgs e)
{
    _rotating = false;
    _dragging = false;
    panel5.Refresh();
}

public void LineMouseMove(object sender, MouseEventArgs e)
{
    var l = (Line)sender;
    if (_rotating)
    {
        if ((DateTime.Now - lastRotate).TotalMilliseconds > 60)
        {
            try
            {
                var angle = GetAngle(l.Left + e.X, l.Top + e.Y, _centerX, 0, _centerX, _centerY);

                var newStartPoint = new Point(
                    (int)Math.Round(_middleX + _radius * Math.Sin(angle)),
                    (int)Math.Round(_middleY + _radius * Math.Cos(angle))
                );

                var newEndPoint = new Point(2 * _middleX - newStartPoint.X, 2 * _middleY - newStartPoint.Y);

                l.SetPoints(newStartPoint, newEndPoint);

                UpdateMiddle(l);

                lastRotate = DateTime.Now;
            }
            catch
            {
                //ignored
            }
        }
    }
    else if (_dragging)
    {
        l.Left = l.Left + e.X - _clickX;
        l.Top = l.Top + e.Y - _clickY;
    }
}

Here what it looks like when I move line

 public class DoubleBufferPanel : Panel
{


    public DoubleBufferPanel()
    {

        // Set the value of the double-buffering style bits to true.
        this.DoubleBuffered = true;



        this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint |
         ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw , true);


        this.UpdateStyles();


    }


}

0 个答案:

没有答案