取り消しおよびやり直し機能を備えた単純な描画アプリケーションを作成しようとしています。私はあなたがリストに描いているものを追加し、リストを呼び出してすべてを描くことができると仮定します。次に元に戻すと、最後に追加されたアイテムが削除され、すべてのアイテムが再び表示されます。問題は、リストに描いたものを追加してそのリストを元に戻すにはどうすればいいですか?複数のフリーハンドポリラインまたは曲線描画を描画する - 取り消し機能を追加する
私はビットマップの再描画メソッドを使用しています。 これは私が描く方法です:
Point start, end;
bool painting;
private List<PointF> myPoints = new List<PointF>();
private void pnlMain_MouseDown(object sender, MouseEventArgs e)
{
start = e.Location;
painting = true;
}
private void pnlMain_MouseUp(object sender, MouseEventArgs e)
{
painting = false;
}
private void pnlMain_MouseMove(object sender, MouseEventArgs e)
{
if (painting == true)
{
end = e.Location;
g.DrawLine(p, start, end);
myPoints.Add(e.Location);
pnlMain.Refresh();
start = end;
}
}
private void btnUndo_Click(object sender, EventArgs e)
{
g.Clear(cldFill.Color);
if (myPoints.Count > 2)
{
myPoints.RemoveAt(myPoints.Count - 1);
g.DrawCurve(p, myPoints.ToArray());
}
pnlMain.Refresh();
//This works but you have to spam it to get rid of
//a line and does some weird connections.
}
描画する座標を 'List'に集める必要があります。収集方法に関するコードについては、私の[コメントはこちら]をご覧ください(http://stackoverflow.com/questions/38280801/smoother-graphics-than-smoothingmode-antialias#38280801)!また、あなたは間違った方法でペイントします。あなたはGraphicsオブジェクトをキャッシュしているようです。これは間違っています!ペイントイベントのすべてを描く! –
TaW
はい、問題ありません。あなたが必要とするのは、あなたが必要とするすべてのコードを実装することです。(私がリンクしたコメントは、(http://stackoverflow.com/questions/38280801/smoother-graphics-than-smoothingmode-antialias#38280801)ボタンをクリックして 'curves.Remove(curves.Last));をクリックします。 pnlMain.Invalidate(); ' – TaW