GraphicsPath相当难的问题

天地客人 2016-10-22 02:00:52
在Winform中随机生成100个小圆,当鼠标移上去时,显示当前是哪一个圆(索引或坐标),坐等大家回复!!

原码如下:
using System;
using System.Drawing;
using System.Windows.Forms;

namespace DrawDeom
{
public partial class Form2 : Form
{
private Point[] aDraw = new Point[100];
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();

public Form2()
{
InitializeComponent();
Run100();
}

private void Run100()
{
System.Random rnd = new Random();

int x, y;

for (int i = 0; i < aDraw.Length; i++)
{
x = rnd.Next(this.Width);
y = rnd.Next(this.Height);
aDraw[i].X = x;
aDraw[i].Y = y;

path.AddEllipse(x, y, 5, 5);
}
}

private void Draw(Graphics _g)
{
Pen redPen = new Pen(Color.Red, 1);
_g.DrawPath(redPen, path);
}

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Draw(e.Graphics);
}

protected override void OnMouseMove(MouseEventArgs e)
{
Point p = System.Windows.Forms.Control.MousePosition;
toolStripStatusLabel1.Text = string.Format("X:{0}-Y{1}", p.X, p.Y);
p = this.PointToClient(p);
if (path.IsVisible(p.X, p.Y))
{
toolStripStatusLabel1.ForeColor = Color.Red;
toolTip1.ToolTipTitle = "标题";
//显示当前圆的索引或左上角坐标
toolTip1.Show("当前图形:XX", this);
}
else
{
toolStripStatusLabel1.ForeColor = Color.Black;
toolTip1.Hide(this);
}
}
}
}
...全文
438 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
天地客人 2016-10-26
  • 打赏
  • 举报
回复
有没有其它更好的方法
天地客人 2016-10-26
  • 打赏
  • 举报
回复
感谢楼上几位回复,这是个需求Demo,图形不一,数量不限,同时考虑性能
xuzuning 2016-10-23
  • 打赏
  • 举报
回复
你可以这样写
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;
using System.Drawing.Drawing2D;

namespace 鼠标定位
{
public partial class Form1 : Form
{
List<GraphicsPath> path = new List<GraphicsPath>();
public Form1()
{
InitializeComponent();
Run100();
}
void Run100()
{
System.Random rnd = new Random();
int x, y;
for (int i = 0; i < 100; i++)
{
x = rnd.Next(this.Width);
y = rnd.Next(this.Height);
var p = new GraphicsPath();
p.AddEllipse(x, y, 5, 5);
path.Add(p);
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
foreach (var p in path)
e.Graphics.DrawPath(Pens.Red, p);
}
protected override void OnMouseMove(MouseEventArgs e)
{
var p = path.Find(t=>t.IsVisible(e.X,e.Y));
if(p != null)
{
var r = p.GetBounds();
toolTip1.ToolTipTitle = "标题";
toolTip1.Show(string.Format("圆心:{0},{1}", (r.Left + r.Right) / 2, (r.Top + r.Bottom) / 2), this);
}
else toolTip1.Hide(this);
}
}
}
xuzuning 2016-10-23
  • 打赏
  • 举报
回复
你这样写是不行的! Run100 方法只是在 path 中放置了 100 个圆 而 path.IsVisible(p.X, p.Y) 只是判定鼠标是否在这 100 个圆中的某一个上。但不能知道具体在哪一个圆上 你应该创建 100个 GraphicsPath,在每个中放一个圆 这样就能通过 path[i].IsVisible(p.X, p.Y),知道鼠标具体在哪一个(或几个)圆上
  • 打赏
  • 举报
回复
封装出来一个高级的“圆”对象,然后定义它们自己的行为。而不是扔一个数组 private Point[] aDraw = new Point[100] 在窗体里。
  • 打赏
  • 举报
回复
看看起来你总是从底层、拼凑的角度来提出简单的问题。 一个专业的系统,它将“圆”封装为控件,这个控件本身就会捕获鼠标划过 Border 的事件。一个专业的系统,它是从底层就分层分级处理好 UI 架构的。而一个低级的系统,总是貌似高大上地去面对每一个应用都重新编写一大堆“遍历底层”的代码。
汉软 2016-10-22
  • 打赏
  • 举报
回复
循环计算当前鼠标坐标与你画的各圆点的圆心之间的距离,如果距离小于半径,当前坐标在该圆点内,ToolTip显示该圆点索引或左上角坐标即可。

111,092

社区成员

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

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

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