【发布时间】:2014-06-02 16:33:36
【问题描述】:
我尝试在我的应用程序中模拟火车运动,所以我使用此代码创建火车地图:
public void DrawMap()
{
Bitmap map = new Bitmap(pictureBoxMetroMap.Size.Width, pictureBoxMetroMap.Size.Height);
var graph = Graphics.FromImage(map);
List<Point> lstPointLeft = new List<Point>();
foreach (var t in lstSensorLeft)
{
Point objPoint = new Point(t.XLocation, t.YLocation);
lstPointLeft.Add(objPoint);
Rectangle rectSens = new Rectangle(t.XLocation, t.YLocation, 3, 3);
try
{
graph.FillRectangle(whiteBrush, rectSens);
}
catch (Exception ea)
{
}
if (t.StationId != null)
{
Rectangle rectEhsansq = new Rectangle(t.XLocation - 6, t.YLocation - 6, 12, 12);
graph.FillRectangle(blueBrush, rectEhsansq);
Label ObjLable = new Label();
ObjLable.ForeColor = Color.Transparent;
ObjLable.Location = new Point(t.XLocation+40, t.YLocation +50);
ObjLable.Text = ObjStationRepository.FindBy(i => i.Id == t.StationId).First().Name;
ObjLable.BackColor = Color.Transparent;
ObjLable.Width = 70;
pictureBoxMetroMap.Controls.Add(ObjLable);
}
}
List<Point> lstPointRight = new List<Point>();
foreach (var t in lstSensorRight)
{
Point objPoint = new Point(t.XLocation + 30, t.YLocation + 30);
lstPointRight.Add(objPoint);
Rectangle rectSens = new Rectangle(t.XLocation + 30, t.YLocation + 30, 3, 3);
graph.FillRectangle(whiteBrush, rectSens);
if (t.StationId != null)
{
Rectangle rectPosition = new Rectangle(t.XLocation + 24, t.YLocation + 24, 12, 12);
graph.FillRectangle(blueBrush, rectPosition);
Label ObjLable = new Label();
ObjLable.ForeColor = Color.Transparent;
ObjLable.Location = new Point(t.XLocation - 50, t.YLocation - 30);
ObjLable.Text = ObjStationRepository.FindBy(i => i.Id == t.StationId).First().Name;
ObjLable.BackColor = Color.Transparent;
ObjLable.Width = 70;
pictureBoxMetroMap.Controls.Add(ObjLable);
}
}
graph.DrawLines(pLine, lstPointLeft.ToArray());
graph.DrawLines(pLine, lstPointRight.ToArray());
pictureBoxMetroMap.Image = map;
// ShowOnlineTrain();
//Thread newThread = new Thread(new ThreadStart(ShowOnlineTrain));
//newThread.Start();
}
如您所见,DramMap 绘制了我的火车地图,我在我的应用程序的 page_load 中调用了此函数,如下所示:
private void frmMain_Load(object sender, EventArgs e)
{
UpdateListBox = new UpdateListBoxDelegate(this.UpdateStatus);
// Initialise and start worker thread
workerThread = new Thread(new ThreadStart(this.ShowOnlineTrain));
workerThread.Start();
DrawMap();
}
正如你在上面看到的,我调用了我的函数并在我的 pageload 中创建了一个线程,所以该线程执行了一项重要操作,它调用了一个函数 ShowOnlineTrain,这个函数获取在线火车的位置,我应该在我的地图上显示这些火车:
List<OnlineTrain> OnlineTrainList = new List<OnlineTrain>();
public void ShowOnlineTrain()
{
OnlineTrainRepository objOnlineTrainRepository = new OnlineTrainRepository();
while(true)
{
OnlineTrainList = objOnlineTrainRepository.GetAll().ToList();
Invoke(UpdateListBox);
}
}
private void UpdateStatus()
{
lstLog.Items.Add("Train Id=" + OnlineTrainList.First().TrainId + " | Current x position=" + OnlineTrainList.First().XTrainLocation + " | Current y position=" + OnlineTrainList.First().YTrainLocation);
}
这个函数获取在线火车的位置。所以 OnlineTrainList ** 有在线火车的位置(即 x 和 y 和 trainId)。所以我必须在我的地图上显示火车。我调用 **我的图片框的绘制事件:
private void pictureBoxMetroMap_Paint(object sender, PaintEventArgs e)
{
if (OnlineTrainList.Count > 0)
{
foreach (OnlineTrain t in OnlineTrainList)
{
var g = pictureBoxMetroMap.CreateGraphics();
Rectangle rectTrainState = new Rectangle(t.XTrainLocation.Value - 3,
t.YTrainLocation.Value - 3,
7, 7);
g.FillRectangle(RedBrush, rectTrainState);
}
}
}
它获取**OnlineTrainList **的所有位置并绘制它们,但是我这里有一个大问题,我需要显示我的火车的运动,我应该清除我的火车的旧位置,但我没有知道我该怎么做???我的火车的所有位置都画在我的图片框上!!有什么想法吗?
最好的问候
【问题讨论】:
-
旁注:不要在
Paint处理程序中使用CreateGraphics。你可以使用e.Graphics免费获得。 -
谢谢我改了
-
@DonBoitnott 您发布的答案有效,但它会清除整个地图,我需要清除地图上的火车
-
什么是“地图”?是加载并分配给
PictureBox.Image的图像吗?还是代码中的PictureBox被绘制的内容? -
@DonBoitnott 是的,地图就是分配给 pb 的图像
标签: c# multithreading winforms