2013-02-18 35 views
0

我嘗試使用下面的代碼根據鼠標點(起止點)來繪製一個橢圓...旋轉與油漆事件指定角度橢圓

private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
{ 
    start = new Point(e.X, e.Y); 
    // Point mouseDownLocation = pictureBox1.PointToClient(new Point(e.X, e.Y)) 
    mRect = new Rectangle(e.X, e.Y, 0, 0); 
} 

private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 
{ 
    end = new Point(e.X, e.Y); 

    pictureBox1.Invalidate(); 

    if (listRect.Count <= 4) 
    { 
     listRect.Add(mRect); 
     this.getObjectPopup(e); 
    } 
    else 
    { 
     MessageBox.Show("Maximum 5 selection per Image", "Alert", MessageBoxButtons.OK); 
    } 
} 

和油漆事件寫了下面的代碼

private void pictureBox1_Paint(object sender, PaintEventArgs e) 
{ 
    Color c1 = Color.FromArgb(50, Color.Green); 
    foreach (Rectangle rect in listRect) 
    { 
     using (Pen pen = new Pen(Color.Red, 2)) 
     { 
      float angle = (float)Angle(start, end); 

      Matrix transformMatrix = new Matrix(); 
      transformMatrix.Translate(200.0F, 0.0F); 
      e.Graphics.RotateTransform(angle); 
      // e.Graphics.MultiplyTransform(transformMatrix, MatrixOrder.Append); 
      e.Graphics.DrawEllipse(pen, rect.X, rect.Y, rect.Width, rect.Height); 

      e.Graphics.FillEllipse(new SolidBrush(c1), rect.X, rect.Y, rect.Width, rect.Height); 
      this.WriteText(e,rect); 
     } 
    }   
} 

橢圓已經繪就,但不與鼠標一起起點和終點

回答

1

您是否嘗試設置水平和垂直對齊?

rect.HorizontalAlignment = HorizontalAlignment.Left; 
rect.VerticalAlignment = VerticalAlignment.Top; 

編輯:分別

+0

它是否解決您的問題橢圓設置這些屬性...或仍然有問題? (如果答案是正確的,請標記爲解決方案,謝謝) – copa017 2013-02-25 14:52:33