215 lines
6.9 KiB
C#
215 lines
6.9 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);
|
|
|
|
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;
|
|
}
|
|
this.Rows[rowIndex1].Cells[rowIndex1].Value = !this.Rows[rowIndex1].Cells[rowIndex2].Value.ParseToBool();
|
|
}
|
|
else
|
|
{
|
|
for (int i = rowIndex1; i <= rowIndex2 - 1; i++)
|
|
{
|
|
this.Rows[i].Cells[colIndex].Value = this.Rows[rowIndex1].Cells[colIndex].Value;
|
|
}
|
|
this.Rows[rowIndex1].Cells[rowIndex2].Value = !this.Rows[rowIndex1].Cells[rowIndex1].Value.ParseToBool();
|
|
}
|
|
}
|
|
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 bool multiSelectCheckBox = true;
|
|
/// <summary>
|
|
/// 选择框批量选中(使用Shift按键)
|
|
/// </summary>
|
|
[Description("选择框批量选中(Shift)"), Category("UserProperty")]
|
|
public bool MultiSelectCheckBox
|
|
{
|
|
get
|
|
{
|
|
return multiSelectCheckBox;
|
|
}
|
|
set
|
|
{
|
|
if (multiSelectCheckBox != value)
|
|
{
|
|
multiSelectCheckBox = value;
|
|
this.Invalidate();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|