■ DiagramControl 클래스의 CustomDrawItem 이벤트를 사용해 다이어그램 항목 내 커스텀 아이콘을 그리는 방법을 보여준다.
▶ Status.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
namespace TestProject { /// <summary> /// 상태 /// </summary> public enum Status { /// <summary> /// 활성 /// </summary> Active, /// <summary> /// 비활성 /// </summary> Inactive } } |
▶ CustomDiagramShape.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
using System.ComponentModel; using DevExpress.Utils.Serializing; using DevExpress.XtraDiagram; namespace TestProject { /// <summary> /// 커스텀 다이어그램 도형 /// </summary> public class CustomDiagramShape : DiagramShape { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 상태 - Status /// <summary> /// 상태 /// </summary> [Category("Info")] [XtraSerializableProperty] public Status Status { get; set; } #endregion } } |
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
using System.ComponentModel; using System.Drawing; using DevExpress.Diagram.Core; using DevExpress.Images; using DevExpress.XtraBars.Ribbon; using DevExpress.XtraDiagram; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : RibbonForm { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); DiagramControl.ItemTypeRegistrator.Register(typeof(CustomDiagramShape)); DiagramStencil stencil = new DiagramStencil("customShapes", "Custom Shapes"); stencil.RegisterTool ( new FactoryItemTool ( "activeTaskShape", () => "Active Task", diagram => new CustomDiagramShape { Content = "Active Task", Status = Status.Active }, new System.Windows.Size(150, 100), false ) ); stencil.RegisterTool ( new FactoryItemTool ( "inactiveTaskShape", () => "Inactive Task", diagram => new CustomDiagramShape { Content = "Inactive Task", Status = Status.Inactive }, new System.Windows.Size(150, 100), false ) ); DiagramToolboxRegistrator.RegisterStencil(stencil); this.diagramControl.OptionsBehavior.SelectedStencils = StencilCollection.Parse("customShapes"); this.diagramControl.CreateRibbon(); this.diagramControl.CreateDocking(); this.diagramControl.CustomGetEditableItemProperties += diagramControl_CustomGetEditableItemProperties; this.diagramControl.CustomDrawItem += diagramControl_CustomDrawItem; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 다이어그램 컨트롤 편집 가능한 항목 속성 커스텀 구하기 - diagramControl_CustomGetEditableItemProperties(sender, e) /// <summary> /// 다이어그램 컨트롤 편집 가능한 항목 속성 커스텀 구하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void diagramControl_CustomGetEditableItemProperties(object sender, DiagramCustomGetEditableItemPropertiesEventArgs e) { e.Properties.Add(TypeDescriptor.GetProperties(typeof(CustomDiagramShape))["Status"]); } #endregion #region 다이어그램 컨트롤 항목 커스텀 그리기 - diagramControl_CustomDrawItem(sender, e) /// <summary> /// 다이어그램 컨트롤 항목 커스텀 그리기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void diagramControl_CustomDrawItem(object sender, CustomDrawItemEventArgs e) { CustomDiagramShape shape = e.Item as CustomDiagramShape; if(shape == null) { return; } e.DefaultDraw(); string imageFilePath = shape.Status == Status.Active ? "images/actions/apply_16x16.png" : "images/actions/cancel_16x16.png"; Image image = ImageResourceCache.Default.GetImage(imageFilePath); float margin = 3f; e.Graphics.DrawImage ( image, new RectangleF ( shape.Width - image.Width - margin, shape.Height - image.Height - margin, image.Width, image.Height ) ); e.Handled = true; } #endregion } } |