2016-06-14 101 views
1

我正在嘗試使用位圖繪製圓形。在保留先前繪製的圓的同時在點擊位置繪製新的圓形位圖

每次我點擊鼠標,我以前繪製的圓被移動到新的位置。

我想要發生的事情是:每當我點擊鼠標,在我點擊的位置創建/繪製一個新的圓圈,並且所有先前繪製的圓圈保持不動。

我用下面的代碼工作:

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; 

    } 
} 
} 
+0

記住該程序不知道你畫的最後一幀。您需要保留最有可能的位置列表,然後在每一幀中繪製它們。 –

+0

這個'scG.Clear'總是清除位圖。刪除它並回報。還請注意,這是有點令人費解,說得溫和.. – TaW

回答

2

你的英語有點混亂。如果我正確理解你的問題,現在唯一被繪製的是點擊的新圈子,並且你還希望所有舊的圈子都堅持下去?在這種情況下,有兩種選擇:

  1. 不要在繪製之前清除位圖。 scG.Clear(SystemColors.Control);將清除您剛繪製的位圖。如果刪除該行並且不清除位圖,則下次單擊時,它將在最後一個位圖的頂部繪製新的橢圓。
  2. 如果你想要一個新的繪圖/位圖,你需要一個你的rectangleObj的列表。每次點擊時,您都會將該點添加到您的rectangleObj集合中。然後在你的繪圖方法中,你將遍歷集合並繪製它們。
+0

謝謝你回答這個問題,你是對的我的英語不是最好的..你得到我需要的..方法之一不會滿足我的需求,方法2是更現實的,我想過它。但因爲我是新來的C#我不知道編碼程序來應用它..例如,我想列出rectangle0bj讓我畫100圈..怎麼可以我這樣做? – user6425922

+0

在你的課堂上,你想要一個矩形容器的成員。列表就是一個簡單的例子。添加:'列表 rectangleList'然後在你的鼠標下來,一旦你得到你的rectangleObj,將它添加到列表:'rectangleList.Add(rectangleObj)'。現在在繪製線程中,您遍歷列表並繪製每個列表。一個簡單的方法'for(int i = 0; i Ryan

+0

謝謝你的有用評論,我用第二種方法解決了這個問題..但是現在我面臨一個新問題,就是代碼運行速度太慢..請看看http://stackoverflow.com/questions/37843054/which-part-of-the-code-make -c-sharp-running-too-slowand-is-it-something-fixab – user6425922

1

我注意到一些事情。首先,在Form1_MouseDown(),你有這樣的:

clickCurrent = PointToClient(Cursor.Position); 
clickPrev = clickCurrent; 

要覆蓋舊的位置(clickPrev)之前,你甚至保存它。如果你想保持這兩個職位,你應該把它們放在一個簡單的結構中,就像一個List。當你得到一個新的點時,只需要Add()就可以了。然後,在您的例程中,遍歷列表中的所有元素並將其全部繪製。

如果你只是想兩個位置 - 而且只有兩個 - 只是交換你的聲明是這樣的:

clickPrev = clickCurrent; 
clickCurrent = PointToClient(Cursor.Position); 

而且你必須分配另一個矩形對象的繪圖,但它將使更多意識到在Draw()例程中照顧這一點。

1

交換下面的語句

clickCurrent = PointToClient(Cursor.Position); 
clickPrev = clickCurrent; 

我想你初始化clickCurrent後你分配到clickCurrentclickPrevious的位置。它需要是另一種方式。

1

請試試這個

Rectangle rectangleObj; 
Bitmap background; 
Graphics scG; 
Pen myPen; 

private void Form1_Load(object sender, EventArgs e) 
{ 
    rectangleObj = new Rectangle(10, 10, 30, 30); 
    background = new Bitmap(Width, Height); 
    scG = Graphics.FromImage(background); 
    myPen = new Pen(Color.Red, 3); 

    BackgroundImage = background; 
} 

private void Form1_MouseDown(object sender, MouseEventArgs e) 
{ 
    var point = PointToClient(Cursor.Position); 

    rectangleObj.X = point.X - rectangleObj.Height/2; 
    rectangleObj.Y = point.Y - rectangleObj.Width/2; 

    scG.DrawEllipse(myPen, rectangleObj); 
    Refresh(); 
} 

OnPaintDraw方法去除。以及clickCurrentclickPrev字段。

當您更改表單大小(例如,最大化它)時,位圖和圖形保持不變,所以您會得到此效果。爲了避免這種情況,你需要添加事件處理

private void Form1_SizeChanged(object sender, EventArgs e) 
{ 
    background = new Bitmap(Width, Height); 
    scG = Graphics.FromImage(background); 
    BackgroundImage = background; 
} 

注意,每次調整窗體時,所有以前繪製的被擦除。如果這是不可取的,則繪製需要不同的方法。讓我知道,我會舉另一個例子。

+0

謝謝你的嘗試,但是這段代碼會給我幾圈可以畫同一時間,並有一個固定的距離..這是有用的,但不幸的是不是我需要做的。 – user6425922

+0

@ user6425922 - 我不理解你。代碼完全符合你的要求。我可以假設你在工作中改變了表格大小,對吧? –

+0

這是什麼得到http://postimg.org/image/tyepklc1z/ – user6425922