我所做的是,在 Form1 的顶部添加了一个新的 int 变量 moveCounter。在构造函数中,我以 0 开始变量。 moveCounter = 0;
在pictureBox1 绘制事件中,我做了:moveCounter++;而且在绘画事件中,我将 moveCounter 值放入一个文本框:
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
SolidBrush brush;
Pen p=null;
Point connectionPointStart;
Point connectionPointEnd;
Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
brush = new SolidBrush(Color.Red);
p = new Pen(brush);
for (int idx = 0; idx < wireObject1._point_X.Count; ++idx)
{
Point dPoint = new Point((int)wireObject1._point_X[idx], (int)wireObject1._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 < wireObject1._connectionstart.Count; i++)
{
int startIndex = wireObject1._connectionstart[i];
int endIndex = wireObject1._connectionend[i];
connectionPointStart = new Point((int)wireObject1._point_X[startIndex], (int)wireObject1._point_Y[startIndex]);
connectionPointEnd = new Point((int)wireObject1._point_X[endIndex], (int)wireObject1._point_Y[endIndex]);
p.Width = 4;
g.DrawLine(p, connectionPointStart, connectionPointEnd);
moveCounter++;
textBox1.Text = moveCounter.ToString();
}
}
在Form1的顶部,我创建了一个StreamWriter w的变量;在构造函数中我做了:
w = new StreamWriter(@"d:\movesCounter.txt");
在 pictureBox1 鼠标移动事件中,我检查是否只有 moveCounter 大于 10,然后将值写入文本文件。
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseMove == true)
{
mouseDrag = true;
Point NewPoint = e.Location;
wireObject1.MovePoint(selectedIndex, NewPoint, NewPoint); // when moving a point dragging the other point is vanished deleted. To check why !
label1.Text = "{X = " + NewPoint.X + "}" + " " + "{Y = " + NewPoint.Y + "}";
refreshFlag = true;
timer2.Start();
if (moveCounter > 10)
{
w.WriteLine("Number Of Moves ===> " + moveCounter);
}
}
else
{
label19.Text = "{X = " + e.X + "}" + " " + "{Y = " + e.Y + "}";
}
}
在pictureBox1 鼠标向上事件中,我关闭了StreamWriter:
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (mouseMove == true)
{
Point NewPoint = e.Location;
wireObject1._point_X[(int)selectedIndex] = NewPoint.X;
wireObject1._point_Y[(int)selectedIndex] = NewPoint.Y;
mouseMove = false;
if (moveCounter > 10)
{
w.Close();
}
WireObjectCoordinatesCloneFrame();
}
}
我想要做的是将 moveCounter 的值记录/写入文本文件。我把 moveCounter++; 在底部循环中的绘制事件中,当两个或多个点通过线/秒连接时,此循环起作用,并且当我在图片框区域客户端周围移动连接点之一时,我实时看到 moveCounter 值上升。
问题是在我硬盘上的文本文件中,我得到的结果是这样的:
Number Of Moves ===> 11
Number Of Moves ===> 11
Number Of Moves ===> 11
Number Of Moves ===> 12
Number Of Moves ===> 12
Number Of Moves ===> 12
Number Of Moves ===> 13
Number Of Moves ===> 13
Number Of Moves ===> 13
Number Of Moves ===> 14
Number Of Moves ===> 14
Number Of Moves ===> 14
Number Of Moves ===> 15
Number Of Moves ===> 15
Number Of Moves ===> 16
Number Of Moves ===> 16
Number Of Moves ===> 17
Number Of Moves ===> 17
Number Of Moves ===> 18
Number Of Moves ===> 18
Number Of Moves ===> 19
Number Of Moves ===> 19
Number Of Moves ===> 20
Number Of Moves ===> 20
Number Of Moves ===> 20
Number Of Moves ===> 21
Number Of Moves ===> 21
Number Of Moves ===> 21
我不明白为什么我会连续两次甚至三次获得某些值,例如 20 20 20 或 21 21 21 或 18 18,而不是每个值都获得一次。
这是值 169。我想添加这个经过 5 秒,没关系,但值 169 再次出现几次而不是一次。在所有的文本文件中都是这样。
Number Of Moves ===> 169
Passed 5 Seconds ===> 5
Number Of Moves ===> 169
有什么解决办法?
已编辑**
这是pictureBox1 鼠标按下事件,我将mouseMove bool 变量设置为true,所以问题可能出在此处。
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
label1.Visible = true;
label4.Visible = true;
float t = wireObject1.GetIndexByXY(e.X, e.Y, 5);
if (t == -1)
{
button3.Enabled = false;
}
else
{
button3.Enabled = true;
{
selectedIndex = t;
mouseMove = true;
OriginalX = wireObject1._point_X[(int)selectedIndex];
OriginalY = wireObject1._point_Y[(int)selectedIndex];
if (cyclicSelectedIndex.Count() == 2)
{
cyclicSelectedIndex[currentCyclicIndex] = (int)selectedIndex;
currentCyclicIndex++;
if (currentCyclicIndex == 2)
{
currentCyclicIndex = 0;
}
if ((cyclicSelectedIndex[0] == cyclicSelectedIndex[1]) || (cyclicSelectedIndex[0] == -1) || (cyclicSelectedIndex[1] == -1))
{
button2.Enabled = false;
}
else
{
button2.Enabled = true;
}
for (int i = 0; i < wireObject1._connectionstart.Count; i++)
{
if ((wireObject1._connectionstart[i] == cyclicSelectedIndex[0] && wireObject1._connectionend[i] == cyclicSelectedIndex[1]) ||
(wireObject1._connectionstart[i] == cyclicSelectedIndex[1] && wireObject1._connectionend[i] == cyclicSelectedIndex[0]) ||
wireObject1._connectionstart[i] == cyclicSelectedIndex[0] || wireObject1._connectionend[i] == cyclicSelectedIndex[1] ||
wireObject1._connectionstart[i] == cyclicSelectedIndex[1] || wireObject1._connectionend[i] == cyclicSelectedIndex[0])
{
button3.Enabled = false;
}
if ((wireObject1._connectionstart[i] == cyclicSelectedIndex[0] && wireObject1._connectionend[i] == cyclicSelectedIndex[1]) ||
(wireObject1._connectionstart[i] == cyclicSelectedIndex[1] && wireObject1._connectionend[i] == cyclicSelectedIndex[0]))
{
button2.Enabled = false;
}
}
label13.Text = selectedIndex.ToString();
label13.Visible = true;
label14.Visible = true;
listView1.Items.Add(selectedIndex.ToString()).EnsureVisible();
}
}
}
}
}
另一件事是我在设计器 Timer1 中添加了一个计时器。计时器已启用并设置为 30 毫秒。
在图片框鼠标移动事件中,我做了:
refreshFlag = true;
然后在 timer1 滴答事件中,我检查这个标志是否为真,然后制作一个图片框1.刷新();这个想法是当我拖动/移动在移动事件中连接或未连接的点时,仅每 30 毫秒进行一次绘制事件。那么问题可能出在哪里?
这是 timer1 滴答事件:
private void timer1_Tick(object sender, EventArgs e)
{
if (refreshFlag == true)
{
pictureBox1.Refresh();
refreshFlag = false;
}
}