■ GridView 클래스에서 정렬/필터 아이콘을 커스텀 그리는 방법을 보여준다.
▶ 예제 코드 (C#)
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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
using System; using System.Drawing; using System.Windows.Forms; using DevExpress.Data; using DevExpress.Utils.Drawing; using DevExpress.XtraEditors; using DevExpress.XtraEditors.Drawing; using DevExpress.XtraGrid.Columns; using DevExpress.XtraGrid.Views.Grid; ... private GridView gridView; private CheckEdit drawCustomFilterCheckExit; private CheckEdit drawCustomSortCheckEdit; ... this.gridView.CustomDrawColumnHeader += gridView_CustomDrawColumnHeader; this.drawCustomSortCheckEdit.CheckedChanged += drawCustomCheckEdit_CheckedChanged; this.drawCustomFilterCheckExit.CheckedChanged += drawCustomCheckEdit_CheckedChanged; ... #region 커스텀 그리기 체크 에디트 체크 변경시 처리하기 - drawCustomCheckEdit_CheckedChanged(sender, e) /// <summary> /// 커스텀 그리기 체크 에디트 체크 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void drawCustomCheckEdit_CheckedChanged(object sender, EventArgs e) { this.gridView.InvalidateColumnHeader(null); } #endregion #region 그리드 뷰 컬럼 헤더 커스텀 그리기 - gridView_CustomDrawColumnHeader(sender, e) /// <summary> /// 그리드 뷰 컬럼 헤더 커스텀 그리기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void gridView_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e) { if(e.Column == null) { return; } ObjectInfoArgs objectInfoArgs = null; Rectangle filterRectangle; Rectangle sortRectangle; filterRectangle = Rectangle.Empty; sortRectangle = Rectangle.Empty; try { UpdateInnerElements ( e, false, ref sortRectangle, ref filterRectangle, ref objectInfoArgs, this.drawCustomSortCheckEdit.Checked, this.drawCustomFilterCheckExit.Checked ); e.Painter.DrawObject(e.Info); } finally { UpdateInnerElements ( e, true, ref sortRectangle, ref filterRectangle, ref objectInfoArgs, this.drawCustomSortCheckEdit.Checked, this.drawCustomFilterCheckExit.Checked ); } if(!sortRectangle.IsEmpty) { DrawCustomSort(e.Graphics, sortRectangle, e.Column.SortOrder, this.sortImageList); } if(!filterRectangle.IsEmpty && objectInfoArgs != null) { DrawCustomFilter(e.Graphics, e.Column, objectInfoArgs, this.filterImageList); } e.Handled = true; } #endregion #region 내부 엘리먼트들 업데이트 하기 - UpdateInnerElements(e, restore, sortRectangle, filterRectangle, objectInfoArgs, drawCustomSort, drawCustomFilter) /// <summary> /// 내부 엘리먼트들 업데이트 하기 /// </summary> /// <param name="e">이벤트 인자</param> /// <param name="restore">복구 여부</param> /// <param name="sortRectangle">정렬 사각형</param> /// <param name="filterRectangle">필터 사각형</param> /// <param name="objectInfoArgs">객체 정보 인자</param> /// <param name="drawCustomSort">커스텀 정렬 그리기 여부</param> /// <param name="drawCustomFilter">커스텀 필터 그리기 여부</param> private void UpdateInnerElements ( ColumnHeaderCustomDrawEventArgs e, bool restore, ref Rectangle sortRectangle, ref Rectangle filterRectangle, ref ObjectInfoArgs objectInfoArgs, bool drawCustomSort, bool drawCustomFilter ) { foreach(DrawElementInfo drawElementInfo in e.Info.InnerElements) { if(drawElementInfo.ElementPainter is SortedShapeObjectPainter) { if(drawCustomSort) { if(restore) { drawElementInfo.ElementInfo.Bounds = sortRectangle; } else { sortRectangle = drawElementInfo.ElementInfo.Bounds; drawElementInfo.ElementInfo.Bounds = Rectangle.Empty; } } } if(drawElementInfo.ElementInfo is GridFilterButtonInfoArgs) { if(drawCustomFilter) { if(restore) { drawElementInfo.ElementInfo.Bounds = filterRectangle; } else { objectInfoArgs = drawElementInfo.ElementInfo; filterRectangle = drawElementInfo.ElementInfo.Bounds; drawElementInfo.ElementInfo.Bounds = Rectangle.Empty; } } } } } #endregion #region 커스텀 정렬 그리기 - DrawCustomSort(graphics, sortRectangle, columnSortOrder, imageList) /// <summary> /// 커스텀 정렬 그리기 /// </summary> /// <param name="graphics">그래픽스</param> /// <param name="sortRectangle">정렬 사각형</param> /// <param name="columnSortOrder">컬럼 정렬 순서</param> /// <param name="imageList">이미지 리스트</param> private void DrawCustomSort ( Graphics graphics, Rectangle sortRectangle, ColumnSortOrder columnSortOrder, ImageList imageList ) { if(columnSortOrder == ColumnSortOrder.None) { return; } int imageIndex = 0; if(columnSortOrder == ColumnSortOrder.Descending) { imageIndex = 1; } graphics.DrawImageUnscaled ( imageList.Images[imageIndex], sortRectangle.X + (sortRectangle.Width - imageList.ImageSize.Width ) / 2, sortRectangle.Y + (sortRectangle.Height - imageList.ImageSize.Height) / 2 ); } #endregion #region 커스텀 필터 그리기 - DrawCustomFilter(graphics, gridColumn, objectInfoArgs, imageList) /// <summary> /// 커스텀 필터 그리기 /// </summary> /// <param name="graphics">그래픽스</param> /// <param name="gridColumn">그리드 컬럼</param> /// <param name="objectInfoArgs">객체 정보 인자</param> /// <param name="imageList">이미지 리스트</param> private void DrawCustomFilter ( Graphics graphics, GridColumn gridColumn, ObjectInfoArgs objectInfoArgs, ImageList imageList ) { int imageIndex = 0; if((objectInfoArgs.State & ObjectState.Hot) != 0) { imageIndex = 1; } if((objectInfoArgs.State & ObjectState.Pressed)!= 0) { imageIndex = 2; } if(gridColumn.FilterInfo.Type != ColumnFilterType.None) { imageIndex += 3; } graphics.DrawImageUnscaled ( imageList.Images[imageIndex], objectInfoArgs.Bounds.X + (objectInfoArgs.Bounds.Width - imageList.ImageSize.Width ) / 2, objectInfoArgs.Bounds.Y + (objectInfoArgs.Bounds.Height - imageList.ImageSize.Height) / 2 ); } #endregion |