需求分析:需要做一款可以通過Winform 自己繪製的月曆,原因是可以通過這種方法進行在月曆下製作日程,如圖所示:
一、製作須知:
1、需要用PictureBox將表格組合起來,並且在PictureBox上面增加Label
2、Label的作用是爲了顯示切換月份的時候需要重新對Label賦值
3、需要編寫讀取每年每月當天有多少天,以及每個月的1號對應的是星期幾
二、製作步驟:
1、新建一個Winform,添加兩個Combox用於獲取某年某月
2、添加PictureBox ,按照圖示所示將控件組合起來
3、在所有的PictureBox(灰色部分)上,新建Label,初始化的時候默認使得 label.text = "",即不顯示
4、新增一個測試按鈕,註冊一個事件
int currentYear = Convert.ToInt32(comboBox1.Text.ToString());
int currentMonth = Convert.ToInt32(comboBox2.Text.ToString());
int X, Y;
X = WhatDay(currentYear, currentMonth);//得出某年某月的第一天是星期幾
Y = EveryMonthDays(currentYear, currentMonth);//得出某年某月的天數
SetValue(currentYear, currentMonth); //將當前的信息傳入自定義函數
4、得出星期幾
public int WhatDay(int currentYear, int month)
{
int num;
int totalDays = 0;
for (int i = 1900; i < currentYear; i++)
{
if (IsLeapYear(i))
{
totalDays += 366;
}
else
{
totalDays += 365;
}
}
for (int j = 1; j < month; j++)
{
totalDays += EveryMonthDays(currentYear, j);
}
num = totalDays % 7;
return num + 1;
}
5、得出天數
public int EveryMonthDays(int year, int month)//判斷某年每個月的天數
{
int i = month;
int monthDay;
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12)
{
monthDay = 31;
}
else if (i == 4 || i == 6 || i == 9 || i == 11)
{
monthDay = 30;
}
else if (i == 2 && IsLeapYear(year) == true)
{
monthDay = 29;
}
else
{
monthDay = 28;
}
return monthDay;
}
6、編寫SetValue(參數1,參數2)自定義函數
這個函數主要是爲了將得到的4,5 的結果,並通過遍歷的方法,將值賦值給Label
以上;需要源碼請發信息