2012-09-10 73 views
2
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); 
     } 
    } 
} 

我想画的形式,中间的简单的圆形,然后后来我想提请从圆心出来线。我该怎么做 ?我如何绘制一个表格中间的圆圈,如何找到圆心?

+0

*('x1','x2')*应该是圆心。 – oldrinb

+0

这似乎是一个家庭作业问题。你有没有试图解决这个问题,如果是的话,你的具体问题是什么? –

+0

@oldrinb,你错了:如果你使用(X1,X2),那么你将每次偏离中心!而是应该构建一个矩形,使用从这里(http://www.codeproject.com/Tips/403031/Extension-methods-for-finding-centers-of-a-rectang)的扩展方法来获得的中心点矩形,然后使用矩形构建椭圆。 – devinbost

回答

3

属性this.Widththis.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(); 
    } 

} 
+0

布拉德多数民众赞成绘制一个圆形或正方形在表格的中间。现在我需要找到如何计算和绘制一个线或一个像素从圆形然后当它的一个正方形,然后当它的矩形中心。 –

+0

我觉得那里有足够的空间让你玩,并看看如何绘制其他线条。使线条从圆形到其边缘的中心去,你将不得不使用一些三角函数这是在'System.Math'库。使用SOH CAH TOA或毕达哥拉斯。任何一个人都会让你的光线到达圆圈的边缘 – Brad

3

如果我正确理解你,你想这么做吗?

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