C#期末作業

用C#寫萬年曆

程式專案檔http://yo801106.wikidot.com/local--files/c/C%23%E6%9C%9F%E6%9C%AB%E4%BD%9C%E6%A5%AD.zip

萬年曆判斷星期幾的公式
int[] t = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
if (m < 3)
y -= 1;
return (y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7;

以下是繪圖事件程式碼及解釋
private void form_paint(object seder, System.Windows.Forms.PaintEventArgs e)
{
int [] month_days= { 31 ,28, 31, 30, 31, 30, 31, 31, 30, 31,30,31 }; //抓月份天數
Graphics g =this.CreateGraphics(); //取得視窗繪圖
string the_year = this.year.ToString(); //將年轉成字串
string the_month =this.month.ToString(); //將月轉成字串
string[] week = { "日", "一", "二", "三", "四", "五", "六" }; //用來列印用
SolidBrush mybursh= new SolidBrush(Color.Green); //畫刷 綠色
the_year+="年 "; //加入年
the_month+="月 "; //加入月
g.DrawString(the_year, new Font("新細明體", 20), new SolidBrush(Color.Red), new Point(10, 50));
g.DrawString(the_month, new Font("新細明體", 20), new SolidBrush(Color.Red), new Point(120, 50));
//列印
for (int i=0;i<week.Length;i++)
g.DrawString(week[i],new Font("新細明體",20.0F),mybursh ,new Point(10+i*50,90));
int first_day, total_day, place=10;
first_day = dow(year, month, 1); //取得第一天星期幾
place += 52 * first_day;
//取得這個月有幾天 還要考慮閏年
if (System.DateTime.IsLeapYear(year) && month == 2)
total_day = month_days[month - 1] + 1;
else
total_day = month_days[month - 1];
int count = 0;
for (int j = 0; first_day <= 6; first_day++, j++)
{
count++;
//先特別處理第一行
g.DrawString((j + 1).ToString(), new Font("新細明體", 20f), new SolidBrush(Color.Blue), new Point(place + j * 50, 120));

}
int now_weekday=0; //星期天 用來計算是否要換行
int y = 150;
place=10;
for (int i = count+1; i <= total_day; i++)
{
if (now_weekday == 7)
{
y += 30;
now_weekday %= 7;
}
g.DrawString(i.ToString(), new Font("新細明體", 20f), new SolidBrush(Color.Blue), new Point(place + now_weekday * 50, y));
now_weekday++;
}


完整程式碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private int year;
private int month;

public Form1()
{
InitializeComponent();
}

private void 結束ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void Form1_Load(object sender, EventArgs e)
{
year = System.DateTime.Now.Year;
month = System.DateTime.Now.Month;
this.Paint += new PaintEventHandler(this.form_paint);
}

private int dow(int y, int m, int d)
{
int[] t = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
if (m < 3)
y -= 1;
return (y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7;

}
private void form_paint(object seder, System.Windows.Forms.PaintEventArgs e)
{
int[] month_days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
Graphics g = this.CreateGraphics();
string the_year = this.year.ToString();
string the_month = this.month.ToString();
string[] week = { "日", "一", "二", "三", "四", "五", "六" };
SolidBrush mybursh = new SolidBrush(Color.Green);
the_year += "年 ";
the_month += "月 ";
g.DrawString(the_year, new Font("新細明體", 20), new SolidBrush(Color.Red), new Point(10, 50));
g.DrawString(the_month, new Font("新細明體", 20), new SolidBrush(Color.Red), new Point(120, 50));
for (int i = 0; i < week.Length; i++)
g.DrawString(week[i], new Font("新細明體", 20.0F), mybursh, new Point(10 + i * 50, 90));
int first_day, total_day, place = 10;
first_day = dow(year, month, 1);
place += 52 * first_day;
if (System.DateTime.IsLeapYear(year) && month == 2)
total_day = month_days[month - 1] + 1;
else
total_day = month_days[month - 1];
int count = 0;
for (int j = 0; first_day <= 6; first_day++, j++)
{
count++;
g.DrawString((j + 1).ToString(), new Font("新細明體", 20f), new SolidBrush(Color.Blue), new Point(place + j * 50, 120));

}
int now_weekday = 0;
int y = 150;
place = 10;
for (int i = count + 1; i <= total_day; i++)
{
if (now_weekday == 7)
{
y += 30;
now_weekday %= 7;
}
g.DrawString(i.ToString(), new Font("新細明體", 20f), new SolidBrush(Color.Blue), new Point(place + now_weekday * 50, y));
now_weekday++;

}
}

private void button2_Click(object sender, EventArgs e)
{
month++;
if (month > 12)
{
month = 1;
year++;
}
this.Refresh();

}

private void button1_Click(object sender, EventArgs e)
{
month;
if (month < 1)
{
month = 12;
year
;

}
this.Refresh();

}

private void 關於本程式ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("程式作者:林俞佑\n\n 2011 年 12 月 初版 ");
}
private void 回到現在toolStripMenuItem2_Click_1(object sender, EventArgs e)
{
year = System.DateTime.Now.Year;
month = System.DateTime.Now.Month;
this.Refresh();
}
}
}