在子窗体中将DoubleBuffered设置为true时,System.ArgumentException将被抛出在Program.cs中(System.Drawing.dll)

时间:2019-04-21 00:07:43

标签: c# winforms

我正在尝试通过将DoubleBuffered = true设置为用户单击主窗体中的按钮时打开的窗体来解决Paint事件中的一些闪烁问题。但是,就像加载新表单一样,我在Program.cs中得到了异常

"System.ArgumentException occurred in System.Drawing.dll"


"System.ArgumentException: 'Parameter is not valid.'"

在Program.cs文件中

我可以确认错误弹出时没有调用Paint方法,并且该错误似乎是在窗体的构造函数完成时发生的。 我正在设计器属性中设置DoubleBuffered = True

第一个表单调用第二个表单(Form1.cs)

if(toggleButton())
            {
                int numThreads = Int32.Parse(numeric_threads.Value.ToString());


                List<Section> sections = new List<Section>();
                sections.Add(new Section(0, 0, prevScreen.Bounds.Width, 100, checkbox_UseAudio.Checked, false, Int32.Parse(numeric_horizontalLEDs.Value.ToString()))); //Top
                sections.Add(new Section(0, prevScreen.Bounds.Height - 100, prevScreen.Bounds.Width, 100, false, false, Int32.Parse(numeric_horizontalLEDs.Value.ToString()))); //Bottom
                sections.Add(new Section(0, 0, 100, prevScreen.Bounds.Height, false, true, Int32.Parse(numeric_verticalLEDs.Value.ToString()))); //Left
                sections.Add(new Section(prevScreen.Bounds.Width - 100, 0, 100, prevScreen.Bounds.Height, false, true, Int32.Parse(numeric_verticalLEDs.Value.ToString())));//  Right
                dial = new LEDSimulate(pathScript, pathPython, formInst, sections, combo_monitor.SelectedIndex, numThreads);
                dial.Show();
            }
            else
            {
                button_flag = true;
                //Kill all the threads by closing
                dial.Close();
            }

第二个表单从第一个表单中打开并导致错误(LEDSimulate.cs)

        public LEDSimulate(string pathScript, string pathPython, Form1 formInst, List<Section> sections, int mon, int numThreads)
        {
            InitializeComponent();
            instance = this;

            this.TransparencyKey = (BackColor);
            this.sections = sections;
            this.procs = new List<Process>();
            this.formInst = formInst;

            //Initialize timer to tick every 30 seconds to update UI
            timer_updatePaint.Interval = 33;

            //Load with dummy data to update later
            Pen dummy_pen = new Pen(Color.FromArgb(0, 0, 0, 0));
            foreach (Section s in sections)
            {
                for(int i = 0; i<s.subSections; i++)
                {
                    paintList.Add(new PaintForm(dummy_pen, 0, 0, 0, 0));
                }
            }
            Console.WriteLine("Finished adding and starting");
            timer_updatePaint.Start();
            //-----Once this is done, the error occurs------
        }

Program.cs

static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1()); //Error pops up on this line
        }
    }
        private void timer_updatePaint_Tick(object sender, EventArgs e)
        {
            this.Refresh();
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;  // Turn on WS_EX_COMPOSITED
                return cp;
            }
        }

0 个答案:

没有答案