我有一个PictureBox
,其中backgroundImage
设置为位图的区域,而Image
设置为位图的区域,其中某些点的位置用矩形绘制
我有一个MouseMove
事件处理程序,其中检测到何时有人在某个点附近按下了鼠标,然后在按下鼠标时移动了点,重新绘制了位图。
问题是,仅在运动停止时才重新绘制点,但是我希望运动保持平稳,我该如何实现?
我尝试设置1ms计时器,该计时器与检查是否按下鼠标有相同的作用,但是对我来说很难在表单坐标和PictureBox
坐标之间进行转换,所以我想避免这种情况>
//MouseMove event codem sliders determine which part of buffer to show inside picturebox, points are drawn to OutputBuffer
void BackgroundImage_MouseMove(object sender, MouseEventArgs e)
{
if(MouseDown && (LEDPainterPanel != null))
{
LEDPainterPanel.MovePoint(e.Location, PictureHorizontalScrollBar.Value, PictureVerticalScrollBar.Value);
OutputBuffer = LEDPainterPanel.Draw(BackgroundBuffer);
BackgroundImage.Image = OutputBuffer.Clone
(new Rectangle(new Point(PictureHorizontalScrollBar.Value, PictureVerticalScrollBar.Value), new Size(BackgroundImage.Width, BackgroundImage.Height)), System.Drawing.Imaging.PixelFormat.DontCare);
}
}
//MovePoint method
public void MovePoint(Point NewCoords, int XOffset, int YOffset)
{
if (CurrentSelectedPointNumber != 0 && CurrentSelectedStripNumber != 0)
{
Strips[CurrentSelectedStripNumber - 1].Points[CurrentSelectedPointNumber - 1] = new Point(NewCoords.X + XOffset, NewCoords.Y + YOffset);
}
}
SelectPointAtCoords
方法。它会遍历所有点集(条带)并遍历它们中的所有点,以选择搜索区域内的第一个点:
public void SelectPointAtCoords(Point MousePos, int XOffset, int YOffset)
{
int i = 0;
int j = 0;
foreach (Strip strip in Strips)
{
i = 0;
foreach (Point StripPoint in strip.Points)
{
if (MousePos.X + XOffset < StripPoint.X && MousePos.X + XOffset > StripPoint.X-5 && MousePos.Y + YOffset < StripPoint.Y && MousePos.Y + YOffset > StripPoint.Y - 5)
{
CurrentSelectedPointNumber = i + 1;
CurrentSelectedStripNumber = j + 1;
return;
}
i += 1;
}
j += 1;
}
CurrentSelectedPointNumber = 0;
CurrentSelectedStripNumber = 0;
}
Draw
方法。有点混乱,我仍然不了解图形对象与位图的关系,但至少它能起作用:
public Bitmap Draw(Bitmap InputBuffer)
{
Bitmap MidBuffer = new Bitmap(InputBuffer);
Graphics BufferGraphics = Graphics.FromImage(MidBuffer);
BufferGraphics.Clear(Color.Transparent);
int k = 0;
int j = 0;
MidBuffer.MakeTransparent();
foreach (Strip CurrentStrip in Strips)
{
if (CurrentStrip != null)
{
if (CurrentStrip.HasPoints())
{
for (int i = 0; i <= CurrentStrip.AmountOfPoints; i++)
{
if (i == CurrentSelectedPointNumber - 1)
{
BufferGraphics.DrawRectangle(System.Drawing.Pens.White, CurrentStrip.Points[i].X - 5, CurrentStrip.Points[i].Y - 5, 5, 5);
BufferGraphics.FillRectangle(System.Drawing.Brushes.Black, CurrentStrip.Points[i].X - 4, CurrentStrip.Points[i].Y - 4, 4, 4);
Console.WriteLine($"Drawing selected point at X:{CurrentStrip.Points[i].X} and Y:{CurrentStrip.Points[i].Y}");
}
else
{
BufferGraphics.DrawRectangle(System.Drawing.Pens.Black, CurrentStrip.Points[i].X - 5, CurrentStrip.Points[i].Y - 5, 5, 5);
BufferGraphics.FillRectangle(System.Drawing.Brushes.White, CurrentStrip.Points[i].X - 4, CurrentStrip.Points[i].Y - 4, 4, 4);
Console.WriteLine($"Drawing NOT selected point at X:{CurrentStrip.Points[i].X} and Y:{CurrentStrip.Points[i].Y}");
}
}
}
}
else
{
Console.WriteLine("Attempted to draw NULL strip");
}
k += 1;
}
//SecondBuffer = new Bitmap(SecondBuffer.Width, SecondBuffer.Height, BufferGraphics);
//InputBuffer = InputBuffer + MidBuffer;
return MidBuffer;
}