1

我面临一个奇怪的问题。

我创建了一个包含主窗体和辅助窗体的小型 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: 
4

3 回答 3

1

也许你应该在这个方法中提供一个参数。尝试:

form.ShowDialog(this);
于 2013-10-03T12:27:24.017 回答
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 运行为空,然后处理所有无效的屏幕区域。这是有效的,因为当某些事情发生变化时,通常会级联到其他事情(控件)也发生变化。

这是您的答案,您必须为绘制矢量创建新的用户控件并使用用户控件绘制事件来完成任务。检查下面的代码。

 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);
                }

        }
    }
于 2013-10-03T12:31:44.003 回答
0

Maybe this?

form.ShowDialog(this);

Can you paste your stacktrace?

于 2013-10-03T12:30:33.517 回答