我正在开发一种解决方案,其中有一个Windows窗体,其中放置了一个pictureBox。我想根据已经计算出的压力中心值在屏幕上移动它。
我实例化了一个名为 pressureCenterManager 的类。
因此,在我的表单代码中,我具有以下内容:
pressureCenterManager.displayPressureCenter(pressureMatrix, this.plot, this.pbox_CoG);
我在var pressureCenter 中得到了正确的值。
这是我在displayPressureCenter函数中的代码:
public void displayPressureCenter(double[,] pressureMatrix, Plot plot, PictureBox pictureBox)
{
//Get matrix size
int xSize = pressureMatrix.GetLength(0);
int ySize = pressureMatrix.GetLength(1);
try
{
//Get CoP and move pictureBox
System.Windows.Point centerOfPressure = getCenterOfPressure(pressureMatrix);
pictureBox.Visible = true;
pictureBox.Parent = plot.plotView;
//Calculamos el punto dónde hay que printar utilizando una regla de 3 y descontando la mitad del tamaño de la señal (para que quede centrada)
System.Drawing.Point displayPositionCart = new System.Drawing.Point((int)Math.Round((centerOfPressure.X * plot.plotView.Width / xSize) - (pictureBox.Width / 2)), (int)Math.Round((centerOfPressure.Y * plot.plotView.Height / ySize) - (pictureBox.Height / 2)));
//Pasamos a coordenadas de pantalla y aplicamos un offset para quitar el eje
System.Drawing.Point displayPositionScre = CartesianToScreenCoordinates(displayPositionCart, plot.plotView);
displayPositionScre.Offset(0, -70);
pictureBox.Location = displayPositionScre;
}
catch
{
}
我不知道为什么,当执行 pictureBox.Visible = true; 时,它会跳转到catch部分。
你能帮我吗?
非常感谢!
答案 0 :(得分:0)
一些建议:
代码
public void displayPressureCenter(double[,] pressureMatrix, Plot plot, PictureBox pictureBox)
{
if ( pictureBox.InvokeRequired )
{
this.Invoke(delegate { displayPressureCenter(pressureMatrix, plot, pictureBox)});
exit;
}
//Get matrix size
int xSize = pressureMatrix.GetLength(0);
int ySize = pressureMatrix.GetLength(1);
try
{
//Get CoP and move pictureBox
System.Windows.Point centerOfPressure = getCenterOfPressure(pressureMatrix);
pictureBox.Visible = true;
pictureBox.Parent = plot.plotView;
//Calculamos el punto dónde hay que printar utilizando una regla de 3 y descontando la mitad del tamaño de la señal (para que quede centrada)
System.Drawing.Point displayPositionCart = new System.Drawing.Point((int)Math.Round((centerOfPressure.X * plot.plotView.Width / xSize) - (pictureBox.Width / 2)), (int)Math.Round((centerOfPressure.Y * plot.plotView.Height / ySize) - (pictureBox.Height / 2)));
//Pasamos a coordenadas de pantalla y aplicamos un offset para quitar el eje
System.Drawing.Point displayPositionScre = CartesianToScreenCoordinates(displayPositionCart, plot.plotView);
displayPositionScre.Offset(0, -70);
pictureBox.Location = displayPositionScre;
}
catch (Exception e)
{
throw e;
}
// Rest of your code
参考: https://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/zyzhdc6b(v=vs.110).aspx