2025-03-18 14:20:11 +08:00

124 lines
5.0 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Drawing;
using System.Windows.Forms;
namespace DH.Commons.Enums
{
public static class UIHelper
{
public static void SetCombo(ComboBox cbo, object dataSource, string display, string value)
{
cbo.DataSource = dataSource;
cbo.DisplayMember = display;
if (!string.IsNullOrWhiteSpace(value))
{
cbo.ValueMember = value;
}
}
public static void SetCombo(ToolStripComboBox cbo, object dataSource, string display, string value)
{
cbo.ComboBox.DataSource = dataSource;
cbo.ComboBox.DisplayMember = display;
if (!string.IsNullOrWhiteSpace(value))
{
cbo.ComboBox.ValueMember = value;
}
}
public static void SetCombo(DataGridViewComboBoxColumn cbo, object dataSource, string display, string value)
{
cbo.DataSource = dataSource;
cbo.DisplayMember = display;
if (!string.IsNullOrWhiteSpace(value))
{
cbo.ValueMember = value;
}
}
}
#region DataGridView设置列头为复选框
public class DataGridViewCheckboxHeaderEventArgs : EventArgs
{
private bool checkedState = false;
public bool CheckedState
{
get { return checkedState; }
set { checkedState = value; }
}
}
public delegate void DataGridViewCheckboxHeaderEventHander(object sender, DataGridViewCheckboxHeaderEventArgs e);
public class DataGridViewCheckboxHeaderCell : DataGridViewColumnHeaderCell
{
Point checkBoxLocation;
Size checkBoxSize;
bool _checked = false;
Point _cellLocation = new Point();
System.Windows.Forms.VisualStyles.CheckBoxState _cbState = System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;
public event DataGridViewCheckboxHeaderEventHander OnCheckBoxClicked;
        //绘制列头checkbox
        protected override void Paint(System.Drawing.Graphics graphics,
System.Drawing.Rectangle clipBounds,
System.Drawing.Rectangle cellBounds,
int rowIndex,
DataGridViewElementStates dataGridViewElementState,
object value,
object formattedValue,
string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex,
dataGridViewElementState, value,
formattedValue, errorText, cellStyle,
advancedBorderStyle, paintParts);
Point p = new Point();
Size s = CheckBoxRenderer.GetGlyphSize(graphics, System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
p.X = cellBounds.Location.X + (cellBounds.Width / 2) - (s.Width / 2) - 1;   //列头checkbox的X坐标
            p.Y = cellBounds.Location.Y + (cellBounds.Height / 2) - (s.Height / 2);     //列头checkbox的Y坐标
            _cellLocation = cellBounds.Location;
checkBoxLocation = p;
checkBoxSize = s;
if (_checked)
_cbState = System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal;
else
_cbState = System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;
CheckBoxRenderer.DrawCheckBox(graphics, checkBoxLocation, _cbState);
}
        ///
        /// 点击列头checkbox单击事件
        ///
        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)
{
_checked = !_checked;
                //获取列头checkbox的选择状态
                DataGridViewCheckboxHeaderEventArgs ex = new DataGridViewCheckboxHeaderEventArgs();
ex.CheckedState = _checked;
object sender = new object();//此处不代表选择的列头checkbox只是作为参数传递 因为列头checkbox是绘制出来的无法获得它的实例
                if (OnCheckBoxClicked != null)
{
OnCheckBoxClicked(sender, ex);//触发单击事件
                    DataGridView.InvalidateCell(this);
}
}
base.OnMouseClick(e);
}
}
#endregion
}