2011-11-26 21 views
0

我畫上使用GDI+Form1一些圈,該圓的中心是Custom Control小紅色矩形這是從User Control衍生Form1的也由GDI+用幾種顏色繪製的位圖的BackgroundImage屬性。如何用鼠標在.NET Framework中移動由GDI繪製的圓?

我想要的是,當我用鼠標移動紅色矩形(圓的中心)時,圓形也將跟隨紅色矩形移動。使用MouseDown,MouseMove事件,我可以用鼠標平滑地移動紅色矩形。

我的問題是如何移動對應於紅色矩形的圓圈(圓心)。 我啓用了雙緩衝來解決閃爍問題。 CircleCenterCustom Control類的對象(例如紅色矩形)。 GObject是一個Grahpics對象。 下面是一些關鍵代碼:

public Form1() 
    { 
     InitializeComponent(); 
     this.SetStyle(ControlStyles.DoubleBuffer | //enables double-buffering 
         ControlStyles.UserPaint | 
         ControlStyles.AllPaintingInWmPaint, 
         true); 
    } 

    Point CCenterPoint = new Point(); 
    private int Diameter = 250; 
    private void CircleCenterMouseDown(object sender, MouseEventArgs e) 
    { 
     CCenterPoint = new Point(-e.X, -e.Y); 
    } 

    private void CircleCenterMouseMove(object sender, MouseEventArgs e) 
    { 
     if (e.Button == MouseButtons.Left) 
     { 
       Point MousePos = CircleCenter.MousePosition; 
       MousePos.Offset(CCenterPoint.X, CCenterPoint.Y); 
       CircleCenter.Location = CircleCenter.Parent.PointToClient(MousePos); 
       CircleCenter.BringToFront(); 
       CirclePen.Color = Color.Black; 
       GObject.DrawEllipse(CirclePen, CircleCenter.Left- Diameter/2, CircleCenter.Top - Diameter/2, Diameter, Diameter); 
       this.Invalidate(); 
     } 
    } 

如何消除由GDI+中,用黑色圓圈在MouseMove生產的進行? 我GOOGLE了幾個網站,並沒有得到滿意的答案。希望你能給我一些提示,Thx!

回答

2

那麼,正如我從你的問題所理解的,你只需要圍繞紅色矩形畫一個圓,這很容易。

在窗體的Paint事件,添加以下(假設你的紅色矩形控制的名稱爲「CircleCenter」和你的形式命名爲「Form1的」):

private void Form1_Paint(object sender, PaintEventArgs e) 
{ 
    // get the Graphics object of the form. 
    Graphics g = e.Graphics; 
    // create a think red pen for the circle drawing 
    Pen redPen = new Pen(Brushes.Red, 4.0f); 
    // drawing the circle 
    PointF ctrlCenter = GetCenterOfControl(CircleCenter); 
    g.DrawEllipse(redPen, 
      ctrlCenter.X - (Diameter/2), 
      ctrlCenter.Y - (Diameter/2), 
      Diameter, Diameter); 
} 

//The following little method to calculate the center point 
PointF GetCenterOfControl(Control ctrl) 
{ 
    return new PointF(ctrl.Left + (ctrl.Width/2), 
      ctrl.Top + (ctrl.Height/2)); 
} 

什麼辦法,我知道它看起來像一個圓形繪圖這樣一個簡單的任務很長!這裏是上面代碼的醜陋單行版本:

e.Graphics.DrawEllipse(new Pen(Brushes.Red, 4.0f), (centerCircle.Left + (centerCircle.Width/2)) - (Diameter/2), (centerCircle.Top + (centerCircle.Height/2)) - (Diameter/2), Diameter, Diameter); 
+0

它的工作原理!感謝您的優秀回答! – viperchaos

+0

不客氣,不要忘記標記爲答案。 – 2011-11-26 09:46:03

0

您將需要始終重置整個GObject(擦除您在其上繪製的圖像),然後再重新繪製它們。

這可以通過簡單地繪製矩形來獲得Graphics對象的對象的顏色來完成(儘管您沒有提及它,我認爲GObject是一個Graphics對象,獲得了一些雙贏控件?) 。

因此,像:

Control control = CircleCenter.Parent; // Parent control where you get Graphics object from. 
System.Drawing.SolidBrush sBrush = new System.Drawing.SolidBrush(control.BackColor); // Brush to be used with the same color like the one of the parent control. 
GObject.FilledRectangle(sBrush, new Rectangle(0, 0, control.Width, control.Height); // Erase background. 
GObject.DrawEllipse(CirclePen, CircleCenter.Left- Diameter/2, CircleCenter.Top - Diameter/2, Diameter, Diameter); // Do your stuff. 

應隱藏舊圖紙,並重新繪製在新位置的圓圈。

+0

我認爲重置整個GObject會消耗太多資源並影響窗體的響應時間。 – viperchaos

+0

我假設你將用鼠標移動控件,在這種情況下,以前繪製的_Paint事件將被留下(因爲表單沒有完全重繪,你需要照顧它,這就是爲什麼你需要繪製一個矩形,其大小與先前相同的背景顏色,然後在其上繪製圓形。 –

+0

是的,GObject是一個Graphics對象,從Form1獲得。我找不到'DrawFilledRectangle'.Is it意思是'FillRectangle'? – viperchaos