将表单最小化到任务栏时获取异常“参数无效”

时间:2014-02-13 10:56:38

标签: c# winforms

只有在程序运行时调整表单大小时才会发生。

我单击“ - ”并将程序最小化到任务栏,然后我看到错误/异常消息。

protected override void OnLayout(LayoutEventArgs levent)
{
    base.OnLayout(levent);

    if (!_addingLines)
        SplitText(this.Text);
    if (_backBmpBU != null)
        this._backBmp = MakeBackBmp(_backBmpBU);
    if (this.BitmapModus)
    {
        UpdateBitmap();
    }
}



private void UpdateBitmap()
{
    if (_lines != null && _lines.Length > 0)
    {
        SizeF sz = new SizeF(0, 0);
        float lineOrigHeight = sz.Height;

        using (Graphics g = this.CreateGraphics())
        {
            sz = g.MeasureString("Teststring", this.Font);

            if (this._additionalSpaceBetweenLines > 0)
                sz = new SizeF(sz.Width, sz.Height + this._additionalSpaceBetweenLines);
        }

        this._textHeight = sz.Height * _lines.Length;

        if (_bmp != null)
        {
            _bmp.Dispose();
            _bmp = null;
        }

        try
        {
            if (this._textHeight > MAXHEIGHT)
                throw new Exception("Text too long, for BitmapMode.");

            _bmp = new Bitmap(this.ClientSize.Width, (int)Math.Ceiling(this._textHeight));

            using (Graphics g = Graphics.FromImage(_bmp))
            {
                //set it to value you like...
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;

                g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.None;

                using (SolidBrush b = new SolidBrush(this.ForeColor))
                {
                    for (int i = 0; i < _lines.Length; i++)
                    {
                        SolidBrush bb = b;

                        if (TrimText)
                            _lines[i] = _lines[i].Trim();

                        sz = g.MeasureString(_lines[i], this.Font);
                        lineOrigHeight = sz.Height;

                        if (this._additionalSpaceBetweenLines > 0)
                            sz = new SizeF(sz.Width, sz.Height + this._additionalSpaceBetweenLines);

                        _posX = 0;

                        if (this.TextLayoutCentered)
                            _posX = (this.ClientSize.Width - sz.Width) / 2.0F;

                        bool drect = false;
                        bool colw = false;
                        int indx = -1;
                        int length = 0;
                        string textToFind = "";
                        Color fc = this.ForeColor;
                        Color bc = Color.Transparent;
                        Color rc = Color.Transparent;

                        if (Words != null && Words.Count > 0)
                        {
                            for (int ii = 0; ii < Words.Count; ii++)
                            {
                                if (_lines[i].Contains(Words[ii].WordOrText))
                                {
                                    bb = new SolidBrush(Words[ii].ForeColor);
                                    if (Words[ii].DrawRect)
                                        drect = true;
                                    if (Words[ii].ColorOnlyThisWord)
                                        colw = true;
                                    indx = _lines[i].IndexOf(Words[ii].WordOrText);
                                    length = Words[ii].WordOrText.Length;
                                    textToFind = Words[ii].WordOrText;
                                    fc = Words[ii].ForeColor;
                                    bc = Words[ii].BackColor;
                                    rc = Words[ii].RectColor;
                                    drect = Words[ii].DrawRect;
                                }
                            }
                        }

                        if (colw)
                        {
                            //reset b and create a new color brush
                            if (bb.Equals(b) == false)
                                bb.Dispose();

                            bb = b;

                            string ftext = _lines[i];
                            float cPosX = _posX;

                            using (SolidBrush bbb = new SolidBrush(fc))
                            {
                                while (indx > -1)
                                {
                                    if (indx > 0)
                                        g.DrawString(ftext.Substring(0, indx), this.Font, bb, new PointF(cPosX, sz.Height * i + _additionalSpaceBetweenLines / 2F));

                                    cPosX += g.MeasureString(ftext.Substring(0, indx), this.Font).Width;

                                    SizeF sfWord = g.MeasureString(ftext.Substring(indx, length), this.Font);

                                    if (bc.ToArgb().Equals(Color.Transparent.ToArgb()) == false)
                                    {
                                        using (SolidBrush bbbb = new SolidBrush(bc))
                                            g.FillRectangle(bbbb, cPosX, sz.Height * i + _additionalSpaceBetweenLines / 2F, sfWord.Width, sfWord.Height);
                                    }
                                    g.DrawString(ftext.Substring(indx, length), this.Font, bbb, new PointF(cPosX, sz.Height * i + _additionalSpaceBetweenLines / 2F));

                                    cPosX += sfWord.Width;

                                    ftext = ftext.Substring(indx + length);

                                    if (textToFind.Length > 0)
                                        indx = ftext.IndexOf(textToFind);
                                    else
                                        indx = -1;
                                }

                                if (ftext.Length > 0)
                                    g.DrawString(ftext, this.Font, bb, new PointF(cPosX, sz.Height * i + _additionalSpaceBetweenLines / 2F));
                            }
                        }
                        else
                        {
                            if (bc.ToArgb().Equals(Color.Transparent.ToArgb()) == false)
                            {
                                using (SolidBrush bbbb = new SolidBrush(bc))
                                    g.FillRectangle(bbbb, _posX, sz.Height * i + _additionalSpaceBetweenLines / 2F, sz.Width, lineOrigHeight);
                            }
                            g.DrawString(_lines[i], this.Font, bb, new PointF(_posX, sz.Height * i + _additionalSpaceBetweenLines / 2F));
                        }

                        if (drect)
                        {
                            if (rc.ToArgb().Equals(Color.Transparent.ToArgb()) == false)
                                using (Pen p = new Pen(rc))
                                    g.DrawRectangle(p, _posX, sz.Height * i + _additionalSpaceBetweenLines / 2F, sz.Width, lineOrigHeight);
                        }

                        if (bb.Equals(b) == false)
                            bb.Dispose();

                        if (DrawRect)
                        {
                            using (Pen p = new Pen(this.ForeColor))
                            {
                                if (DrawRectAroundText)
                                    g.DrawRectangle(p, _posX, sz.Height * i + _additionalSpaceBetweenLines / 2F, sz.Width, lineOrigHeight);
                                else
                                    g.DrawRectangle(p, 0, sz.Height * i + _additionalSpaceBetweenLines / 2F, this.ClientSize.Width - 1, lineOrigHeight);
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            if (_bmp != null)
            {
                _bmp.Dispose();
                _bmp = null;
            }

            this.BitmapModus = false;

            //MessageBox.Show(ex.Message + " switching to Dynamic-Draw-Mode.");

            OnSwitchModeOnError();
        }
    }
}

当程序运行时,我将表单最小化到任务栏即可获得异常:

参数无效

完整的异常消息:

System.ArgumentException was caught
  HResult=-2147024809
  Message=Parameter is not valid.
  Source=System.Drawing
  StackTrace:
       at System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format)
       at System.Drawing.Bitmap..ctor(Int32 width, Int32 height)
       at ScrollLabelTest.ScrollLabel.UpdateBitmap() in e:\scrolllabel\ScrollLabel\ScrollLabel\ScrollLabel.cs:line 601
  InnerException: 

1 个答案:

答案 0 :(得分:0)

我有同样的问题,在Just-In-Time-Debugger中有一个巨大的错误列表。经过几个小时的搜索,我找到了自己的方式。试试这个,不再有例外!!

        private void picMinimize_Click(object sender, EventArgs e)
        {
           try
           {
               panelUC.Visible = false;                      //change visible status of your form, etc.
               this.WindowState = FormWindowState.Minimized; //minimize
               minimizedFlag = true;                         //set a global flag
           }
           catch (Exception)
           {

           }

        }

    private void mainForm_Resize(object sender, EventArgs e)
    {
            //check if form is minimized, and you know that this method is only called if and only if the form get a change in size, meaning somebody clicked in the taskbar on your application
        if (minimizedFlag == true)
        {

            panelUC.Visible = true;      //make your panel visible again! thats it
            minimizedFlag = false;       //set flag back
        }
    }

竖起大拇指!