퇴근5분전


아 칙칙해보이넹. ㅋㅋ

< 달력 결과물 >


훈스에 달력을 직접 그려보고 싶어하시는 분이 계시는데... 훔... 그냥 이렇게... 후! 

좀 더 시간투자를 하면 완전 바꿨을텐데... 잠깐 짬내본거라.


internal class Day : Label
    {
        public static int _Width = 120;
        public static int _Height = 24;

        public Day()
        {
            this.AutoSize = false;
            this.Width = _Width;
            this.Height = _Height;
            this.MouseHover += new EventHandler(Day_MouseHover);

            this.BorderStyle = BorderStyle.Fixed3D;
        }

        public Day(string text, Point location) : this()
        {
            this.Text = text;
            this.Location = location;
        }

        void Day_MouseHover(object sender, EventArgs e)
        {
            ToolTip tip = new ToolTip();
            {
                tip.Show(this.Text, this, 2000 );
            }
        }
    }

    public class Calendar : UserControl
    {
        int month = DateTime.Now.Month;

        [Category("설정")]
        [Description("해당 월을 입력합니다.")]
        public int Month
        {
            get { return month; }
            set { month = value; Draw(year, month); }
        }
        int year = DateTime.Now.Year;

        [Category("설정")]
        [Description("해당 년을 입력합니다.")]
        public int Year
        {
            get { return year; }
            set { year = value; Draw(year, month);
            }
        }

        public Calendar()
        {
            this.BorderStyle = BorderStyle.FixedSingle;
            Draw(year, month);
        }

        void Draw(int year, int month)
        {
            int heighttotal = 0;
            int widthtotal = 0;
            this.Controls.Clear();
            DateTime date = new DateTime(year, month, 1); // 해당년월에 1일.

            int totalDay = DateTime.DaysInMonth(year, month);

            string[] dayofweek = Enum.GetNames(typeof(DayOfWeek));

            for (int i = 0; i < dayofweek.Length; i++)
            {
                Day weekctrl = new Day(dayofweek[i].Substring(0, 3), new Point(i * Day._Width, 0));
                this.Controls.Add(weekctrl);

                if ((i % 7 )== (int)DayOfWeek.Saturday) // 토요일
                    weekctrl.ForeColor = Color.Blue;
                else if ((i % 7 ) == (int)DayOfWeek.Sunday) // 일요일
                    weekctrl.ForeColor = Color.Red;

                widthtotal += Day._Width;
            }

            int y = 1;
            heighttotal += Day._Height;

            for (int day = 1 - (int)date.DayOfWeek; day <= totalDay; day++)
            {
                Day dayCtrl = new Day();

                dayCtrl.Location = new Point(((day + (int)date.DayOfWeek) % 7) * Day._Width, y * Day._Height);

                //설정
                if ((day + (int)date.DayOfWeek) % 7 == (int)DayOfWeek.Saturday ) // 토요일
                    dayCtrl.ForeColor = Color.Blue;
                else if ((day + (int)date.DayOfWeek) % 7 == (int)DayOfWeek.Sunday) // 일요일
                    dayCtrl.ForeColor = Color.Red;

                if (day <= 0)
                    dayCtrl.ResetText();
                else
                {
                    if (day == DateTime.Now.Day && year == DateTime.Now.Year && month == DateTime.Now.Month) // 오늘 날짜 표시
                    {
                        dayCtrl.Font = new Font(dayCtrl.Font.FontFamily, dayCtrl.Font.Size, FontStyle.Bold);
                    }

                    dayCtrl.Text = day.ToString();
                }
                this.Controls.Add(dayCtrl);
                if ((int)DayOfWeek.Saturday  == (day + (int)date.DayOfWeek) % 7)
                {
                    y++;
                    heighttotal += Day._Height;
                };// 다음주~ 로 넘어가는 부분   DayofWeek 요일 열거형
            }
            heighttotal += Day._Height;
            this.Height = heighttotal + 2;
            this.Width = widthtotal;
        }
   
    }