小妹刚学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;
}
}