今天在网上无意中搜索到“Vista风格日历控件”(http://www.cnpopsoft.com/article.asp?id=56)。下载之后发现,略有BUG,于是进行改进。无意中,制作出来带农历的万年历(周历版)。
运行如下图:
主要的改进在:
// frmDemo.cs
//#####################################################################################
//★★★★★★★ http://www.cnpopsoft.com [华普软件] ★★★★★★★
//★★ '华普软件-VB、C#专业论文与源码荟萃,敏捷开发,平台战略,让商业软件靓起来! ★★
//#####################################################################################
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace CNPOPSOFT.Controls
{
public partial class frmDemo : Form
{
public frmDemo()
{
//InitializeComponent();
this.Load += new System.EventHandler(this.frmDemo_Load);
}
private void frmDemo_Load(object sender, EventArgs e)
{
InitContent();
}
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitContent()
{
this.SuspendLayout();
int calendarX = 0;
int calendarY = 0;
int offsetX = -10;
int offsetY = -10;
int columns = 7;
int rows = 1;
VistaCalendar[] vistaCalendarList = new VistaCalendar[columns * rows];
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(1024, 700);
DateTime dateTimeNow = DateTime.Now;
int year = dateTimeNow.Year;
int month = dateTimeNow.Month;
int day = dateTimeNow.Day;
int styleCount = Enum.GetNames(typeof(CNPOPSOFT.Controls.VistaCalendar.VistaCalendarStyle)).Length;
for (int i = 0; i < vistaCalendarList.Length; i++)
{
vistaCalendarList[i] = new VistaCalendar();
vistaCalendarList[i].BackColor = System.Drawing.Color.Transparent;
vistaCalendarList[i].CurrentDate = dateTimeNow.AddDays(i);
vistaCalendarList[i].IsUseToday = false;
calendarX = (i % columns) * (155 + offsetX);
calendarY = (i / columns) * (160 + offsetY);
vistaCalendarList[i].Location = new System.Drawing.Point(calendarX, calendarY);
vistaCalendarList[i].Name = "vistaCalendar" + (i+1).ToString();
vistaCalendarList[i].Size = new System.Drawing.Size(155, 160);
vistaCalendarList[i].Style = (CNPOPSOFT.Controls.VistaCalendar.VistaCalendarStyle)((i + i/columns) % styleCount);
vistaCalendarList[i].TabIndex = i + 1;
this.Controls.Add(vistaCalendarList[i]);
}
//
// frmDemo
//
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "frmDemo";
this.Text = "Vista时钟控件";
this.Load += new System.EventHandler(this.frmDemo_Load);
this.ResumeLayout(false);
}
}
}
VistaCalendar.cs中新增了CurrentDate和IsUseToday两个公共属性:
public DateTime CurrentDate
{
get
{
return _currentDate;
}
set
{
_currentDate = value;
}
}
public bool IsUseToday
{
get { return _isUseToday; }
set { _isUseToday = value; }
}
并将UpdateCurrentDateInfo();由构造函数中放到了void VistaCalendar_Paint(object sender, PaintEventArgs e)中,这样更加合理。其他地方的微小改动包括字体大小等。
在此,谨向原作者致谢!感谢他的辛勤劳动!