C# 在面板中绘制动态椭圆
我只想在跑步时动态地画一个椭圆。鼠标单击,然后鼠标移动,然后鼠标释放,就是这样。但是,对检测点(x,y)感到困惑。有人能帮我解决这个问题吗?你基本上只需要记录MouseDown事件的起点,这样你就可以用MouseUp事件记录的点制作椭圆 简单演示:C# 在面板中绘制动态椭圆,c#,.net,C#,.net,我只想在跑步时动态地画一个椭圆。鼠标单击,然后鼠标移动,然后鼠标释放,就是这样。但是,对检测点(x,y)感到困惑。有人能帮我解决这个问题吗?你基本上只需要记录MouseDown事件的起点,这样你就可以用MouseUp事件记录的点制作椭圆 简单演示: public partial class Form1 : Form { private Point _StartPoint; private List<Rectangle> _Ovals = new List<Rectan
public partial class Form1 : Form {
private Point _StartPoint;
private List<Rectangle> _Ovals = new List<Rectangle>();
public Form1() {
InitializeComponent();
this.MouseDown += new MouseEventHandler(Form1_MouseDown);
this.MouseUp += new MouseEventHandler(Form1_MouseUp);
this.Paint += new PaintEventHandler(Form1_Paint);
}
void Form1_Paint(object sender, PaintEventArgs e) {
foreach (Rectangle r in _Ovals)
e.Graphics.FillEllipse(Brushes.Red, r);
}
void Form1_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left)
_StartPoint = e.Location;
}
void Form1_MouseUp(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
_Ovals.Add(MakeRectangle(_StartPoint, e.Location));
this.Invalidate();
}
}
private Rectangle MakeRectangle(Point p1, Point p2) {
int x = (p1.X < p2.X ? p1.X : p2.X);
int y = (p1.Y < p2.Y ? p1.Y : p2.Y);
int w = Math.Abs(p1.X - p2.X);
int h = Math.Abs(p1.Y - p2.Y);
return new Rectangle(x, y, w, h);
}
}
公共部分类表单1:表单{
私人点(StartPoint);;
私有列表_Ovals=新列表();
公共表格1(){
初始化组件();
this.MouseDown+=新的MouseEventHandler(Form1\u MouseDown);
this.MouseUp+=新的MouseEventHandler(Form1_MouseUp);
this.Paint+=新的PaintEventHandler(Form1_Paint);
}
void Form1_Paint(对象发送器、PaintEventArgs e){
foreach(椭圆中的矩形r)
e、 图形。填充椭圆(画笔。红色,r);
}
void Form1\u MouseDown(对象发送器,MouseEventArgs e){
if(e.Button==MouseButtons.Left)
_起始点=e.位置;
}
void Form1_MouseUp(对象发送器,MouseEventArgs e){
if(e.Button==MouseButtons.Left){
_添加(MakeRectangle(_StartPoint,e.Location));
这个。使无效();
}
}
私有矩形MakeRectangle(点p1、点p2){
intx=(p1.x
您使用的是哪种UI框架?WPF?WinForms(GDI)?如果这是WinForms,您正在寻找。对不起,朋友们,昨天无法提前回复