简体   繁体   English

如何防止面板内容在滚动时被擦除

[英]How to prevent panel content from being erased upon scroll

I have a question regarding how I can prevent some content drawn on a panel control from being erased when a scroll action brings it out of view. 我有一个问题,当滚动动作使它看不见时,如何防止擦除面板控件上绘制的某些内容。

What I am trying to do is create a 2D tile-map editor. 我想做的是创建2D瓷砖地图编辑器。 Whenever a mouse click event happens on the panel, a tile should get drawn onto the panel. 每当面板上发生鼠标单击事件时,都应在面板上绘制一个磁贴。 I have this working fine. 我的工作很好。 But if I place the object on the panel and scroll to one side, and scroll back, the object I had placed is gone. 但是,如果我将对象放在面板上并滚动到一侧,然后向后滚动,我放置的对象就会消失。

I have done some research and I have seen suggestions on implementing the paint event. 我已经进行了一些研究,并且看到了有关实施绘画活动的建议。 The problem is that I do not understand what to implement here. 问题是我不知道在这里实现什么。 I think most of my struggles is coming from not fully understanding the Graphics object. 我认为我的大部分努力都来自于不完全了解Graphics对象。

Here is some of my code: 这是我的一些代码:

     private void canvas_MouseClick(object sender, MouseEventArgs e)
     {
        Graphics g = canvas.CreateGraphics();

        float x1 = CommonUtils.GetClosestXTile(e.X);
        float y1 = CommonUtils.GetClosestYTile(e.Y);

        if (currentTile != null)
        {
            g.DrawImage(currentTile, x1, y1);
            me.AddTile((int)currX, (int)currY, (int)x1, (int)y1, "C:\\DemoAssets\\tileb.png");
        }
        else
        {
            // dont do anything
        }
        g.Dispose();
    }

    private void canvas_Paint(object sender, PaintEventArgs e)
    {
        // update here?
    }

To hold multiple Tiles, you'd need a List to hold each clicked location along with its associated tile: 要保存多个图块,您需要一个列表来保存每个单击的位置及其关联的图块:

    List<Tuple<Image, PointF>> Tiles = new List<Tuple<Image, PointF>>();

    private void canvas_MouseClick(object sender, MouseEventArgs e)
    {
        if (currentTile != null)
        {
            float x1 = CommonUtils.GetClosestXTile(e.X);
            float y1 = CommonUtils.GetClosestYTile(e.Y);
            Tiles.Add(new Tuple<Image, PointF>(currentTile, new PointF(x1, y1)));
            canvas.Refresh();

            me.AddTile((int)currX, (int)currY, (int)x1, (int)y1, "C:\\DemoAssets\\tileb.png");
        }
    }

    private void canvas_Paint(object sender, PaintEventArgs e)
    {
        foreach (Tuple<Image, PointF> tile in Tiles)
        {
            e.Graphics.DrawImage(tile.Item1, tile.Item2);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM