259 lines
9.5 KiB
C#
259 lines
9.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using DevComponents.DotNetBar.Controls;
|
|
using NaiBa.Util.Extension;
|
|
|
|
namespace TemuKJD.UserComponent
|
|
{
|
|
public class DataGridViewEx : DataGridView
|
|
{
|
|
Form parentForm = null;
|
|
public DataGridViewEx()
|
|
{
|
|
BackgroundColor = Color.White;
|
|
AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(249, 249, 249);
|
|
RowPostPaint += DataGridViewEx_RowPostPaint;
|
|
this.CellContentClick += DataGridViewEx_CellContentClick;
|
|
|
|
this.SetStyle(
|
|
ControlStyles.OptimizedDoubleBuffer |
|
|
ControlStyles.EnableNotifyMessage |
|
|
ControlStyles.ResizeRedraw |
|
|
ControlStyles.AllPaintingInWmPaint,
|
|
true);
|
|
|
|
this.CellPainting += DataGridViewEx_CellPainting;
|
|
|
|
Task.Run(() =>
|
|
{
|
|
for (int i = 0; i < 50; i++)
|
|
{
|
|
System.Threading.Thread.Sleep(100);
|
|
parentForm = FindForm();
|
|
if (parentForm != null)
|
|
{
|
|
parentForm.KeyPreview = true;
|
|
parentForm.KeyDown += ParentForm_KeyDown;
|
|
parentForm.KeyUp += ParentForm_KeyUp;
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
private void DataGridViewEx_CellContentClick(object sender, DataGridViewCellEventArgs e)
|
|
{
|
|
if (e.ColumnIndex < 0)
|
|
return;
|
|
|
|
var chkCol = this.Columns.Cast<DataGridViewColumn>().FirstOrDefault(c => c is DataGridViewCheckBoxColumn);
|
|
if (chkCol == null)
|
|
return;
|
|
var colIndex = chkCol.Index;
|
|
if (colIndex < 0 || e.ColumnIndex != colIndex)
|
|
return;
|
|
|
|
rowIndex1 = rowIndex2;
|
|
rowIndex2 = e.RowIndex;
|
|
if (IsShiftDown && MultiSelectCheckBox)
|
|
{
|
|
if (rowIndex1 > -1 && rowIndex2 > -1)
|
|
{
|
|
try
|
|
{
|
|
if (rowIndex1 > rowIndex2)
|
|
{
|
|
for (int i = rowIndex2; i <= rowIndex1 - 1; i++)
|
|
{
|
|
this.Rows[i].Cells[colIndex].Value = this.Rows[rowIndex1].Cells[colIndex].Value;
|
|
}
|
|
if (this.Rows[rowIndex2].Cells[colIndex].Value == this.Rows[rowIndex1].Cells[colIndex].Value && rowIndex1 != rowIndex2)
|
|
this.Rows[rowIndex2].Cells[colIndex].Value = !this.Rows[rowIndex2].Cells[colIndex].Value.ParseToBool();
|
|
}
|
|
else
|
|
{
|
|
for (int i = rowIndex1; i <= rowIndex2 - 1; i++)
|
|
{
|
|
this.Rows[i].Cells[colIndex].Value = this.Rows[rowIndex1].Cells[colIndex].Value;
|
|
}
|
|
if (this.Rows[rowIndex2].Cells[colIndex].Value == this.Rows[rowIndex1].Cells[colIndex].Value && rowIndex1 != rowIndex2)
|
|
this.Rows[rowIndex2].Cells[colIndex].Value = !this.Rows[rowIndex2].Cells[colIndex].Value.ParseToBool();
|
|
}
|
|
//this.ClearSelection();
|
|
//this.SetSelectedCellCore(colIndex, rowIndex2, true);
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
//rowIndex1 = -1;
|
|
//rowIndex2 = -1;
|
|
}
|
|
}
|
|
}
|
|
|
|
int rowIndex1 = -1;
|
|
int rowIndex2 = -1;
|
|
bool IsShiftDown = false;
|
|
private void ParentForm_KeyDown(object sender, KeyEventArgs e)
|
|
{
|
|
switch (e.KeyCode)
|
|
{
|
|
case Keys.Shift:
|
|
case Keys.ShiftKey:
|
|
case Keys.LShiftKey:
|
|
case Keys.RShiftKey:
|
|
IsShiftDown = true;
|
|
break;
|
|
}
|
|
}
|
|
private void ParentForm_KeyUp(object sender, KeyEventArgs e)
|
|
{
|
|
switch (e.KeyCode)
|
|
{
|
|
case Keys.Shift:
|
|
case Keys.ShiftKey:
|
|
case Keys.LShiftKey:
|
|
case Keys.RShiftKey:
|
|
IsShiftDown = false;
|
|
rowIndex1 = -1;
|
|
rowIndex2 = -1;
|
|
break;
|
|
}
|
|
}
|
|
|
|
private Color _BorderColor = Color.FromArgb(63, 126, 255);
|
|
/// <summary>
|
|
/// 边框颜色
|
|
/// </summary>
|
|
[Description("边框颜色"), Category("UserProperty")]
|
|
public Color BorderColor
|
|
{
|
|
get
|
|
{
|
|
return _BorderColor;
|
|
}
|
|
set
|
|
{
|
|
if (_BorderColor != value)
|
|
{
|
|
_BorderColor = value;
|
|
this.Invalidate();
|
|
}
|
|
}
|
|
}
|
|
|
|
private bool autoRowNumber = true;
|
|
/// <summary>
|
|
/// 边框颜色
|
|
/// </summary>
|
|
[Description("自动行号"), Category("UserProperty")]
|
|
public bool AutoRowNumber
|
|
{
|
|
get
|
|
{
|
|
return autoRowNumber;
|
|
}
|
|
set
|
|
{
|
|
if (autoRowNumber != value)
|
|
{
|
|
autoRowNumber = value;
|
|
this.Invalidate();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 自动生成行号
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void DataGridViewEx_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
|
|
{
|
|
if (AutoRowNumber)
|
|
{
|
|
Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, RowHeadersWidth - 4, e.RowBounds.Height);
|
|
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), RowHeadersDefaultCellStyle.Font, rectangle, Color.DarkGray, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 绘制边框颜色
|
|
/// </summary>
|
|
/// <param name="e"></param>
|
|
protected override void OnPaint(PaintEventArgs e)
|
|
{
|
|
base.OnPaint(e);
|
|
|
|
e.Graphics.DrawRectangle(new Pen(this.BackgroundColor), new Rectangle(1, 1, this.Width - 2, this.Height - 2));
|
|
e.Graphics.DrawRectangle(new Pen(this.BorderColor), new Rectangle(0, 0, this.Width - 1, this.Height - 1));
|
|
}
|
|
|
|
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
|
|
{
|
|
//if (e.ColumnIndex == yourCheckBoxColumnIndex && e.RowIndex >= 0)
|
|
//{
|
|
// bool isChecked = (bool)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
|
|
// Rectangle checkBoxBounds = new Rectangle(e.CellBounds.Location.X + 10, e.CellBounds.Location.Y + (e.CellBounds.Height - 20) / 2, 20, 20); // 自定义大小和位置
|
|
// ControlPaint.DrawCheckBox(e.Graphics, checkBoxBounds, DataGridViewState.Default, isChecked); // 绘制复选框
|
|
// e.Handled = true; // 阻止默认绘制
|
|
//}
|
|
}
|
|
|
|
|
|
private int checkBoxSize = 12;
|
|
public Color CheckBoxColor = Color.White;
|
|
public Color CheckBoxRowBoxCheckColor = Color.Blue;
|
|
public Color CheckBoxRowBoxBorderColor = Color.Blue;
|
|
|
|
private void DataGridViewEx_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
|
|
{
|
|
if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
|
|
{
|
|
var cellTmp = this.Rows[e.RowIndex].Cells[e.ColumnIndex];
|
|
if (cellTmp is DataGridViewCheckBoxCell)
|
|
{
|
|
e.PaintBackground(e.CellBounds, true);
|
|
|
|
bool isChecked = cellTmp.Value.ParseToBool();
|
|
var locationX = e.CellBounds.Location.X + e.CellBounds.Width / 2 - checkBoxSize / 2 - 1;
|
|
var locationY = e.CellBounds.Location.Y + e.CellBounds.Height / 2 - checkBoxSize / 2;
|
|
Rectangle checkBoxBounds = new Rectangle(locationX, locationY, checkBoxSize, checkBoxSize); // 自定义大小和位置
|
|
//ControlPaint.DrawCheckBox(e.Graphics, checkBoxBounds, isChecked ? ButtonState.Checked : ButtonState.Normal); // 绘制复选框
|
|
UcCheckBox.DrawCheckBoxIcon(e.Graphics, checkBoxBounds, isChecked, CheckBoxColor, CheckBoxRowBoxCheckColor, CheckBoxRowBoxBorderColor);
|
|
|
|
e.Handled = true; // 阻止默认绘制
|
|
}
|
|
}
|
|
}
|
|
|
|
private bool multiSelectCheckBox = true;
|
|
/// <summary>
|
|
/// 选择框批量选中(使用Shift按键)
|
|
/// </summary>
|
|
[Description("选择框批量选中(Shift)"), Category("UserProperty")]
|
|
public bool MultiSelectCheckBox
|
|
{
|
|
get
|
|
{
|
|
return multiSelectCheckBox;
|
|
}
|
|
set
|
|
{
|
|
if (multiSelectCheckBox != value)
|
|
{
|
|
multiSelectCheckBox = value;
|
|
this.Invalidate();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|