[DEVEXPRESS/WINFORM] ListBoxControl 클래스 : DrawItem 이벤트를 사용해 커스텀 항목 그리기
DevExpress/WinForm 2020. 2. 23. 11:14■ ListBoxControl 클래스 : DrawItem 이벤트를 사용해 커스텀 항목 그리기
------------------------------------------------------------------------------------------------------------------------
▶ MainForm.cs
using System.Drawing; using System.Windows.Forms;
using DevExpress.XtraEditors;
namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : XtraForm { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary> /// 배경 브러시 1 /// </summary> private Brush backgroundBrush1;
/// <summary> /// 배경 브러시 2 /// </summary> private Brush backgroundBrush2;
/// <summary> /// 배경 브러시 3 /// </summary> private Brush backgroundBrush3;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainForm()
/// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent();
this.backgroundBrush1 = new SolidBrush(Color.FromArgb(224, 251, 254)); this.backgroundBrush2 = new SolidBrush(Color.FromArgb(198, 241, 249)); this.backgroundBrush3 = new SolidBrush(Color.FromArgb(253, 192, 47 ));
this.listBoxControl.Items.Add("Ancient Egypt" ); this.listBoxControl.Items.Add("Kingdom of Kerma" ); this.listBoxControl.Items.Add("Carthage" ); this.listBoxControl.Items.Add("Kingdom of Kush" ); this.listBoxControl.Items.Add("Kingdom of D'mt" ); this.listBoxControl.Items.Add("Ptolemaic Kingdom" ); this.listBoxControl.Items.Add("Kingdom of Numidia");
this.listBoxControl.DrawItem += listBoxControl_DrawItem; }
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private
#region 리스트 박스 컨트롤 항목 그리기 - listBoxControl_DrawItem(sender, e)
/// <summary> /// 리스트 박스 컨트롤 항목 그리기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void listBoxControl_DrawItem(object sender, ListBoxDrawItemEventArgs e) { string itemText = (sender as ListBoxControl).GetItemText(e.Index);
if((e.State & DrawItemState.Selected) != 0) { e.Cache.FillRectangle(backgroundBrush3, e.Bounds);
ControlPaint.DrawBorder3D(e.Graphics, e.Bounds);
e.Cache.DrawString ( itemText, new Font ( e.Appearance.Font.Name, e.Appearance.Font.Size, FontStyle.Bold ), new SolidBrush(Color.Black), e.Bounds, e.Appearance.GetStringFormat() );
e.Handled = true;
return; }
if(e.Index % 2 == 0) { e.Cache.FillRectangle(backgroundBrush1, e.Bounds); } else { e.Cache.FillRectangle(backgroundBrush2, e.Bounds); }
e.Cache.DrawString(itemText, e.Appearance.Font, new SolidBrush(Color.Black),
e.Bounds, e.Appearance.GetStringFormat());
e.Handled = true; }
#endregion } }
|
------------------------------------------------------------------------------------------------------------------------
댓글을 달아 주세요