[英]How do i draw a circle in the middle of a Form and how do i find the circle center?
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);
}
}
}
我想在表單中間繪制一個簡單的圓,然后再繪制從圓心出來的線。 我該怎么做 ?
屬性this.Width
和this.Height
是相同的this.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();
}
}
如果我正確理解您的意思,這是您想做的嗎?
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.