如何将我点击的每个点都涂成不同的颜色

本文关键字:颜色 | 更新日期: 2023-09-27 18:29:31

这是我点击点的鼠标向下的代码:

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();
         }
      }
   }
}

这是第一个FOR LOOP中的绘制事件,我在第二个FOR LOOP中绘制点,我在点之间绘制线。

我希望如果我点击一个点,它将是蓝色的,如果我点击第二个点,将是黄色的。现在,当我创建点时,它们是红色的。

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);
   }
}

如何将我点击的每个点都涂成不同的颜色

您需要修改用于绘制每个点的笔刷。这里有一个简单的解决方案。

首先,将另一个名为previouslySelectedIndex的int类型变量添加到类中,并用-1初始化它。然后,在鼠标按下事件中,添加如下所示的行:

     ...
     if (dist < MinDist)
     {
        MinDist = dist;
        previouslySelectedIndex = selectedIndex  // <--- Add this line
        selectedIndex = idx;
     }
     ...

现在,在绘制事件处理程序中的正确位置添加下面注释的部分:

   ...
   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);
   }
   ...