C#画圆的问题 实现的效果是2步完成画圆~~第一个click确定圆心~第二个click确定半径~在确定半径的时候要图的整个样子要展现出来~~这样的画圆函数应该怎么写啊~?麻烦师兄些指点啊~~在线等~~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 .net内部有自带的画圆函数,按照你的描述,首先确定一个点,然后再确定半径,调用Graphics.DrawEllipse方法你需要一个矩形来确定一个圆实际上就是计算该矩形所在的位置以及边长的问题了,需要注意的是矩形的位置是左上角确定的而圆心是位于矩形的中心 private void button1_Click(object sender, EventArgs e) { Graphics _Graphics = Graphics.FromHwnd(pictureBox1.Handle); _Graphics.DrawEllipse(new Pen(Brushes.Yellow, 2), GetEllopse(new Point(50, 50), 10)); _Graphics.Dispose(); } /// <summary> /// 根据圆心和半径获取图形坐标 /// </summary> /// <param name="p_CenterPoint">圆心</param> /// <param name="p_Radius">半径</param> /// <returns>坐标</returns> public Rectangle GetEllopse(Point p_CenterPoint, int p_Radius) { int _X = p_CenterPoint.X - p_Radius; int _Y = p_CenterPoint.Y - p_Radius; return new Rectangle(_X, _Y, p_Radius * 2, p_Radius * 2); } 你需要的是在MouseMove事件中画圆,等会给你个详细代码,我先试下,以前做过。 你在窗体上加个pictureBox1给pictureBox1加3个事件方法 MouseMove MouseUp MouseDown /// <summary> /// 根据圆心和半径获取图形坐标 /// </summary> /// <param name="p_CenterPoint">圆心</param> /// <param name="p_Radius">半径</param> /// <returns>坐标</returns> public Rectangle GetEllopse(Point p_CenterPoint, int p_Radius) { int _X = p_CenterPoint.X - p_Radius; int _Y = p_CenterPoint.Y - p_Radius; return new Rectangle(_X, _Y, p_Radius * 2, p_Radius * 2); } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (m_Panit) { Graphics _Graphics = pictureBox1.CreateGraphics(); _Graphics.Clear(pictureBox1.BackColor); int _RadiusW = m_Point.X - e.X; int _RadiusH = m_Point.Y - e.Y; _RadiusH = Math.Abs(_RadiusH); _RadiusW = Math.Abs(_RadiusW); if (_RadiusW > _RadiusH) _RadiusH = _RadiusW; Rectangle _EllopseRectangle = GetEllopse(m_Point, _RadiusH); _Graphics.DrawEllipse(new Pen(Brushes.Yellow, 2), _EllopseRectangle); _Graphics.Dispose(); } } /// <summary> /// 绘制标示 /// </summary> private bool m_Panit = false; /// <summary> /// 开始坐标 /// </summary> private Point m_Point = new Point(0, 0); private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { m_Panit = true; m_Point.X = e.X; m_Point.Y = e.Y; } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { m_Panit = false; } pictureBox1 是添加的图片~? public int CursorX,CursorY,r;public Image theimage;public Graphics ig,g;private void Form1_Load(object sender, System.EventArgs e){ theimage=new Bitmap(this.pictureBox1.ClientRectangle.Width,this.pictureBox1.ClientRectangle.Height); ig=Graphics.FromImage(theimage); ig.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g=this.pictureBox1.CreateGraphics(); g.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;}private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e){ if(CursorMod==0) { CursorMod=1; CursorX=e.X; CursorY=e.Y; } else { CursorMod=0; ig.DrawEllipse(new Pen(Color.Green,1),CursorX-r,CursorY-r,2*r,2*r); pictureBox1.Image=theimage; }}private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e){ if(CursorMod==1) { this.pictureBox1.Refresh(); r=Convert.ToInt32(Math.Sqrt(Math.Abs(e.X-CursorX)*Math.Abs(e.X-CursorX)+Math.Abs(e.Y-CursorY)*Math.Abs(e.Y-CursorY))); g.DrawEllipse(new Pen(Color.Green,1),CursorX-r,CursorY-r,2*r,2*r); } else//CursorMod==0 { this.pictureBox1.Refresh(); }} 多线程查询本机指定服务的状态 线程死了,没有抛异常,该怎么处理? 灰鸽子中的CServer.dat和QQwry.dat文件分别起什么作用? 如何编写dll文件啊,在线等 问一个DEVExpress中XTRAFORM调用的问题 如何实现整个项目page重载? 控件的小问题 求助一个方法调用的问题 Mdi的子窗口如何关闭? 简体转繁体的问题,急!!! 向C#程序员请教! C#math.ceiling() 怎样替代Excel中的ceiling()
按照你的描述,首先确定一个点,然后再确定半径,调用
Graphics.DrawEllipse方法你需要一个矩形来确定一个圆
实际上就是计算该矩形所在的位置以及边长的问题了,需要注意的是
矩形的位置是左上角确定的而圆心是位于矩形的中心
{ Graphics _Graphics = Graphics.FromHwnd(pictureBox1.Handle); _Graphics.DrawEllipse(new Pen(Brushes.Yellow, 2), GetEllopse(new Point(50, 50), 10));
_Graphics.Dispose(); }
/// <summary>
/// 根据圆心和半径获取图形坐标
/// </summary>
/// <param name="p_CenterPoint">圆心</param>
/// <param name="p_Radius">半径</param>
/// <returns>坐标</returns>
public Rectangle GetEllopse(Point p_CenterPoint, int p_Radius)
{
int _X = p_CenterPoint.X - p_Radius;
int _Y = p_CenterPoint.Y - p_Radius;
return new Rectangle(_X, _Y, p_Radius * 2, p_Radius * 2);
}
给pictureBox1加3个事件方法 MouseMove MouseUp MouseDown
/// <summary>
/// 根据圆心和半径获取图形坐标
/// </summary>
/// <param name="p_CenterPoint">圆心</param>
/// <param name="p_Radius">半径</param>
/// <returns>坐标</returns>
public Rectangle GetEllopse(Point p_CenterPoint, int p_Radius)
{
int _X = p_CenterPoint.X - p_Radius;
int _Y = p_CenterPoint.Y - p_Radius;
return new Rectangle(_X, _Y, p_Radius * 2, p_Radius * 2);
} private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{ if (m_Panit)
{
Graphics _Graphics = pictureBox1.CreateGraphics();
_Graphics.Clear(pictureBox1.BackColor);
int _RadiusW = m_Point.X - e.X;
int _RadiusH = m_Point.Y - e.Y; _RadiusH = Math.Abs(_RadiusH);
_RadiusW = Math.Abs(_RadiusW);
if (_RadiusW > _RadiusH) _RadiusH = _RadiusW;
Rectangle _EllopseRectangle = GetEllopse(m_Point, _RadiusH); _Graphics.DrawEllipse(new Pen(Brushes.Yellow, 2), _EllopseRectangle);
_Graphics.Dispose();
}
}
/// <summary>
/// 绘制标示
/// </summary>
private bool m_Panit = false;
/// <summary>
/// 开始坐标
/// </summary>
private Point m_Point = new Point(0, 0); private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
m_Panit = true;
m_Point.X = e.X;
m_Point.Y = e.Y;
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
m_Panit = false;
}
是添加的图片~?
public Image theimage;
public Graphics ig,g;private void Form1_Load(object sender, System.EventArgs e)
{
theimage=new Bitmap(this.pictureBox1.ClientRectangle.Width,this.pictureBox1.ClientRectangle.Height);
ig=Graphics.FromImage(theimage);
ig.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g=this.pictureBox1.CreateGraphics();
g.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
}private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(CursorMod==0)
{
CursorMod=1;
CursorX=e.X;
CursorY=e.Y;
}
else
{
CursorMod=0;
ig.DrawEllipse(new Pen(Color.Green,1),CursorX-r,CursorY-r,2*r,2*r);
pictureBox1.Image=theimage;
}
}private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(CursorMod==1)
{
this.pictureBox1.Refresh();
r=Convert.ToInt32(Math.Sqrt(Math.Abs(e.X-CursorX)*Math.Abs(e.X-CursorX)+Math.Abs(e.Y-CursorY)*Math.Abs(e.Y-CursorY)));
g.DrawEllipse(new Pen(Color.Green,1),CursorX-r,CursorY-r,2*r,2*r);
}
else//CursorMod==0
{
this.pictureBox1.Refresh();
}
}