如何在窗体中心画一个圆形并找到圆心?

3
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test
{
    public partial class Form1 : Form
    {
        int x1, x2, wid = 100;

        public Form1()
        {
            InitializeComponent();

            x1 = this.Width / 2 ;
            x2 = this.Height / 2 ;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {


            e.Graphics.DrawEllipse(Pens.Red, x1,x2, wid, wid);
        }
    }
}

我想在表单中央画一个简单的圆,然后稍后我想从圆心画出线条。我该怎么做?


(x1, x2) 应该是圆心。 - obataku
这似乎是一道作业题。您尝试过解决这个问题吗?如果是,您在实现上遇到了什么具体问题? - JP.
@oldrinb,你错了:如果你使用(x1,x2),那么每次都会偏离中心!相反,你应该构造一个矩形,使用这里的扩展方法(http://www.codeproject.com/Tips/403031/Extension-methods-for-finding-centers-of-a-rectang)来获取矩形的中心点,然后使用矩形来构造椭圆。 - devinbost
2个回答

5

属性this.Widththis.Heightthis.Bounds相同,后者的描述如下:

以像素为单位获取或设置控件(包括其非客户区元素)相对于父控件的大小和位置。

这意味着您需要调整边框和标题栏的厚度。使用this.ClientRectangle可以避免整个问题。

public partial class Form1 : Form
{
    int circleDiameter  = 100;

    public Form1()
    {
        InitializeComponent();
    }

     private void Form1_Paint(object sender, PaintEventArgs e)
    {

        Point CenterPoint = new Point()
        {
            X = this.ClientRectangle.Width/2,
            Y = this.ClientRectangle.Height/2
        };
        Point topLeft = new Point()
        {
            X=(this.ClientRectangle.Width - circleDiameter) / 2,
            Y=(this.ClientRectangle.Height - circleDiameter) / 2
        };
        Point topRight = new Point()
        {
            X=(this.ClientRectangle.Width + circleDiameter) / 2,
            Y=(this.ClientRectangle.Height - circleDiameter) / 2
        };
        Point bottomLeft = new Point()
        {
            X=(this.ClientRectangle.Width - circleDiameter) / 2,
            Y=(this.ClientRectangle.Height + circleDiameter) / 2
        };
        Point bottomRight = new Point()
        {
            X=(this.ClientRectangle.Width + circleDiameter) / 2,
            Y=(this.ClientRectangle.Height + circleDiameter) / 2
        };

         e.Graphics.DrawRectangle(Pens.Red, topLeft.X, topLeft.Y, circleDiameter, circleDiameter);
         e.Graphics.DrawLine(Pens.Red, CenterPoint, topLeft);
         e.Graphics.DrawLine(Pens.Red, CenterPoint, topRight);
         e.Graphics.DrawLine(Pens.Red, CenterPoint, bottomLeft);
         e.Graphics.DrawLine(Pens.Red, CenterPoint, bottomRight);
    }

    private void Form1_Resize(object sender, EventArgs e)
    {
        this.Invalidate();
    }

}

Brad,在窗体中间画圆或正方形很棒。现在我需要找到如何计算并绘制从圆形、正方形和矩形中心开始的线条或像素。 - Daniel Lip
我认为这里有足够的内容供您使用,以便了解如何绘制其他线条。要使线条从圆的中心到其边缘,您将需要使用一些三角函数,这些函数在System.Math库中。可以使用SOH CAH TOA或勾股定理。任何一个都可以让你的射线到达圆的边缘。 - Brad

4
如果我理解正确,您想要做的是什么?
        const int circleRadius = 150;
        const int circleDiameter = circleRadius * 2;

        const double angleOfLineInDegrees = 65;
        const double angleOfLineInRadians = (angleOfLineInDegrees / 180) * Math.PI;

        // Center of the form
        var cirleCenter = new PointF(((float)Width / 2), ((float)Height / 2));

        // Ellipses draw like a rectangle. 
        // So just start at the center 
        // subtract by the radius along x and y axis
        // make the width and height the diameter.
        e.Graphics.DrawEllipse(Pens.Black, cirleCenter.X - circleRadius, cirleCenter.Y - circleRadius, circleDiameter, circleDiameter);

        // This line is using trig to convert the angle into a usable vector
        var lineVector = new PointF((float)Math.Cos(angleOfLineInRadians) * circleRadius, (float)Math.Sin(angleOfLineInRadians) * circleRadius);

        var lineEndPoint = new PointF(cirleCenter.X + lineVector.X, cirleCenter.Y + lineVector.Y);

        // Draw the line starting at 
        e.Graphics.DrawLine(Pens.Blue, cirleCenter, lineEndPoint);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接