FillRectangle参数无效

时间:2013-10-22 12:48:16

标签: c# drawing

这是我得到的例外

System.ArgumentException was unhandled
  HResult=-2147024809
  Message=Parameter is not valid.
  Source=System.Drawing
  StackTrace:
       at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
       at System.Drawing.Graphics.FillRectangle(Brush brush, Int32 x, Int32 y, Int32 width, Int32 height)
       at System.Drawing.Graphics.FillRectangle(Brush brush, Rectangle rect)
       at frmMain.drawCboxItem(Object sender, DrawItemEventArgs e) in frmMain.cs:line 465
       at frmMain.cboxSectionCell_DrawItem(Object sender, DrawItemEventArgs e) in frmMain.cs:line 485
       at System.Windows.Forms.ComboBox.OnDrawItem(DrawItemEventArgs e)
       at System.Windows.Forms.ComboBox.WmReflectDrawItem(Message& m)
       at System.Windows.Forms.ComboBox.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
       at System.Windows.Forms.Control.WmOwnerDraw(Message& m)
       at System.Windows.Forms.Control.WmDrawItem(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ContainerControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
       at System.Windows.Forms.Control.DefWndProc(Message& m)
       at System.Windows.Forms.Control.WmOwnerDraw(Message& m)
       at System.Windows.Forms.Control.WmDrawItem(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ComboBox.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  InnerException: 

代码

if (e.Index > -1)
{
    e.DrawBackground();

    Brush bgColourBrush = null;
    Brush fgColourBrush = null;
    ComboBox combo = (ComboBox)sender;
    objPDT.ListCell pdt = (objPDT.ListCell)combo.Items[e.Index];

    if (e.ForeColor == SystemColors.HighlightText)
    {
        bgColourBrush = new SolidBrush(e.BackColor);
        fgColourBrush = new SolidBrush(e.ForeColor);
    }
    else if (pdt.bgColour == null)
    {
        bgColourBrush = Brushes.Black;
        fgColourBrush = Brushes.White;
    }
    else
    {
        bgColourBrush = pdt.bgColour;
        fgColourBrush = pdt.fgColour;
    }


        // background
        e.Graphics.FillRectangle(bgColourBrush, e.Bounds);
        //foreground
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
        e.Graphics.DrawString(pdt.Name, combo.Font, fgColourBrush, e.Bounds.X, e.Bounds.Y);

}

最后传递给它的值:

pdt {
    Section 8--> CELL}  objPDT.ListCell
    _bgcolour   {Color = {Color [A=255, R=166, G=166, B=166]}}  System.Drawing.Brush {System.Drawing.SolidBrush}
    _CellID 27  int
    _fgcolour   {Color = {Color [A=255, R=255, G=255, B=255]}}  System.Drawing.Brush {System.Drawing.SolidBrush}
    _name   "Section 8--> CELL" string
    _SectionID  8   int
    bgColour    {Color = {Color [A=255, R=166, G=166, B=166]}}  System.Drawing.Brush {System.Drawing.SolidBrush}
    fgColour    {Color = {Color [A=255, R=255, G=255, B=255]}}  System.Drawing.Brush {System.Drawing.SolidBrush}
    Name    "Section 8--> CELL" string
    Value   "8:27"  string
}

e.Bounds    {X = 0 Y = 30 Width = 290 Height = 15}  System.Drawing.Rectangle
    Bottom  45  int
    Height  15  int
    IsEmpty false   bool
    Left    0   int
    Location    {X = 0 Y = 30}  System.Drawing.Point
    Right   290 int
    Size    {Width = 290 Height = 15}   System.Drawing.Size
    Top 30  int
    Width   290 int
    X   0   int
    Y   30  int

2 个答案:

答案 0 :(得分:2)

您应该始终处置非托管类,例如BrushSolidBrushPen。假设pdt.bgColourpdt.fgColour的类型为Color,则类似以下代码的内容应该可以防止崩溃。警告:未经测试的代码。

if (e.Index > -1)
{
    e.DrawBackground();

    SolidBrush bgColourBrush = null;
    SolidBrush fgColourBrush = null;
    ComboBox combo = (ComboBox)sender;
    objPDT.ListCell pdt = (objPDT.ListCell)combo.Items[e.Index];
    try {
        if (e.ForeColor == SystemColors.HighlightText)
        {
            bgColourBrush = new SolidBrush(e.BackColor);
            fgColourBrush = new SolidBrush(e.ForeColor);
        }
        else if (pdt.bgColour == null)
        {
            bgColourBrush = new SolidBrush(Color.Black);
            fgColourBrush = new SolidBrush(Color.White);
        }
        else
        {
            bgColourBrush = pdt.bgColour;
            fgColourBrush = pdt.fgColour;
        }

        // background
        e.Graphics.FillRectangle(bgColourBrush, e.Bounds);
        //foreground
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
        e.Graphics.DrawString(pdt.Name, combo.Font, fgColourBrush, e.Bounds.X, e.Bounds.Y);
    } finally {
        if(bgColorBrush != null) {
            bgColorBrush.Dispose();
        }
        if(fgColorBrush != null) {
            fgColorBrush.Dispose();
        }
    }
}

答案 1 :(得分:1)

您可以创建bgColourBrushfgColourBrush全局变量,而不是使用每次绘制创建Brush对象,而只需改变它们的颜色。如果下面的代码没有按原样编译,请原谅我,我是徒手做到的。 像这样:

if (e.Index > -1)
{
    e.DrawBackground();

    ComboBox combo = (ComboBox)sender;
    objPDT.ListCell pdt = (objPDT.ListCell)combo.Items[e.Index];

    if (e.ForeColor == SystemColors.HighlightText)
    {
        this.bgColourBrush.Color = e.BackColor;
        this.fgColourBrush.Color = e.ForeColor;
    }
    else if (pdt.bgColour == null)
    {
        this.bgColourBrush.Color = Brushes.Black.Color;
        this.fgColourBrush.Color = Brushes.White.Color;
    }
    else
    {
        this.bgColourBrush.Color = pdt.bgColour;
        this.fgColourBrush.Color = pdt.fgColour;
    }


    // background
    e.Graphics.FillRectangle(bgColourBrush, e.Bounds);
    //foreground
    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
    e.Graphics.DrawString(pdt.Name, combo.Font, fgColourBrush, e.Bounds.X, e.Bounds.Y);
}

更新

所以我看到你的pdt ListCell对象有一个bgColour和一个fgColour属性。为了使我的上述解决方案有效,bgColourfgColour必须属于Color类型。或者,如果它们是SolidColorBrush类型,则需要执行此操作:bgColour.ColorfgColour.Color