2016-11-22 44 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 –