请问怎么在一个panel里画一个圆?

callmesai 2009-03-16 08:07:45
例如在panel1上画一个圆心为(x,y)半径为z的圆~~

请问怎么才能做到?谢谢啦~~~
...全文
714 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
baiyunyinv 2009-03-17
  • 打赏
  • 举报
回复

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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
Panel panel1 = new Panel();
panel1.Parent = this;
panel1.Dock = DockStyle.Fill;
//匿名方法
panel1.Paint += delegate(object sender, PaintEventArgs e)
{
// 画一个圆心为(x,y)半径为z的圆
float x = 80;
float y = 90;
float z = 50;
e.Graphics.DrawEllipse(Pens.Red, x - z, y - z, z * 2, z * 2);
};
}
}
}

wdz0909 2009-03-17
  • 打赏
  • 举报
回复
GDI+画图一般是在内存中画,直接在窗体上画刷新就会没了。
内存中画图方式:
按钮单击事件中写一下代码
Bitmap bmp = new Bitmap(400,500);
Graphic g = Graphic.FromImage(bmp);
g.DrawEllipse(...); //参数省略


然后呢在
panel中放个图片控件
pic.Image = bmp;


差不多就这个意思
AdaEniac 2009-03-17
  • 打赏
  • 举报
回复
但是为什么刷新一下就没有啦?

你是怎么刷新的,我这边很正常啊...
OnPaint 是时时画的,别说你刷新,你就是切换界面它就会重新画一次.
callmesai 2009-03-16
  • 打赏
  • 举报
回复
谢谢楼上各位~~画出圆了~~但是为什么刷新一下就没有啦?
AdaEniac 2009-03-16
  • 打赏
  • 举报
回复
在panel的Paint方法中,写上下面的代码...
private int x = 100;
private int y = 100;
private int z = 50;
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawEllipse(new Pen(Color.Green, 2), x - z, y - z, 2 * z, 2 * z);

//下面部分可以省略,只是方便验证
e.Graphics.DrawLine(new Pen(Color.Blue, 1), 0, 0, x, y);
e.Graphics.DrawLine(new Pen(Color.Blue, 1), 0, y, x, y);
e.Graphics.DrawLine(new Pen(Color.Blue, 1), x, 0, x, y);
}
wenblue7 2009-03-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuyi8808 的回复:]
C# codeusing System;
using System.Drawing;
using System.Windows.Forms;

class Form1 : Form
{
Form1()
{
Panel panel1 = new Panel();
panel1.Parent = this;
panel1.Dock = DockStyle.Fill;
panel1.Paint += delegate(object o, PaintEventArgs e)
{
// 画一个圆心为(x,y)半径为z的圆
float x = 80;
float y = 90;
float z = 50;
e.Graphics.D…
[/Quote]
方法很好啊
wuyi8808 2009-03-16
  • 打赏
  • 举报
回复
如果要画实心圆,则用 FillEllipse() 方法:

using System;
using System.Drawing;
using System.Windows.Forms;

class Form1 : Form
{
Form1()
{
Panel panel1 = new Panel();
panel1.Parent = this;
panel1.Dock = DockStyle.Fill;
panel1.Paint += delegate(object o, PaintEventArgs e)
{
// 画一个圆心为(x,y)半径为z的实心圆
float x = 80;
float y = 90;
float z = 20;
e.Graphics.FillEllipse(new SolidBrush(Color.Blue), x - z, y - z, z * 2, z * 2);
};
}

static void Main()
{
Application.Run(new Form1());
}
}
kbtjh 2009-03-16
  • 打赏
  • 举报
回复

private void panel1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawEllipse( new Pen (Color.Yellow ,1 ) ,5,5,150,150);
}
你看看吧!!
cppfaq 2009-03-16
  • 打赏
  • 举报
回复
int x = 100;
int y = 100;
int z = 40;
Graphics g = CreateGraphics();
Pen pen = new Pen(Color.Red, 2);

g.DrawEllipse(pen, x - z, y - z, 2 * z , 2* z);
g.Flush();
g.Dispose();
wuyi8808 2009-03-16
  • 打赏
  • 举报
回复
using System;
using System.Drawing;
using System.Windows.Forms;

class Form1 : Form
{
Form1()
{
Panel panel1 = new Panel();
panel1.Parent = this;
panel1.Dock = DockStyle.Fill;
panel1.Paint += delegate(object o, PaintEventArgs e)
{
// 画一个圆心为(x,y)半径为z的圆
float x = 80;
float y = 90;
float z = 50;
e.Graphics.DrawEllipse(Pens.Red, x - z, y - z, z * 2, z * 2);
};
}

static void Main()
{
Application.Run(new Form1());
}
}

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧