在paint事件上以指定角度旋转椭圆

时间:2013-02-18 10:46:19

标签: c#

我正在尝试使用以下代码根据鼠标点(起点和终点)绘制一个椭圆...

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);
    }
}

并在paint事件上编写了以下代码

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 个答案:

答案 0 :(得分:1)

您是否尝试过设置水平和垂直对齐?

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

编辑:分别在椭圆上设置这些属性