以3d样式显示DataGridViewComboBoxColumn

时间:2016-11-22 03:58:12

标签: c# winforms datagridview datagridviewcomboboxcell

我计划让DataGridViewComboboxCell显示类似于文本框的3D固定样式。我设法使用这个代码使用Combobox来完成它:

    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 个答案:

答案 0 :(得分:1)

创建3D样式DataGridViewComboBoxColumn您应该执行以下两个设置:

  1. 您应该禁用组合框编辑控件的视觉样式
  2. 您应该自己绘制组合框单元并绘制一个3d组合按钮
  3. 要执行此操作,请处理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

    另外要创建以下外观,没有任何自定义代码,只需将列的DisplayStyle设置为ComboBox

    enter image description here