我如何绘制表格中间的圆圈,如何找到圆心? [英] How do i draw a circle in the middle of a Form and how do i find the circle center?

查看:169
本文介绍了我如何绘制表格中间的圆圈,如何找到圆心?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我要画一个简单的圈形式的中间,然后后来我想画线从圆心出来。我该怎么办呢?

I want to draw a simple circle in the middle of the form and then later on i want to draw lines coming out from the circle center. How can i do it ?

推荐答案

属性 this.Width this.Height 相同 this.Bounds 自称是:

获取或设置的大小和控制的位置,包括其
非客户的元素,上的像素,相对于亲本对照

Gets or sets the size and location of the control including its non-client elements, on pixels, relative to the parent control

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

this means you'd need to adjust for the thickness of the borders and the title bar. Using this.ClientRectangle avoids that whole issue.

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

}

这篇关于我如何绘制表格中间的圆圈,如何找到圆心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆