| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1320 人关注过本帖
标题:小妹刚学C#,想问一下怎么把一段日历的程序放到winsform上!希望大家多帮忙
只看楼主 加入收藏
qih33
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-2-27
收藏
 问题点数:0 回复次数:11 
小妹刚学C#,想问一下怎么把一段日历的程序放到winsform上!希望大家多帮忙

这段程序使用C#写的,我不知道怎么让他显示到FORM上,请高人指点

class LargeScaleCalendar : System.Windows.Forms.Control
    {
        private System.Drawing.Point[] _lines;

        private Dictionary<string, DateTime> _holidays = new Dictionary<string, DateTime>();



        private string[] _days = { "M", "T", "W", "T", "F", "S", "S" };
        private string[] _months = { "J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D" };
        // Currently selected date
        private DateTime _currentSelected = DateTime.Now;
        private Int32 _currentYearDisplay = DateTime.Now.Year;

        public Int32 cellWidth = 20;
        public Int32 cellHeight = 20;
        public Int32 cellBorder = 1;
        public Int32 heightPadding = 15;
        public Int32 widthPadding = 15;

        public Int32 monthsToDisplay = 12;
        public Int32 daysToDisplay = 38;

        private System.Drawing.Color _activeDateColor = System.Drawing.Color.White;
        private System.Drawing.Color _holidayDateColor = System.Drawing.Color.CadetBlue;
        private System.Drawing.Color _inactiveDateColor = System.Drawing.Color.DarkGray;
        private System.Drawing.Color _invalidDateColor = System.Drawing.Color.Gray;

        private System.Drawing.Font _font = new System.Drawing.Font("Arial", 8.0f);
        private System.Drawing.SolidBrush _solidBlack = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
        private System.Drawing.Pen _pen = new System.Drawing.Pen(System.Drawing.Color.Black);

        static DateTime nullDateTime = new DateTime(1900, 1, 1);

        public delegate void DateHighlighted(DateTime selected, string value);
        public event DateHighlighted OnDateHighlighted;

        protected void HighlightDate(DateTime selected, string value)
        {
            if (OnDateHighlighted != null)
            {
                OnDateHighlighted(selected, value);
            }
        }

        public LargeScaleCalendar(Int32 width, Int32 height)
            : base()
        {
            this.Width = width;
            this.Height = height;

            InitializeComponent();
        }

        public LargeScaleCalendar()
            : base()
        {
            this.Width = 756;
            this.Height = 257;

            InitializeComponent();
           

        }

        // Generate list of lines based on variables defined above.
        private void SetupLines()
        {
            List<System.Drawing.Point> points = new List<System.Drawing.Point>();

            for (int columns = 0; columns < this.daysToDisplay; columns++)
            {
                points.Add(new System.Drawing.Point(columns * this.cellWidth + this.cellBorder + this.widthPadding, this.heightPadding + this.cellBorder));
                points.Add(new System.Drawing.Point(columns * this.cellWidth + this.cellBorder + this.widthPadding, Height));
            }

            for (int rows = 0; rows < this.monthsToDisplay; rows++)
            {
                points.Add(new System.Drawing.Point(this.widthPadding + this.cellBorder, rows * this.cellHeight + this.cellBorder + this.heightPadding));
                points.Add(new System.Drawing.Point(Width, rows * this.cellHeight + this.cellBorder + this.heightPadding));
            }

            _lines = points.ToArray();
        }

        protected override void OnMouseClick(System.Windows.Forms.MouseEventArgs e)
        {
            // Figure out where click ended up.
            System.Windows.Forms.MessageBox.Show("QQQQQQQQQQQQQQQQQQ");

            if (e.X > this.widthPadding && e.Y > this.heightPadding)
            {
                DateTime dateTimeAtXY = GetDateTimeAtXY(e.X, e.Y);

                if (!dateTimeAtXY.Equals(nullDateTime))
                {
                    string value = "";

                    foreach (KeyValuePair<string, DateTime> holiday in _holidays)
                    {
                        if (holiday.Value.Equals(dateTimeAtXY))
                        {
                            value = holiday.Key;
                            break;
                        }
                    }

                    HighlightDate(dateTimeAtXY, value);
                }
            }

            base.OnMouseClick(e);
        }

        private DateTime GetDateTimeAtXY(Int32 x, Int32 y)
        {
            DateTime dateTime = new DateTime(DateTime.Today.Year, 1, 1);

            Int32 k = 0;
            for (int i = this.heightPadding; i < this.Height; i = i + this.cellHeight)
            {
                if (i >= y)
                {
                    dateTime = dateTime.AddMonths(k - 1);
                    break;
                }
                k++;
            }

            Int32 numberOfDays = DateTime.DaysInMonth(DateTime.Today.Year, dateTime.Month);
            DayOfWeek firstDay = new DateTime(DateTime.Today.Year, dateTime.Month, 1).DayOfWeek;
            Int32 daysIn = (int)firstDay - 1;

            if (firstDay == DayOfWeek.Sunday)
            {
                daysIn += 7;
            }

            Int32 j = 0;
            for (int i = this.widthPadding + (daysIn * this.cellWidth); i < this.Width; i = i + this.cellWidth)
            {

                if (i >= x)
                {
                    if (j <= 0) j = 0;
                    if (j > numberOfDays) j = 0;
                    dateTime = dateTime.AddDays(j - 1);
                    break;
                }
                j++;
            }

            if (j == 0)
            {
                return new DateTime(1, 1, 1);
            }

            return dateTime;
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            System.Windows.Forms.MessageBox.Show("QQQQQQQQQQQQQQQQQQ");
            System.Drawing.Graphics graphics = e.Graphics;
            System.Drawing.SolidBrush activeBrush = new System.Drawing.SolidBrush(_activeDateColor);
            System.Drawing.SolidBrush inactiveBrush = new System.Drawing.SolidBrush(_inactiveDateColor);
            System.Drawing.SolidBrush invalidBrush = new System.Drawing.SolidBrush(_invalidDateColor);
            System.Drawing.SolidBrush holidayBrush = new System.Drawing.SolidBrush(_holidayDateColor);

            // Clean slate
            graphics.FillRectangle(activeBrush, e.ClipRectangle);

            // Fill rectangles with necessary colours. Days to roles; holiday, normal, blank.

            // Do weekends.
            for (int i = 0; i < (daysToDisplay / 7); i++)
            {
                System.Drawing.Rectangle rec = new System.Drawing.Rectangle(this.widthPadding + this.cellWidth * (((i + 1) * 7) - 2) + this.cellBorder, this.heightPadding + this.cellBorder, this.cellWidth * 2 + this.cellBorder, this.Height);

                graphics.FillRectangle(inactiveBrush, rec);
            }

            // Move through months. Find start date and draw rectangle for length based on number of days.
            for (int i = 1; i < monthsToDisplay + 1; i++)
            {
                Int32 numberOfDays = DateTime.DaysInMonth(DateTime.Today.Year, i);
                DayOfWeek firstDay = new DateTime(DateTime.Today.Year, i, 1).DayOfWeek;
                Int32 daysIn = (int)firstDay - 1;

                System.Drawing.Rectangle leftSide;
                Int32 dayOffset = 0;

                if (firstDay != DayOfWeek.Monday)
                {
                    leftSide = new System.Drawing.Rectangle(this.widthPadding + this.cellBorder, this.heightPadding + this.cellBorder + this.cellHeight * i - this.cellHeight, daysIn * this.cellWidth, this.cellHeight + this.cellBorder);

                    if (leftSide.Width < this.widthPadding)
                    {
                        dayOffset = this.cellWidth * 7;
                        leftSide.Width += dayOffset;
                    }

                    graphics.FillRectangle(invalidBrush, leftSide);
                }

                System.Drawing.Rectangle rightSide = new System.Drawing.Rectangle((daysIn * this.cellWidth) + (numberOfDays * this.cellWidth) + this.widthPadding + dayOffset + this.cellBorder, this.heightPadding + this.cellBorder + this.cellHeight * i - this.cellHeight, this.Width, this.cellHeight + this.cellBorder);

                graphics.FillRectangle(invalidBrush, rightSide);
            }

            foreach (KeyValuePair<string, DateTime> holiday in _holidays)
            {
                DateTime holidayMonth = new DateTime(holiday.Value.Year, holiday.Value.Month, 1);
                Int32 daysIn = (int)holidayMonth.DayOfWeek - 1;
                Int32 widthIn = (daysIn * this.cellWidth) + (this.cellWidth * holiday.Value.Day) - this.cellWidth;

                if (holidayMonth.DayOfWeek == DayOfWeek.Sunday)
                {
                    widthIn += this.cellWidth * 7;
                }

                graphics.FillRectangle(holidayBrush, new System.Drawing.Rectangle(this.widthPadding + this.cellBorder + widthIn, this.heightPadding + (this.cellHeight * holiday.Value.Month) + this.cellBorder - this.cellHeight, this.cellHeight + this.cellBorder, this.cellWidth + this.cellBorder));
            }

            for (int i = 0; i < daysToDisplay; i++)
            {
                graphics.DrawString(_days[i % 7], _font, _solidBlack, new System.Drawing.PointF(5 + (i * this.cellWidth) + this.widthPadding + this.cellBorder, 2));
            }

            for (int i = 1; i < monthsToDisplay + 1; i++)
            {
                graphics.DrawString(_months[(i - 1) % 12], _font, _solidBlack, new System.Drawing.PointF(3, 4 + (i * this.cellHeight) + this.heightPadding + this.cellBorder - this.cellHeight));

                System.Drawing.Point point = new System.Drawing.Point(((int)new DateTime(DateTime.Today.Year, i, 1).DayOfWeek) * this.cellWidth - this.cellWidth + this.cellBorder + this.widthPadding, (i - 1) * this.cellHeight + this.cellBorder + this.heightPadding);

                if (point.X < this.widthPadding)
                {
                    point.X += this.cellWidth * 7;
                }

                for (int j = 1; j < DateTime.DaysInMonth(DateTime.Today.Year, i) + 1; j++)
                {
                    graphics.DrawString(j.ToString(), _font, _solidBlack, point.X + ((j - 1) * this.cellWidth), point.Y);
                }
            }

            for (int i = 0; i < _lines.Length; i = i + 2)
            {
                graphics.DrawLine(_pen, _lines[i], _lines[i + 1]);
            }

            graphics.Dispose();
        }

        private void InitializeComponent()
        {
            SetupHolidays(DateTime.Today.Year);
            SetupLines();

            this.SuspendLayout();
            //
            // LargeScaleCalendar
            //
            this.Name = "LargeScaleCalendar";
            this.Size = new System.Drawing.Size(this.Width, this.Height);
            this.ResumeLayout(false);
        }

        private void SetupHolidays(Int32 year)
        {
            _holidays.Clear();

            _holidays.Add("New Years Day", new DateTime(year, 1, 1));
            _holidays.Add("Day after New Years Day", new DateTime(year, 1, 2));
            _holidays.Add("Waitangi Day", new DateTime(year, 2, 6));
            DateTime easter = calculateEaster(year);
            _holidays.Add("Good Friday", easter);
            _holidays.Add("Easter Monday", easter.AddDays(3));
            _holidays.Add("Anzac Day", new DateTime(year, 4, 25));
            _holidays.Add("Queen's Birthday", calculateNDayOfXMonthOfYear(1, DayOfWeek.Monday, new DateTime(year, 6, 1)));
            _holidays.Add("Labour Day", calculateNDayOfXMonthOfYear(4, DayOfWeek.Monday, new DateTime(year, 10, 1)));
            _holidays.Add("Christmas Day", new DateTime(year, 12, 25));
            _holidays.Add("Boxing Day", new DateTime(year, 12, 26));

            _holidays.Add("Buller Anniversary Day", calculateNDayClosestToXDay(DayOfWeek.Monday, new DateTime(year, 2, 1)));
        }

        public static bool isLeapYear(Int32 year)
        {
            return DateTime.IsLeapYear(year);
        }

        /// <summary>
        /// Calculate the day of easter friday for a given year.
        ///
        /// Example: Find Easter Friday on 2010.
        /// </summary>
        /// <param name="year">Year to find easter friday for.</param>
        /// <returns>Return dateTime of easter friday for the year specified.</returns>
        public static DateTime calculateEaster(Int32 year)
        {
            int Y = year;
            int a = Y % 19;
            int b = Y / 100;
            int c = Y % 100;
            int d = b / 4;
            int e = b % 4;
            int f = (b + 8) / 25;
            int g = (b - f + 1) / 3;
            int h = (19 * a + b - d - g + 15) % 30;
            int i = c / 4;
            int k = c % 4;
            int L = (32 + 2 * e + 2 * i - h - k) % 7;
            int m = (a + 11 * h + 22 * L) / 451;
            int Month = (h + L - 7 * m + 114) / 31;
            int Day = ((h + L - 7 * m + 114) % 31) - 1;
            DateTime dtEasterSunday = new DateTime(year, Month, Day);
            return dtEasterSunday;
        }

        /// <summary>
        /// Calculate the dateTime of a specific day (or occurence of day) in a specific month of a specific year.
        ///
        /// Example: Find the 2nd Monday of October 2010.
        /// </summary>
        /// <param name="dayOfWeek">Day of the week to find.</param>
        /// <param name="n">Number of occurences of this day in the month to specify on.</param>
        /// <param name="monthYear">Month and year to look in.</param>
        /// <returns>Returns a dateTime of the specified point in time.</returns>
        public static DateTime calculateNDayOfXMonthOfYear(Int32 occurenceN, DayOfWeek dayOfWeek, DateTime monthYear)
        {
            // Get a baseline of the 1st of the monthYear specified.
            DateTime date = new DateTime(monthYear.Year, monthYear.Month, 1);

            // Move through each day until the day is the same as specified.
            while (date.DayOfWeek != dayOfWeek)
            {
                date = date.AddDays(1);
            }

            // Add a week for each occurence specified.
            for (int i = 1; i < occurenceN; i++)
            {
                date = date.AddDays(7);
            }

            // Return.
            return date;
        }

        /// <summary>
        /// Find the day of the week closest to a specified date.
        ///
        /// Example: The Monday closest to 1st Feb 2010
        /// </summary>
        /// <param name="dayOfWeek">Day of the week to find.</param>
        /// <param name="closeToDateTime">Date to look from.</param>
        /// <returns>Returns a dateTime of the day of the week closest to the specified time.</returns>
        public static DateTime calculateNDayClosestToXDay(DayOfWeek dayOfWeek, DateTime closeToDateTime)
        {
            // Search forward 3 days and backward 3 days. Must be in either direction.
            DateTime tempDate = closeToDateTime;

            for (int i = 0; i != 3; i++)
            {
                tempDate = tempDate.AddDays(1);
                if (tempDate.DayOfWeek == dayOfWeek)
                {
                    return tempDate;
                }
            }

            tempDate = closeToDateTime;

            for (int i = 0; i != 3; i++)
            {
                tempDate = tempDate.AddDays(-1);

                if (tempDate.DayOfWeek == dayOfWeek)
                {
                    return tempDate;
                }
            }

            return closeToDateTime;
        }
    }
搜索更多相关主题的帖子: 日历 
2011-02-27 10:14
何事惊慌
Rank: 6Rank: 6
等 级:侠之大者
威 望:4
帖 子:220
专家分:499
注 册:2008-7-2
收藏
得分:0 
是自己写的一个控件吗?不简单

QQ:860234001
编程交流群:236949758
2011-02-27 18:48
wangnannan
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:87
帖 子:2546
专家分:9359
注 册:2007-11-3
收藏
得分:0 
妹子 你也不用把所有代码都贴出来吧 可以做成自定义控件拖放到winform就可以了

出来混,谁不都要拼命的嘛。 。拼不赢?那就看谁倒霉了。 。有机会也要看谁下手快,快的就能赢,慢。 。狗屎你都抢不到。 。还说什么拼命?
2011-02-28 10:37
c1_wangyf
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:7
帖 子:665
专家分:2832
注 册:2010-5-24
收藏
得分:0 
刚学就别整的这么复杂的了,慢慢来吗?!
2011-03-01 12:11
t492162921
Rank: 1
来 自:湖南株洲
等 级:新手上路
帖 子:4
专家分:5
注 册:2011-4-12
收藏
得分:0 
汗,打击人。

 不直接把 MonthCalendar 控件丢上去得了。
2011-04-12 22:03
OneHunter
Rank: 2
等 级:论坛游民
帖 子:25
专家分:26
注 册:2010-10-28
收藏
得分:0 
一看到这么长的代码,我头晕,哈,我水平还低了点,打那种几行的代码还OK,这个就不行了。。。

我还好~!
2011-04-12 22:16
wei65871533
Rank: 2
等 级:论坛游民
帖 子:12
专家分:22
注 册:2011-3-17
收藏
得分:0 
我记得有有控件的。。。
2011-04-13 20:45
a101069
Rank: 2
等 级:论坛游民
帖 子:18
专家分:64
注 册:2011-4-10
收藏
得分:0 
直接拖日历控件?这么大的一串代码,我表示很有鸭梨!!
2011-04-13 21:57
z1005036329
Rank: 1
来 自:河南南阳
等 级:新手上路
帖 子:4
专家分:2
注 册:2011-3-17
收藏
得分:0 
额。。。同感
2011-04-22 07:31
lwsfyts
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:39
专家分:132
注 册:2011-4-3
收藏
得分:0 
这是刚学的水平,鸭梨很大
2011-04-22 07:35
快速回复:小妹刚学C#,想问一下怎么把一段日历的程序放到winsform上!希望大家多 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.032105 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved