블로그 이미지
진연
3차원 CAD프로그램인 UGS에서 지원하는 API를 이용하여 프로그램하는 방법등을 소개하는 블로그입니다. 혹시 연락이 필요하신분은 youni7311@hanmail.net로 메일 보내주세요..

calendar

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
2008. 10. 7. 10:40 코드자료

안녕하세요
먼저 콤보박스를 상속받는 클래스를 하나 작성하시고..

//드로우 모드를 OwnerDrawVariable로 변경
DrawMode = DrawMode.OwnerDrawVariable;

OnMeasureItem , OnDrawItem 두개의 메서드를 재정의 하시면 되겄습니다.
한번 짜봤습니다.

     /// <summary>

    /// ImageCombo

    /// </summary>

    public class ImageCombo: ComboBox
    {
        ImageList imageList = null;
    
        public ImageList Images
        {
            get { return this.imageList; }
            set { this.imageList = value; }
        }

        public ImageCombo()
        {          
            DrawMode = DrawMode.OwnerDrawVariable;     
        }      
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMeasureItem(MeasureItemEventArgs e)
        {
            base.OnMeasureItem (e);
            e.ItemWidth = DropDownWidth + 24;
        }

         /// <summary>
        /// OnDrawItem 메서드의 오버라이드 입니다.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
        {  
            base.OnDrawItem(e);

            if(e.Index > -1)
            {
                string text = Items[e.Index].ToString();
                Image img = null;

                if(this.imageList != null)
                {
                    if(this.imageList.Images.Count > e.Index)
                    {
                        img = imageList.Images[e.Index];                   
                    }              
                }

                //선택한 상황이 아니라면
                if((e.State & DrawItemState.Focus)==0)
                {                  
                    using(SolidBrush backBrush = new SolidBrush(SystemColors.Window))
                    {

             e.Graphics.FillRectangle(backBrush, 
                  e.Bounds.X,e.Bounds.Y,e.Bounds.Width,e.Bounds.Height);

                    }

                    if(img != null)
                        e.Graphics.DrawImage(img, e.Bounds.X, e.Bounds.Y, 24, e.Bounds.Height);

                    using(SolidBrush brush = new SolidBrush(SystemColors.WindowText))
                    {
                        e.Graphics.DrawString(text, this.Font, brush, e.Bounds.X + 24,e.Bounds.Y);
                    }       
                }

                //선택한 상황이라면
                else
                {
                    using(SolidBrush backBrush = new SolidBrush(SystemColors.Highlight))
                    {
                        e.Graphics.FillRectangle(backBrush,
                            e.Bounds.X,e.Bounds.Y,e.Bounds.Width,e.Bounds.Height);
                    }

                    if(img != null)
                        e.Graphics.DrawImage(img, e.Bounds.X, e.Bounds.Y, 24, e.Bounds.Height);

                    using(SolidBrush brush = new SolidBrush(SystemColors.HighlightText))
                    {
                        e.Graphics.DrawString(text, this.Font, brush, e.Bounds.X + 24,e.Bounds.Y);
                    }                
                }              
            }
        }
    }

 사용하실때는 디자인 모드에서 이미지 리스트 하나 생성 하시고
콤보박스의 Images에다가 지정 하시면 됩니다.

출처: 데브피아


posted by 진연