메뉴 바로가기

서브메뉴 바로가기

본문 바로가기

logo

C# .NET 자료실

GroupBox 를 상속받아 OnPaint 함수를 오버라이딩 하면

GroupBox의 색상을 변경할수 있습니다.

 

전체소스

       WATGroupBoxDlg.zip

 

      WATGroupbox.gif

 

 

주요 소스

 

       WATGroupBox myGroupBox = new WATGroupBox();
       
        public frmMain()
        {
            InitializeComponent();
            myGroupBox.Text = "GroupBox1";
            myGroupBox.BorderColor = Color.Black;
            this.Controls.Add(myGroupBox);
        }

        private void btnRED_Click(object sender, EventArgs e)
        {
            myGroupBox.BorderColor = Color.Red;

        }

        private void btnBLUE_Click(object sender, EventArgs e)
        {
            myGroupBox.BorderColor = Color.Blue;
        }

        private void btnDelBorder_Click(object sender, EventArgs e)
        {
            myGroupBox.BorderColor = System.Drawing.SystemColors.Control;
        }
    }


    public class WATGroupBox : GroupBox
    {
        private Color borderColor;
        public Color BorderColor
        {
            get { return this.borderColor; }

            set { this.borderColor = value;
                this.Invalidate();
            }

        }


        public WATGroupBox()
        {
            this.borderColor = Color.Black;
        }


        protected override void OnPaint(PaintEventArgs e)
        {
            Size tSize = TextRenderer.MeasureText(this.Text, this.Font);
            Rectangle borderRect = e.ClipRectangle;
            borderRect.Y += tSize.Height / 2;
            borderRect.Height -= tSize.Height / 2;
            ControlPaint.DrawBorder(e.Graphics, borderRect, this.borderColor, ButtonBorderStyle.Solid);

            Rectangle textRect = e.ClipRectangle;
            textRect.X += 6;
            textRect.Width = tSize.Width;
            textRect.Height = tSize.Height;
            e.Graphics.FillRectangle(new SolidBrush(this.BackColor), textRect);
            e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), textRect);

        }

    }

관련 문서가 검색되었습니다.
  1. [2018/02/26] List 에서 고유값 얻기 by WhiteAT (4027)
  2. [2016/09/24] XML 으로 환경설정 저장하기 ( \n,엔터값 포함, NewLine) by WhiteAT (9668)
  3. [2015/05/22] C#, 아두이노 간의 WIFI 통신으로 LCD 제어 by WhiteAT (4565)
  4. [2015/03/13] 항상 마지막에 추가한 TEXT 보이게 by WhiteAT (15782)
  5. [2014/01/17] ComboBox Text 편집 안되게 by WhiteAT (14491)
  6. [2014/01/08] if 문에서 여러개 비교할때 by WhiteAT (25339) *3
  7. [2013/12/30] C++, C# 간단한 기능 비교 by WhiteAT (12884)
  8. [2013/12/18] 3자리마다 ,(콤마) 찍기 (원화, 달러 표시) by WhiteAT (15224)
  9. [2013/09/29] 설치된 IE 버전 얻기 by WhiteAT (12397)
  10. [2013/08/29] byte array to Hexa String by WhiteAT (13026)