DrawToBitmap不适用于Windows XP,但在Windows 7中

时间:2012-10-09 23:45:03

标签: drawtobitmap

我在datagridview中托管了一个自定义控件,让我说CustomControl(第三方控件),并且只有在进入编辑模式时才会绘制单元格。如果退出编辑模式,它是不可见的,所以我已经覆盖了paint方法(见下文)。它在Windows 7中工作正常,但在Windows XP中没有。 DrawToBitmap失败。有什么想法吗?

        protected override void Paint(
        Graphics graphics,
        Rectangle clipBounds,
        Rectangle cellBounds,
        int rowIndex,
        DataGridViewElementStates cellState,
        object value,
        object formattedValue,
        string errorText,
        DataGridViewCellStyle cellStyle,
        DataGridViewAdvancedBorderStyle advancedBorderStyle,
        DataGridViewPaintParts paintParts)         {             // Call the base class method to paint the default​ cell appearance.
        base.Paint(
            graphics,
            clipBounds,
            cellBounds,
            rowIndex,
            cellState,
            value,
            formattedValue,
            errorText,
            cellStyle,
            advancedBorderStyle,
            paintParts);

        CustomControl customControl= (CustomControl)this.DataGridView.Rows[rowIndex].Cells[this.ColumnIndex].Value;

        Bitmap img = new Bitmap(cellBounds.Width, cellBounds.Height);

        // Resize propertyEditor control to cell bounds
        propertyEditor.Height = cellBounds.Hei​ght;
        propertyEditor.Width = cellBounds.Widt​h;

        // Sets the cell's backcolor according to the data​ grid view's color
        customControl.BackColor = this.DataGridView.Rows[rowIndex].Cells[this.ColumnIndex].Style.BackColor;

        // Finally paint the propertyEditor control       ​     
        customControl.DrawToBitmap(img, new Rectangle(0, 0, customControl.Width, customControl.Height​));
        graphics.DrawImage(img, cellBounds.Loc​ation);
    }

1 个答案:

答案 0 :(得分:1)

通过从Application.EnableVisualStyles()删除行Program.cs,我能够在Windows 7中重现一个非常类似的问题。 我使用的是简单的ComboBox而不是第三方,但效果是一样的。要克服这个问题,我必须做三件事:

  1. 将(自定义)控件设置为可见,以便DrawToBitmap肯定能够呈现它
  2. 将控件的父级设置为DataGridView(例如customControl.Parent = DataGridView),以使其父级也可见
  3. 将控件移出可见区域(例如customControl.Location = new Point(0, -(customControl.Height)),以便控件不会显示在不应该显示的位置
  4. 这适用于我的情况,但可能取决于您的自定义控件如何处理DrawToBitmap函数。

    我很好奇这是否对你的情况有帮助,或者是否有人能找到更优雅的解决方案。