1.当C1FlexGrid为树模式的时候,实现和winform的TreeView控件那样自由拖拽。
2.关键在于 这个三个 事件MouseDown;MouseMove;MouseUp;
public partial class Form6 : Form
{
//Dragging code
private DRAGINFO m_DragInfo;
private bool _allowDragging; //form property
private const int DRAGTOL = 5; // mouse movement before dragging starts
public bool AllowDragging
{
get
{
return this._allowDragging;
}
set
{
this._allowDragging = value;
}
}
public Form6()
{
InitializeComponent();
flex.MouseDown += this.flex_MouseDown;
flex.MouseMove += this.flex_MouseMove;
flex.MouseUp += this.flex_MouseUp;
}
protected override void OnLoad(EventArgs e)
{
// initialize grid
// layout
flex.Rows.Count = 1;
flex.Cols.Count = 2;
flex.Cols.Fixed = 0;
flex[0, 0] = "File";
flex[0, 1] = "Modified Date";
//c1FlexGrid1.Cols1.Format = "c";
// styles
CellStyle cs = flex.Styles.Normal;
cs.Border.Direction = BorderDirEnum.Vertical;
cs.TextAlign = TextAlignEnum.LeftCenter;
cs.WordWrap = false;
cs = flex.Styles.Add("Data");
//cs.BackColor = Color.FromArgb(234, 236, 245);
cs.Border.Direction = BorderDirEnum.Horizontal;
cs.ForeColor = SystemColors.InfoText;
cs = flex.Styles.Add("SourceNode");
//cs.BackColor = Color.FromArgb(255, 213, 141);
cs.Font = new Font(flex.Font, FontStyle.Bold);
// outline tree
flex.Tree.Column = 0;
flex.Tree.Style = TreeStyleFlags.Simple;
flex.Tree.LineStyle = System.Drawing.Drawing2D.DashStyle.Solid;
flex.AllowMerging = AllowMergingEnum.Nodes;
// other
flex.AllowResizing = AllowResizingEnum.Columns;
flex.SelectionMode = SelectionModeEnum.Cell;
flex.HighLight = HighLightEnum.Always;
flex.FocusRect = FocusRectEnum.Solid;
flex.AllowSorting = AllowSortingEnum.None;
_allowDragging = true;
BuildTree();
base.OnLoad(e);
}
private void BuildTree()
{
C1.Win.C1FlexGrid.CellStyle cs = flex.Styles.Add("SourceNode");
cs.Font = new System.Drawing.Font("微软雅黑", 10);
cs.ForeColor = Color.DarkBlue;
flex.AddItem("西安葡萄城控件");
C1.Win.C1FlexGrid.Row row = flex.Rows[flex.Rows.Count - 1];
row.IsNode = true;
row.Style = flex.Styles["SourceNode"];
C1.Win.C1FlexGrid.Node nd = row.Node;
nd.Level = 0;
nd.Image = Image.FromFile("1.png");
flex.AddItem("产品部");
row = flex.Rows[flex.Rows.Count - 1];
row.IsNode = true;
row.Style = flex.Styles["SourceNode"];
nd = row.Node;
nd.Level = 1;
nd.Image = Image.FromFile("2.png");
//flex.AddItem("ComponentOne 产品部");
//flex.AddItem("Spread 产品部");
//flex.AddItem("ActiveReports 产品部");
//flex.AddItem("TX TextControl 产品部");
flex.AddItem("市场部");
row = flex.Rows[flex.Rows.Count - 1];
row.IsNode = true;
row.Style = flex.Styles["SourceNode"];
nd = row.Node;
nd.Level = 2;
nd.Image = Image.FromFile("2.png");
//flex.AddItem("ComponentOne 市场部");
//flex.AddItem("Spread 市场部");
//flex.AddItem("ActiveReports 市场部");
//flex.AddItem("TX TextControl 市场部");
flex.AddItem("销售部");
row = flex.Rows[flex.Rows.Count - 1];
row.IsNode = true;
row.Style = flex.Styles["SourceNode"];
nd = row.Node;
nd.Level = 3;
nd.Image = Image.FromFile("2.png");
//flex.AddItem("ComponentOne 销售部");
//flex.AddItem("Spread 销售部");
//flex.AddItem("ActiveReports 销售部");
//flex.AddItem("TX TextControl 销售部");
flex.AddItem("服务部");
row = flex.Rows[flex.Rows.Count - 1];
row.IsNode = true;
row.Style = flex.Styles["SourceNode"];
nd = row.Node;
nd.Level = 1;
nd.Image = Image.FromFile("2.png");
//flex.AddItem("ComponentOne 服务部");
//flex.AddItem("Spread 服务部");
//flex.AddItem("ActiveReports 服务部");
flex.AddItem("TX TextControl jn服务部");
row = flex.Rows[flex.Rows.Count - 1];
row.IsNode = true;
row.Style = flex.Styles["SourceNode"];
nd = row.Node;
nd.Level = 1;
nd.Image = Image.FromFile("2.png");
}
private void flex_MouseDown(object sender, MouseEventArgs e)
{
m_DragInfo.checkDrag = false;
// left button, no shift: start tracking mouse to drag
if (e.Button != MouseButtons.Left) return;
if (!this._allowDragging || m_DragInfo.dragging) return;
if (flex.MouseRow < flex.Rows.Fixed || flex.MouseCol != 0) return;
// save current row and mouse position
m_DragInfo.row = flex.Row;
m_DragInfo.mouseDown = new Point(e.X, e.Y);
// start checking
m_DragInfo.checkDrag = true;
}
private void flex_MouseMove(object sender, MouseEventArgs e)
{
// if checking and the user moved past our tolerance, start dragging
if (!m_DragInfo.checkDrag || e.Button != MouseButtons.Left) return;
if (Math.Abs(e.X - m_DragInfo.mouseDown.X) +
Math.Abs(e.Y - m_DragInfo.mouseDown.Y) <= DRAGTOL) return;
// update flags
m_DragInfo.dragging = true;
// set cursor and highlight node
CellStyle cs = flex.Styles["SourceNode"];
flex.Cursor = Cursors.PanSouth;
flex.SetCellStyle(m_DragInfo.row, 0, cs);
// check whether we can drop here
Cursor c = (NoDropHere()) ? Cursors.No : Cursors.PanSouth;
if (c != flex.Cursor) flex.Cursor = c;
}
private bool NoDropHere()
{
if (flex.MouseRow < flex.Rows.Fixed) return true;
if (flex.MouseCol < flex.Cols.Fixed) return true;
if (flex.GetDataDisplay(flex.Row, 0) == "SKU") return true;
//if (flex.Rows[flex.MouseRow].Node.Children == 0) return true;
return false;
}
private void flex_MouseUp(object sender, MouseEventArgs e)
{
// we're not checking until the mouse goes down again
m_DragInfo.checkDrag = false;
// not dragging? we're done
if (!m_DragInfo.dragging) return;
// stop dragging
m_DragInfo.dragging = false;
flex.SetCellStyle(m_DragInfo.row, 0, (CellStyle)null);
flex.Cursor = Cursors.Default;
// test whether the drop is allowed
if (NoDropHere()) return;
// move node into new parent node
Node ndSrc = flex.Rows[m_DragInfo.row].Node;
Node ndDst = flex.Rows[flex.Row].Node;
ndSrc.Move(NodeMoveEnum.ChildOf, ndDst);
ndSrc.Select();
//UpdateIcons();
}
// to handle node dragging
internal struct DRAGINFO
{
public bool dragging; // currently dragging
public bool checkDrag; // currently checking mouse to start dragging
public int row; // index of row being dragged
public Point mouseDown; // mouse down position
}
}