2016-11-22 45 views
1

我打算讓DataGridViewComboboxCell顯示類似於文本框的3D固定樣式。我管理與使用此代碼一個ComboBox做到這一點:在3d樣式中顯示DataGridViewComboBoxColumn

public Form1() 
    { 
     cmbbox.DrawMode = DrawMode.OwnerDrawFixed; 
     cmbbox.DrawItem += ComboBox_DrawItem_3DFixed; 
    } 

    private void ComboBox_DrawItem_3DFixed(object sender, DrawItemEventArgs e) 
    { 
     ComboBox cmb = sender as ComboBox; 

     e.DrawBackground(); 
     if (e.State == DrawItemState.Focus) 
      e.DrawFocusRectangle(); 

     var index = e.Index; 
     if (index < 0 || index >= cmb.Items.Count) 
      return; 

     var item = cmb.Items[index]; 
     string text = (item == null) ? "(null)" : cmb.GetItemText(item); 
     using (var brush = new SolidBrush(e.ForeColor)) 
     { 
      e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; 
      e.Graphics.DrawString(text, e.Font, brush, e.Bounds); 
     } 
    } 

不幸的是,我不知道如何使用的DataGridViewComboBoxCell做到這一點。芹苴我沒有在這裏找到一個解決方案:

public void Form1() 
    { 
     dgView.CellPainting += dgView_EditingControlShowing; 
    } 

    void dgView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
    { 
     if (e.Control is ComboBox) 
     { 
      ComboBox cb = (ComboBox)e.Control; 
      cb.DrawMode = DrawMode.OwnerDrawFixed; 
      cb.DrawItem += new DrawItemEventHandler(ComboBox_DrawItem_3DFixed); 
     } 
    } 

但問題與此,它只會改變的DataGridViewComboBoxCell的外觀點擊特定的小區時,當它失去焦點,它返回到正常。

我找到了CellPainting事件,但我不知道它是如何工作的這段代碼。誰能幫我?謝謝!

回答

1

創建3D風格DataGridViewComboBoxColumn您應該執行這2個設置:

  1. 你應該爲組合框禁用視覺樣式編輯控制
  2. 你應該畫組合框單元格自己和繪製三維組合鍵

要做到這一點,處理EditingControlShowingCellPaint事件:

[DllImport("uxtheme.dll", ExactSpelling = true, CharSet = CharSet.Unicode)] 
static extern int SetWindowTheme(IntPtr hWnd, String pszSubAppName, String pszSubIdList); 
void dataGridView1_EditingControlShowing(object sender, 
    DataGridViewEditingControlShowingEventArgs e) 
{ 
    if (e.Control is ComboBox) 
     SetWindowTheme(e.Control.Handle, "", ""); 
} 
void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
{ 
    if (e.ColumnIndex >= 0 && e.RowIndex >= 0 && 
     this.dataGridView1.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn) 
    { 
     var r1 = e.CellBounds; 
     using (var brush = new SolidBrush(e.CellStyle.BackColor)) 
      e.Graphics.FillRectangle(brush, r1); 
     r1.Width --; 
     ControlPaint.DrawBorder3D(e.Graphics, r1, Border3DStyle.Sunken); 
     e.Paint(r1, DataGridViewPaintParts.Border | 
      DataGridViewPaintParts.ContentForeground); 
     var d = SystemInformation.VerticalScrollBarWidth; 
     var r2 = new Rectangle(r1.Right - d - 2, r1.Top + 2, d, r1.Height - 5); 
     ControlPaint.DrawComboButton(e.Graphics, r2, ButtonState.Normal); 
     e.Handled = true; 
    } 
} 

enter image description here

還製作了下面的外觀,沒有任何自定義代碼,它足以使你列的DisplayStyleComboBox

enter image description here

+0

看起來不錯,但有什麼辦法,我可以改變ControlPaint.DrawComboButton的外觀,它看起來像是3D Fixed。 –

+0

你希望它是3D。你不是嗎? –

+0

我其實想要在圖像的頂部做外觀,下面的是我得到的。 Image here:https://i.imgsafe.org/6467d94258.jpg –