[英]How to prevent panel content from being erased upon scroll
我有一個問題,當滾動動作使它看不見時,如何防止擦除面板控件上繪制的某些內容。
我想做的是創建2D瓷磚地圖編輯器。 每當面板上發生鼠標單擊事件時,都應在面板上繪制一個磁貼。 我的工作很好。 但是,如果我將對象放在面板上並滾動到一側,然后向后滾動,我放置的對象就會消失。
我已經進行了一些研究,並且看到了有關實施繪畫活動的建議。 問題是我不知道在這里實現什么。 我認為我的大部分努力都來自於不完全了解Graphics對象。
這是我的一些代碼:
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?
}
要保存多個圖塊,您需要一個列表來保存每個單擊的位置及其關聯的圖塊:
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.