winform datagridview 表头重绘问题

最近有做winform的东西,遇到了datagridview  需要在表头加 复选框,并且表头是自定义的,费了很多时间搞了一个重绘表头的东东。

 

public class DataGridviewCheckboxHeaderEventHander : EventArgs
    {
        private bool checkedState = false;

        public bool CheckedState
        {
            get { return checkedState; }
            set { checkedState = value; }
        }
    }

    public delegate void DataGridviewCheckboxHeaderCellEventHander(object sender, DataGridviewCheckboxHeaderEventHander e);

    public class DataGridviewCheckboxHeaderCell : DataGridViewColumnHeaderCell
    {
        private Point checkBoxLocation;
        private Size checkBoxSize;
        private bool isChecked = false;
        private Point cellLocation = new Point();
        private System.Windows.Forms.VisualStyles.CheckBoxState cbState = System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;


        public event DataGridviewCheckboxHeaderCellEventHander OnCheckBoxClicked;


        protected override void Paint(Graphics g,
                                          Rectangle clipBounds,
                                          Rectangle cellBounds,
                                          int rowIndex,
                                          DataGridViewElementStates dataGridViewElementState,
                                          object value,
                                          object formattedValue,
                                          string errorText,
                                          DataGridViewCellStyle cellStyle,
                                          DataGridViewAdvancedBorderStyle advancedBorderStyle,
                                          DataGridViewPaintParts paintParts)
        {
            base.Paint(g, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
            Point p = new Point();
            Size s = CheckBoxRenderer.GetGlyphSize(g, System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
            //列头checkbox的X坐标
            p.X = cellBounds.Location.X + (cellBounds.Width / 2) - (s.Width / 2) - 1;
            //列头checkbox的Y坐标
            p.Y = cellBounds.Location.Y + (cellBounds.Height / 2) - (s.Height / 2);
            cellLocation = cellBounds.Location;
            checkBoxLocation = p;
            checkBoxSize = s;
            if (isChecked)
                cbState = System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal;
            else
                cbState = System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;
            //绘制复选框
            CheckBoxRenderer.DrawCheckBox(g, checkBoxLocation, cbState);
        }



        protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
        {
            Point p = new Point(e.X + cellLocation.X, e.Y + cellLocation.Y);
            if (p.X >= checkBoxLocation.X && p.X <= checkBoxLocation.X + checkBoxSize.Width && p.Y >= checkBoxLocation.Y && p.Y <= checkBoxLocation.Y + checkBoxSize.Height)
            {
                isChecked = !isChecked;

                //获取列头checkbox的选择状态
                DataGridviewCheckboxHeaderEventHander ex = new DataGridviewCheckboxHeaderEventHander();
                ex.CheckedState = isChecked;

                //此处不代表选择的列头checkbox,只是作为参数传递。应该列头checkbox是绘制出来的,无法获得它的实例
                object sender = new object();

                if (OnCheckBoxClicked != null)
                {
                    //触发单击事件
                    OnCheckBoxClicked(sender, ex);
                    this.DataGridView.InvalidateCell(this);
                }

            }
            base.OnMouseClick(e);
        }


    }

 

转载于:https://www.cnblogs.com/jiasonglin/archive/2013/03/28/2986040.html

你可能感兴趣的:(winform datagridview 表头重绘问题)