DrawToBitmap不能在Windows XP上工作,但在Windows 7中是

问题描述:

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

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

我能够从我的Program.cs删除行Application.EnableVisualStyles()重现与Windows 7的一个非常类似的问题。 我使用了一个简单的ComboBox而不是第三方,但效果是一样的。为了克服我有问题,做三件事情:

  1. 设置(自定义)控制,可见真正使DrawToBitmap一定能够以使其
  2. 设置控件的父到DataGridView(如customControl.Parent = DataGridView)所以,它有一个家长说的也是可见
  3. 移动控制了可视区域(如customControl.Location = new Point(0, -(customControl.Height))因此控制没有显示出来,它应该不

这个工作在我的情况,但它可能取决于h您的自定义控件处理DrawToBitmap功能。

我很好奇,如果这将有助于你的情况,或者如果有人可以找到一个更优雅的解决方案。