Programming/C#

[C# 개념정리] Drawitem(listbox)

Calif 2018. 10. 25. 10:10

윈폼으로 호가창을 만드는 중 매도/매수잔량에 게이지를 어떻게 코딩해야 하는지 감이 안잡히던 중...


장시간의 구글링을 통하여 Listbox/view에서 사용하는 Drawitem 이벤트를 통하여 개발이 가능하다는 사실을 알게 되었ㄷ...


기존에 Label 여러개를 덕지덕지 붙이는 (듀얼코어 이전 코어2개 때려박았던 CPU 느낌? ㅋㅋㅋㅋ) 멍청한 짓을 뒤로하고


Drawitem 이벤트를 활용하여 보자.




Drawitem은 기본적으로 Listbox에 무엇인가 그려지면(글자, 혹은 이미지 등) 발생하는 이벤트로


여러분이 원하는 대부분의 튜닝이 가능할 것이라 생각된다.


      private void listBox1_DrawItem(object sender, DrawItemEventArgs e)

      {

//

      }


기본적으로 이벤트를 만들면 이런 형식의 함수가 만들어 지며 파라메터 'e'를 통하여 원하는 튜닝을 할 수 있다.


    private void listBox1_DrawItem(object sender, DrawItemEventArgs e)

        {

            // Get the ListBox and the item.

            ListBox lst = sender as ListBox;

            long value =  Convert.ToInt64(lst.Items[e.Index].ToString());

            string txt = (string)lst.Items[e.Index];

            txt = string.Format("{0:#,#}", txt);            // 이거 안됨.. 왜 안될까?

            Console.WriteLine(txt);

            int ItemMargin = 3;


            // Draw the background.

            e.DrawBackground();


            // See if the item is selected.

            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)

            {   

                // Selected. Draw with the system highlight color.

                e.Graphics.DrawString(txt, this.Font, SystemBrushes.HighlightText, e.Bounds.Left, e.Bounds.Top + ItemMargin);

            }

            else

            {

                // Not selected. Draw with ListBox's foreground color.

                if (value > StdPrice)

                {

                    using (SolidBrush br = new SolidBrush(Color.Red))

                    {

                        float olength = e.Graphics.MeasureString(txt, e.Font).Width;

                        float pos = lst.Width - olength;

                        e.Graphics.DrawString(txt, this.Font, br, pos, e.Bounds.Top + ItemMargin);

                    }

                }

                else if (value == StdPrice)

                {

                    using (SolidBrush br = new SolidBrush(e.ForeColor))

                    {

                        float olength = e.Graphics.MeasureString(txt, e.Font).Width;

                        float pos = lst.Width - olength;

                        e.Graphics.DrawString(txt, this.Font, br, pos, e.Bounds.Top + ItemMargin);

                    }

                }

                else

                {

                    using (SolidBrush br = new SolidBrush(Color.Blue))

                    {

                        float olength = e.Graphics.MeasureString(txt, e.Font).Width;

                        float pos = lst.Width - olength;

                        e.Graphics.DrawString(txt, this.Font, br, pos, e.Bounds.Top + ItemMargin);

                    }

                }    

            }


위의 코드는 호가창에서 매도호가를 구현한 코드이다. StdPrice 라는 기준가 변수를 통하여 폰트가 어떤색을 나타낼지 설정하였다.


이것저것 실험해보면서 추가해보겠다. [2018-10-25]


모든 문제를 해결하였다


'Programming > C#' 카테고리의 다른 글

[윈폼] 호가창 만들기  (0) 2018.10.24
[C# 개념정리] 상속(Inheritance)  (0) 2018.10.18