using System; using System.Collections.Generic; using System.Linq; using System.Text; using DevExpress.XtraTreeList; using System.Drawing; using System.Windows.Forms; using DevExpress.XtraTreeList.Nodes; using System.ComponentModel; namespace WMS.Ctrl.DevExpressCustom { public class TreeListCustom : TreeList { #region 定义控件 private System.Windows.Forms.ContextMenuStrip GridControl_ContextMenu1; private System.Windows.Forms.ToolStripMenuItem tCut; private System.Windows.Forms.ToolStripMenuItem tCopy; private System.ComponentModel.IContainer components; private System.Windows.Forms.ToolStripMenuItem tPaste; #endregion #region 定义变量 private Bitmap _Image = null; private Boolean showRowID = true; private int indicatorRowWidth = 40; private ToolStripSeparator toolStripSeparator1; private ToolStripMenuItem tExpandAll; private ToolStripMenuItem tCollapseAll; private bool enableAppearanceEvenRow = true;//是否允许隔行显示 #endregion #region 自定义属性 /// /// 是否显示行号 /// [Category("自定义属性"), Description("显示行号")] public Boolean ShowRowID { get { return showRowID; } set { showRowID = value; } } /// /// 默认行号宽度 /// [Category("自定义属性"), Description("行号宽度")] public int IndicatorRowWidth { get { return indicatorRowWidth; } set { indicatorRowWidth = value; } } /// /// 是否允许隔行显示 /// [Category("自定义属性"), Description("是否允许隔行显示")] public bool EnableAppearanceEvenRow { get { return enableAppearanceEvenRow; } set { enableAppearanceEvenRow = value; this.Invalidate(); } } #endregion #region 构造函数 /// /// 构造函数 /// public TreeListCustom() { InitializeComponent(); //列表行号显示 if (ShowRowID) { this.IndicatorWidth = IndicatorRowWidth; this.CustomDrawNodeIndicator -= new DevExpress.XtraTreeList.CustomDrawNodeIndicatorEventHandler(this.Tree_CustomDrawNodeIndicator); this.CustomDrawNodeIndicator += new DevExpress.XtraTreeList.CustomDrawNodeIndicatorEventHandler(this.Tree_CustomDrawNodeIndicator); } //隔行色显示 if (EnableAppearanceEvenRow) { this.OptionsView.EnableAppearanceEvenRow = true; this.Appearance.EvenRow.BackColor = Color.FromArgb(240, 240, 250); } //焦点行颜色 this.Appearance.FocusedRow.BackColor = Color.LightSteelBlue; } /// /// 构造控件 /// private void InitializeComponent() { this.GridControl_ContextMenu1 = new System.Windows.Forms.ContextMenuStrip(); this.tCut = new System.Windows.Forms.ToolStripMenuItem(); this.tCopy = new System.Windows.Forms.ToolStripMenuItem(); this.tPaste = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator(); this.tExpandAll = new System.Windows.Forms.ToolStripMenuItem(); this.tCollapseAll = new System.Windows.Forms.ToolStripMenuItem(); this.GridControl_ContextMenu1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this)).BeginInit(); this.SuspendLayout(); // // GridControl_ContextMenu1 // this.GridControl_ContextMenu1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.tCut, this.tCopy, this.tPaste, this.toolStripSeparator1, this.tExpandAll, this.tCollapseAll}); this.GridControl_ContextMenu1.Name = "GridControl_ContextMenu11"; this.GridControl_ContextMenu1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System; this.GridControl_ContextMenu1.Size = new System.Drawing.Size(146, 120); // // tCut // this.tCut.Enabled = false; this.tCut.Name = "tCut"; this.tCut.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.T))); this.tCut.Size = new System.Drawing.Size(145, 22); this.tCut.Text = "剪切"; this.tCut.Click += new System.EventHandler(this.tCut_Click); // // tCopy // this.tCopy.Name = "tCopy"; this.tCopy.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.C))); this.tCopy.Size = new System.Drawing.Size(145, 22); this.tCopy.Text = "复制"; this.tCopy.Click += new System.EventHandler(this.tCopy_Click); // // tPaste // this.tPaste.Enabled = false; this.tPaste.Name = "tPaste"; this.tPaste.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.V))); this.tPaste.Size = new System.Drawing.Size(145, 22); this.tPaste.Text = "粘贴"; this.tPaste.Click += new System.EventHandler(this.tPaste_Click); // // toolStripSeparator1 // this.toolStripSeparator1.Name = "toolStripSeparator1"; this.toolStripSeparator1.Size = new System.Drawing.Size(142, 6); // // tExpandAll // this.tExpandAll.Image = global::WMS.Ctrl.Properties.Resources.Book_openHS; this.tExpandAll.Name = "tExpandAll"; this.tExpandAll.Size = new System.Drawing.Size(145, 22); this.tExpandAll.Text = "全展开"; this.tExpandAll.Click += new System.EventHandler(this.tExpandAll_Click); // // tCollapseAll // this.tCollapseAll.Image = global::WMS.Ctrl.Properties.Resources.Book_angleHS; this.tCollapseAll.Name = "tCollapseAll"; this.tCollapseAll.Size = new System.Drawing.Size(145, 22); this.tCollapseAll.Text = "全收缩"; this.tCollapseAll.Click += new System.EventHandler(this.tCollapseAll_Click); // // TreeListCustom // this.ContextMenuStrip = this.GridControl_ContextMenu1; this.GridControl_ContextMenu1.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this)).EndInit(); this.ResumeLayout(false); } #endregion #region 创建TreeList节点信息 /// /// 创建TreeList节点信息 /// /// /// 实体对象 /// public object[] CreateNodeData(T entity) { try { object[] nodeData = new object[this.Columns.Count]; // ListOperate myListOperate = new ListOperate(); for (int col = 0; col < this.Columns.Count; col++) { string fieldName = this.Columns[col].FieldName; // nodeData[col] = myListOperate.GetValue(entity, fieldName); } return nodeData; } catch (Exception ex) { throw (ex); } } #endregion #region 右键菜单 /// /// 复制 /// /// /// private void tCopy_Click(object sender, EventArgs e) { try { object value = this.FocusedNode.GetValue(this.FocusedColumn); string strValue = ""; if (value != null) { strValue = value.ToString(); } Clipboard.SetText(strValue); } catch (Exception) { } } /// /// 剪切 /// /// /// private void tCut_Click(object sender, EventArgs e) { try { } catch (Exception) { } } /// /// 粘贴 /// /// /// private void tPaste_Click(object sender, EventArgs e) { } /// /// 全展开 /// /// /// private void tExpandAll_Click(object sender, EventArgs e) { try { if (this.Nodes.Count > 0) { this.Nodes[0].ExpandAll(); } } catch (Exception) { } } /// /// 全收缩 /// /// /// private void tCollapseAll_Click(object sender, EventArgs e) { try { this.CollapseAll(); if (this.Nodes.Count > 0) { this.Nodes[0].Expanded = true; } } catch (Exception) { } } #endregion #region 控件事件 /// /// 行号事件 /// /// /// private void Tree_CustomDrawNodeIndicator(object sender, CustomDrawNodeIndicatorEventArgs e) { try { WMS.Ctrl.DevExpressCustom.TreeListCustom tmpTree = new WMS.Ctrl.DevExpressCustom.TreeListCustom(); DevExpress.Utils.Drawing.IndicatorObjectInfoArgs args = e.ObjectArgs as DevExpress.Utils.Drawing.IndicatorObjectInfoArgs; if (args != null) { int rowNum = tmpTree.GetVisibleIndexByNode(e.Node) + 1; args.DisplayText = rowNum.ToString(); } e.ImageIndex = -1; } catch { } } #endregion #region 显示导出对话框 /// /// 显示导出对话框 /// public void ShowExportDialog() { try { System.Windows.Forms.SaveFileDialog fileSave = new System.Windows.Forms.SaveFileDialog(); fileSave.InitialDirectory = System.Windows.Forms.Application.StartupPath; fileSave.Filter = "Excel文件(*.XLS)|*.xls|Excel文件(2007/2010)(*.XLSX)|*.xlsx|文本文件(*.TXT)|*.txt|CSV文件(*.CSV)|*.csv|Pdf文件(*.Pdf)|*.pdf|Html文件(*.Html)|*.html"; fileSave.RestoreDirectory = true; fileSave.FilterIndex = 1; if (fileSave.ShowDialog() == System.Windows.Forms.DialogResult.OK) { DevExpress.XtraPrinting.ExportTarget ls_type = DevExpress.XtraPrinting.ExportTarget.Xls; string fName = fileSave.FileName; if (fileSave.FileName.ToLower().EndsWith(".txt")) { ls_type = DevExpress.XtraPrinting.ExportTarget.Text; } else if (fileSave.FileName.ToLower().EndsWith(".xls")) { ls_type = DevExpress.XtraPrinting.ExportTarget.Xls; } else if (fileSave.FileName.ToLower().EndsWith(".xlsx")) { ls_type = DevExpress.XtraPrinting.ExportTarget.Xlsx; } else if (fileSave.FileName.ToLower().EndsWith(".csv")) { ls_type = DevExpress.XtraPrinting.ExportTarget.Csv; } else if (fileSave.FileName.ToLower().EndsWith(".html")) { ls_type = DevExpress.XtraPrinting.ExportTarget.Html; } else if (fileSave.FileName.ToLower().EndsWith(".Pdf")) { ls_type = DevExpress.XtraPrinting.ExportTarget.Pdf; } this.ExpandAll(); this.Export(ls_type, fName); } } catch (Exception ex) { } } #endregion } }