C# 如何以不同的颜色绘制单击的每个点?
这是鼠标按下的代码,我在其中单击点:C# 如何以不同的颜色绘制单击的每个点?,c#,C#,这是鼠标按下的代码,我在其中单击点: private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { label1.Text = e.X.ToString(); label2.Text = e.Y.ToString(); label1.Visible = true; label2.Visi
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
label1.Text = e.X.ToString();
label2.Text = e.Y.ToString();
label1.Visible = true;
label2.Visible = true;
label3.Visible = true;
label4.Visible = true;
// find the index that is closest to the current mouse location
MinDist = float.MaxValue;
for (idx = 0; idx < Point_X.Count; ++idx)
{
float dx = Point_X[idx] - e.X;
float dy = Point_Y[idx] - e.Y;
float dist = (float)Math.Sqrt(dx * dx + dy * dy);
if (dist < MinDist)
{
MinDist = dist;
selectedIndex = idx;
}
}
if (MinDist < 5)
{
mouseMove = true;
OriginalX = Point_X[(int)selectedIndex];
OriginalY = Point_Y[(int)selectedIndex];
if (cyclicSelectedIndex.Count() == 2)
{
cyclicSelectedIndex[currentCyclicIndex] = (int)selectedIndex;
currentCyclicIndex++;
if (currentCyclicIndex > 1)
{
currentCyclicIndex = 0;
}
if ((cyclicSelectedIndex[0] == cyclicSelectedIndex[1]) ||
(cyclicSelectedIndex[0] == -1) || (cyclicSelectedIndex[1] == -1))
{
button2.Enabled = false;
}
else
{
button2.Enabled = true;
}
label13.Text = selectedIndex.ToString();
label13.Visible = true;
label14.Visible = true;
listView1.Items.Add(selectedIndex.ToString()).EnsureVisible();
}
}
}
}
private void pictureBox1\u MouseDown(对象发送方,MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
label1.Text=e.X.ToString();
label2.Text=e.Y.ToString();
标签1.可见=真;
label2.Visible=true;
标签3.可见=真;
label4.可见=真;
//查找最接近当前鼠标位置的索引
MinDist=float.MaxValue;
对于(idx=0;idx1)
{
currentcyclindex=0;
}
if((cyclicSelectedIndex[0]==cyclicSelectedIndex[1])||
(cyclicSelectedIndex[0]=-1)| |(cyclicSelectedIndex[1]=-1))
{
按钮2.Enabled=false;
}
其他的
{
按钮2.Enabled=true;
}
label13.Text=selectedIndex.ToString();
label13.可见=真;
标签14.可见=真实;
listView1.Items.Add(selectedIndex.ToString()).EnsureRevible();
}
}
}
}
这是第一个FOR循环中的绘画事件,我在第二个FOR循环中画点,我在点之间画线
我想,如果我点击一个点,它将是蓝色的,如果我点击第二个点,它将是黄色的。现在,当我创建点时,它们是红色的
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Point connectionPointStart;
Point connectionPointEnd;
Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
SolidBrush brush = new SolidBrush(Color.Red);
Pen p = new Pen(brush);
for (int idx = 0; idx < Point_X.Count; ++idx)
{
Point dPoint = new Point((int)Point_X[idx], (int)Point_Y[idx]);
dPoint.X = dPoint.X - 5; // was - 2
dPoint.Y = dPoint.Y - 5; // was - 2
Rectangle rect = new Rectangle(dPoint, new Size(10, 10));
g.FillEllipse(brush, rect);
}
for (int i = 0; i < connectionStart.Count; i++)
{
int startIndex = connectionStart[i];
int endIndex = connectionEnd[i];
connectionPointStart = new Point(
(int)Point_X[startIndex], (int)Point_Y[startIndex]);
connectionPointEnd = new Point(
(int)Point_X[endIndex], (int)Point_Y[endIndex]);
p.Width = 4;
g.DrawLine(p, connectionPointStart, connectionPointEnd);
}
}
private void pictureBox1\u Paint(对象发送方,System.Windows.Forms.PaintEventArgs e)
{
点连接点开始;
点连接点端点;
图形g=e.图形;
g、 SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
SolidBrush笔刷=新的SolidBrush(颜色为红色);
笔p=新笔(刷子);
对于(int idx=0;idx
您需要修改用于绘制每个点的画笔。这里有一个简单的解决方案
首先,将另一个名为previouslySelectedIndex的int类型变量添加到类中,并用-1初始化它。然后,在鼠标按下事件中,添加如下所示的行:
...
if (dist < MinDist)
{
MinDist = dist;
previouslySelectedIndex = selectedIndex // <--- Add this line
selectedIndex = idx;
}
...
。。。
if(dist previouslySelectedIndex=selectedIndex//我不知道你的观点(这不是用简单的计数就能解决的吗?)。但是你应该处理笔、画笔和类似的东西……不能制作画笔=画笔,因为我收到错误,无法将画笔转换为SolidBrush确定我做了:画笔=(SolidBrush)画笔。黄色;并且在鼠标按下事件中也执行了代码。但是,当我运行程序并添加新点时,在我单击它们之前,它们已经是蓝色的。我希望它们像以前一样是红色的,并且只有当我单击一个点时,它才会是蓝色的,第二次单击始终是黄色的。现在,当我添加一个点时,当我单击它时,它就会是蓝色的它是黄色的。但我希望当我添加一个点时,它将是红色的单击它将是蓝色的其他点我单击它将是黄色的另一个点我单击蓝色seocnd一个黄色,但当我添加新点时,它们将始终是红色的。@user1196715:请检查更新的代码。不要强制转换为实心笔刷,而是更改变量笔刷的类型,如我上面所示。That简化语法并使代码更易于阅读。@user1196715:对于您在解决方案中发现的错误,我深表歉意。您只需在代码中添加新行即可轻松解决此问题:brush=brush.Red;如果解决方案仍然不起作用或您不明白我为什么这样做。@user1196715:澄清:添加代码中的新行“brush=brushs.Red;”在循环的每次迭代开始时出现。发生的情况是您必须在每次迭代时重置笔刷的颜色。否则,如果一个点更改笔刷的颜色,其他点将使用不正确的颜色绘制。
...
Brush brush = Brushes.Red; // <-Change the type from SolidBrush to Brush, which is more general
...
for (int idx = 0; idx < Point_X.Count; ++idx)
{
...
// START OF NEW CODE
brush = Brushes.Red; // <-- This line was not in my original solution.
if (idx==selectedIndex) // <-- Add all this code
brush = Brushes.Blue;
else if(idx==previouslySelectedIndex)
brush = Brushes.Yellow;
// END OF NEW CODE
g.FillEllipse(brush, rect);
}
...