我正在嘗試使用位圖繪製圓形。在保留先前繪製的圓的同時在點擊位置繪製新的圓形位圖
每次我點擊鼠標,我以前繪製的圓被移動到新的位置。
我想要發生的事情是:每當我點擊鼠標,在我點擊的位置創建/繪製一個新的圓圈,並且所有先前繪製的圓圈保持不動。
我用下面的代碼工作:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace multirectangle
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
}
Bitmap background;
Graphics scG;
Rectangle rectangleObj;
private Point clickCurrent = Point.Empty;
private Point clickPrev = Point.Empty;
private void Form1_Load(object sender, EventArgs e)
{
background = new Bitmap(Width, Height);
rectangleObj = new Rectangle(10, 10, 30, 30);
scG = Graphics.FromImage(background);
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
clickCurrent = PointToClient(Cursor.Position);
clickPrev = clickCurrent;
if (clickPrev == Point.Empty) return;
rectangleObj.X = clickPrev.X - rectangleObj.Height/2;// +radius;
rectangleObj.Y = clickPrev.Y - rectangleObj.Width/2;
Refresh();
}
protected override void OnPaint(PaintEventArgs pe)
{
pe.Graphics.DrawImage(Draw(), 0, 0);
}
public Bitmap Draw()
{
Graphics scG = Graphics.FromImage(background);
Pen myPen = new Pen(System.Drawing.Color.Red, 3);
scG.Clear(SystemColors.Control);
scG.DrawEllipse(myPen, rectangleObj);
return background;
}
}
}
記住該程序不知道你畫的最後一幀。您需要保留最有可能的位置列表,然後在每一幀中繪製它們。 –
這個'scG.Clear'總是清除位圖。刪除它並回報。還請注意,這是有點令人費解,說得溫和.. – TaW