2012-10-09 206 views
0

我已經在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); 
    } 

回答

1

我能夠從我的Program.cs刪除行Application.EnableVisualStyles()重現與Windows 7的一個非常類似的問題。 我使用了一個簡單的ComboBox而不是第三方,但效果是一樣的。爲了克服我有問題,做三件事情:

  1. 設置(自定義)控制,可見真正使DrawToBitmap一定能夠以使其
  2. 設置控件的父到DataGridView(如customControl.Parent = DataGridView)所以,它有一個家長說的也是可見
  3. 移動控制了可視區域(如customControl.Location = new Point(0, -(customControl.Height))因此控制沒有顯示出來,它應該不

這個工作在我的情況,但它可能取決於h您的自定義控件處理DrawToBitmap功能。

我很好奇,如果這將有助於你的情況,或者如果有人可以找到一個更優雅的解決方案。

相關問題