You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

261 lines
15 KiB
C#

1 year ago
<EFBFBD><EFBFBD>using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
namespace Mesnac.Controls.Default
{
public partial class MultiColHeaderDgv : DataGridView
{
#region W[<EFBFBD>k<EFBFBD>[<EFBFBD>
/// <summary>Y<EFBFBD>}RjL<EFBFBD><EFBFBD>v9jP}<EFBFBD>i
///
/// </summary>
private TreeView _ColHeaderTreeView;
/// <summary>9j<EFBFBD>vg'Yd\xe
///
/// </summary>
public int iNodeLevels;
/// <summary>N<EFBFBD>}RjL<EFBFBD><EFBFBD>vؚ<EFBFBD>^
///
/// </summary>
private int iCellHeight;
/// <summary>@b gI<EFBFBD><EFBFBD>{ޞ
///
/// </summary>
public IList<TreeNode> ColLists = new List<TreeNode>();
#endregion
#region l\'`<EFBFBD>[<EFBFBD>
/// <summary>Y<EFBFBD>}RjL<EFBFBD><EFBFBD>v9jP}<EFBFBD>i
///
/// </summary>
[
Description("Y<>}RjL<6A><4C>v9jP}<7D>i")
]
public TreeView myColHeaderTreeView
{
get { return _ColHeaderTreeView; }
set { _ColHeaderTreeView = value; }
}
#endregion
#region <EFBFBD>e<EFBFBD>l<EFBFBD>Qxe
/// <summary>^<EFBFBD>xk<EFBFBD><EFBFBD>{9jg'Yd\xe <EFBFBD>&N<EFBFBD>OX[@b gI<EFBFBD><EFBFBD>{ޞ
///
/// </summary>
/// <param name="tnc"></param>
/// <returns></returns>
private int myGetNodeLevels(TreeNodeCollection tnc)
{
if (tnc == null) return 0;
foreach (TreeNode tn in tnc)
{
if ((tn.Level + 1) > iNodeLevels)//tn.Level/f<EFBFBD>_0<EFBFBD><EFBFBD><EFBFBD>Y<EFBFBD>v
{
iNodeLevels = tn.Level+1;
}
if (tn.Nodes.Count > 0)
{
myGetNodeLevels(tn.Nodes);
}
else
{
ColLists.Add(tn);//I<EFBFBD><EFBFBD>{ޞ
}
}
return iNodeLevels;
}
/// <summary><EFBFBD><EFBFBD>(u^<EFBFBD>xkBlg'Yd\xe0R-<EFBFBD>=~ؚ
///
/// </summary>
public void myNodeLevels()
{
iNodeLevels = 1;//R<EFBFBD>Y<EFBFBD>p1
ColLists.Clear();
int iNodeDeep = myGetNodeLevels(_ColHeaderTreeView.Nodes);
iCellHeight = this.ColumnHeadersHeight;
this.ColumnHeadersHeight = this.ColumnHeadersHeight * iNodeDeep;//R-<EFBFBD>=~ؚ=N<EFBFBD>}Rؚ*d\xe
}
/// <summary><EFBFBD><EFBFBD><EFBFBD>_Tv^h<EFBFBD><EFBFBD>W[<EFBFBD>k<EFBFBD>v<EFBFBD>[<EFBFBD>^
///
/// </summary>
/// <param name="node">W[<EFBFBD>k<EFBFBD><EFBFBD><EFBFBD>p</param>
/// <returns>W[<EFBFBD>k<EFBFBD>[<EFBFBD>^</returns>
private int GetUnitHeaderWidth(TreeNode node)
{
int uhWidth = 0;
//<EFBFBD><EFBFBD><EFBFBD>_g<EFBFBD>^B\W[<EFBFBD>k<EFBFBD>v<EFBFBD>[<EFBFBD>^
if (node.Nodes == null)
return this.Columns[GetColumnListNodeIndex(node)].Width;
if (node.Nodes.Count == 0)
return this.Columns[GetColumnListNodeIndex(node)].Width;
//<EFBFBD><EFBFBD><EFBFBD>_^<EFBFBD>g<EFBFBD>^B\W[<EFBFBD>k<EFBFBD>v<EFBFBD>[<EFBFBD>^
for (int i = 0; i <= node.Nodes.Count - 1; i++)
{
uhWidth = uhWidth + GetUnitHeaderWidth(node.Nodes[i]);
}
return uhWidth;
}
/// <summary><EFBFBD><EFBFBD><EFBFBD>_<EFBFBD>^B\W[<EFBFBD>k"}_
///
/// </summary>
///' <param name="node"><EFBFBD>^B\W[<EFBFBD>k<EFBFBD><EFBFBD><EFBFBD>p</param>
/// <returns>"}_</returns>
/// <remarks></remarks>
private int GetColumnListNodeIndex(TreeNode node)
{
for (int i = 0; i <= ColLists.Count - 1; i++)
{
if (ColLists[i].Equals(node))
return i;
}
return -1;
}
///<summary><EFBFBD>~6RTv^h<EFBFBD>4Y
///
///</summary>
///<param name="node">Tv^h<EFBFBD>4Y<EFBFBD><EFBFBD><EFBFBD>p</param>
///<param name="e"><EFBFBD>~<EFBFBD>V<EFBFBD>SpeƖ</param>
///<param name="level"><EFBFBD>~<EFBFBD>p<EFBFBD>m<EFBFBD>^</param>
///<remarks></remarks>
public void PaintUnitHeader(
TreeNode node,
System.Windows.Forms.DataGridViewCellPaintingEventArgs e,
int level)
{
//9h<EFBFBD><EFBFBD><EFBFBD>p<EFBFBD>e<EFBFBD><EFBFBD>Q<EFBFBD>R_<EFBFBD>(u
if (level == 0)
return;
RectangleF uhRectangle;
int uhWidth;
SolidBrush gridBrush = new SolidBrush(this.GridColor);
SolidBrush backColorBrush = new SolidBrush(e.CellStyle.BackColor);
Pen gridLinePen = new Pen(gridBrush);
StringFormat textFormat = new StringFormat();
textFormat.Alignment = StringAlignment.Center;
uhWidth = GetUnitHeaderWidth(node);
//N<EFBFBD>S4<EFBFBD><EFBFBD>{<EFBFBD>l g@b:S+R(Wُ0
if (node.Nodes.Count == 0)
{
uhRectangle = new Rectangle(e.CellBounds.Left,
e.CellBounds.Top + node.Level * iCellHeight,
uhWidth - 1,
iCellHeight * (iNodeLevels - node.Level) - 1);
}
else
{
uhRectangle = new Rectangle(
e.CellBounds.Left,
e.CellBounds.Top + node.Level * iCellHeight,
uhWidth - 1,
iCellHeight - 1);
}
//;u<EFBFBD>wb_
e.Graphics.FillRectangle(backColorBrush, uhRectangle);
//R<EFBFBD>^<EFBFBD>~
e.Graphics.DrawLine(gridLinePen
, uhRectangle.Left
, uhRectangle.Bottom
, uhRectangle.Right
, uhRectangle.Bottom);
//R<EFBFBD>S<EFBFBD>z<EFBFBD>~
e.Graphics.DrawLine(gridLinePen
, uhRectangle.Right
, uhRectangle.Top
, uhRectangle.Right
, uhRectangle.Bottom);
e.Graphics.DrawString(node.Text, this.ColumnHeadersDefaultCellStyle.Font
, Brushes.Black
, uhRectangle.Left + uhRectangle.Width / 2 -
e.Graphics.MeasureString(node.Text, this.ColumnHeadersDefaultCellStyle.Font).Width / 2 - 1
, uhRectangle.Top +
uhRectangle.Height / 2 - e.Graphics.MeasureString(node.Text, this.ColumnHeadersDefaultCellStyle.Font).Height / 2);
//<EFBFBD>R_<EFBFBD>(u()
if (node.PrevNode == null)
if (node.Parent != null)
PaintUnitHeader(node.Parent, e, level - 1);
}
#endregion
//͑<EFBFBD>[h<EFBFBD>-<EFBFBD>
protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
{
try
{
//L<EFBFBD>h<EFBFBD><EFBFBD> N͑<EFBFBD>Q
if (e.ColumnIndex < 0)
{
base.OnCellPainting(e);
return;
}
if (iNodeLevels == 1)
{
base.OnCellPainting(e);
return;
}
//<EFBFBD>~6Rh<EFBFBD>4Y
if (e.RowIndex == -1)
{
if (_ColHeaderTreeView != null)
{
if (iNodeLevels == 0)
{
myNodeLevels();
}
PaintUnitHeader((TreeNode)this.ColLists[e.ColumnIndex], e, iNodeLevels);
e.Handled = true;
}
else
{
base.OnCellPainting(e);
}
}
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, "Error");
}
}
}
}