我面临一个奇怪的问题。
我创建了一个包含主窗体和辅助窗体的小型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:
答案 0 :(得分:1)
也许您应该在此方法中提供参数。尝试:
form.ShowDialog(this);
答案 1 :(得分:1)
如果你没有在form.ShowDialog()方法中传递参数,这将默认为当前窗口/表单作为所有者。所以你可以试试。
form.ShowDialog(App.OpenForms[0]); // as default form
或
form.ShowDialog(this); // to make the current window as owner for the form.
修改:: 强>
Windows(Win32和WinForms.Net)处理此问题的正常方法是等待MessageQueue运行为空,然后处理所有无效的屏幕区域。这是有效的,因为当某些事情发生变化时,通常会变为其他事物(控件)也会发生变化。
以下是您必须为绘制矢量创建新的usercontrol并使用usercontrols paint事件来完成任务的答案。检查下面的代码。
private void button1_Click(object sender, EventArgs e)
{
Form2 Plot = new Form2(new Point2D(10, 10), new Point2D(100, 100));
Plot.ShowDialog();
}
public partial class Form2 : Form
{
private Point2D _originalVector;
private Point2D _rotatedVector;
public Form2()
{
InitializeComponent();
}
public Form2(Point2D OriginalVector, Point2D RotatedVector)
{
InitializeComponent();
_originalVector = OriginalVector;
_rotatedVector = RotatedVector;
}
private void userControl11_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);
}
}
}
答案 2 :(得分:0)
也许这个?
form.ShowDialog(this);
你可以粘贴堆栈跟踪吗?