简体   繁体   English

在绘画事件中以指定角度旋转椭圆

[英]Rotate the ellipse with specified angle on paint event

I am trying to draw an ellipse according to mouse points(starting and ending point) using the following code... 我正在尝试使用以下代码根据鼠标点(起点和终点)绘制一个椭圆...

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

and

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

and on paint event wrote the following code 并在绘画事件上编写了以下代码

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

ellipse has been drawn but not along with mouse starting and ending points 椭圆已绘制,但未与鼠标的起点和终点一起绘制

Have you tried to set horizontal and vertical alignment? 您是否尝试过设置水平和垂直对齐方式?

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

EDIT: respectively setting those properties on the Ellipse 编辑:分别在椭圆上设置这些属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM