1

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

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

椭圆已绘制但未与鼠标起点和终点一起绘制

4

1 回答 1

1

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

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

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

于 2013-02-18T11:09:17.717 回答