我面临一个奇怪的问题。
我创建了一个包含主窗体和辅助窗体的小型 WinForms 项目。我在第一个表单中创建了第二个表单,代码类似于这个:
private void btnOpenForm_Click(object sender, EventArgs e)
{
NewForm form = new NewForm(value1, value2);
form.ShowDialog();
}
当我使用该ShowDialog()
方法时,程序在方法执行后立即失败并出现Invalid parameter
错误,而常规Show()
方法工作正常。通过stacktrace,它说我不能使用与新的所有者相同的表单,但是我应该如何显示一个模式对话框,从主表单调用?
主窗口构造函数:
public MainWindow()
{
InitializeComponent();
}
辅助表单构造函数:
public VectorPlot(Point2D OriginalVector, Point2D RotatedVector)
{
InitializeComponent();
_originalVector = OriginalVector;
_rotatedVector = RotatedVector;
}
主窗口调用辅助窗口:
VectorPlot Plot = new VectorPlot(OriginalVector, ModifiedVector);
Plot.ShowDialog();
绘画事件:
private void VectorPlot_Paint(object sender, PaintEventArgs e)
{
using (Graphics g = e.Graphics)
{
GraphicsContainer container = g.BeginContainer(new RectangleF(0f, 0f, 300f, 300f), new RectangleF(0f, 0f, 300f, 300f), GraphicsUnit.Pixel);
g.FillRectangle(new SolidBrush(Color.White), new RectangleF(0f, 0f, 300f, 350f));
g.DrawLine(new Pen(Color.Gray), 150f, 0f, 150f, 300f);
g.DrawLine(new Pen(Color.Gray), 0f, 150f, 300f, 150f);
float xOrigVect = (float)_originalVector.X;
float yOrigVect = (float)_originalVector.Y;
float xRotVect = (float)_rotatedVector.X;
float yRotVect = (float)_rotatedVector.Y;
xOrigVect = 150f + xOrigVect;
yOrigVect = 150f - yOrigVect;
xRotVect = 150f + xRotVect;
yRotVect = 150f - yRotVect;
g.DrawLine(new Pen(Color.Blue, 2f), 150f, 150f, xOrigVect, yOrigVect);
g.DrawLine(new Pen(Color.Red, 2f), 150f, 150f, xRotVect, yRotVect);
g.DrawString("Legenda: ", new System.Drawing.Font("sans-serif", 8.0f), Brushes.Black, new PointF(0f, 310f));
g.DrawLine(new Pen(Color.Blue, 2f), 0f, 330f, 10f, 330f);
g.DrawString("Vetor original", new System.Drawing.Font("sans-serif", 8.0f), Brushes.Black, new PointF(10f, 322f));
g.DrawLine(new Pen(Color.Red, 2f), 0f, 340f, 10f, 340f);
g.DrawString("Vetor rotacionado", new System.Drawing.Font("sans-serif", 8.0f), Brushes.Black, new PointF(10f, 332f));
g.EndContainer(container);
}
}
堆栈跟踪:
System.ArgumentException was unhandled
HResult=-2147024809
Message=Parâmetro inválido.
Source=System.Drawing
StackTrace:
em System.Drawing.Graphics.get_Clip()
em System.Drawing.Graphics.GetContextInfo()
em System.Windows.Forms.Internal.WindowsGraphics.FromGraphics(Graphics g, ApplyGraphicsProperties properties)
em System.Windows.Forms.WindowsGraphicsWrapper..ctor(IDeviceContext idc, TextFormatFlags flags)
em System.Windows.Forms.VisualStyles.VisualStyleRenderer.DrawBackground(IDeviceContext dc, Rectangle bounds)
em System.Windows.Forms.Form.OnPaint(PaintEventArgs e)
em System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
em System.Windows.Forms.Control.WmPaint(Message& m)
em System.Windows.Forms.Control.WndProc(Message& m)
em System.Windows.Forms.ScrollableControl.WndProc(Message& m)
em System.Windows.Forms.ContainerControl.WndProc(Message& m)
em System.Windows.Forms.Form.WndProc(Message& m)
em System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
em System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
em System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
InnerException: