Windows 窗体 MonthCalendar 控件允许你以多种方式自定义日历的外观。 例如,可以设置配色方案,并选择显示或隐藏周号和当前日期。
更改月历的配色方案
设置属性,如TitleBackColorTitleForeColor和 TrailingForeColor。 该 TitleBackColor 属性还确定一周中的日期的字体颜色。 该TrailingForeColor属性确定在显示的月份之前和之后的日期的颜色。
MonthCalendar1.TitleBackColor = System.Drawing.Color.Blue MonthCalendar1.TrailingForeColor = System.Drawing.Color.Red MonthCalendar1.TitleForeColor = System.Drawing.Color.Yellow
monthCalendar1.TitleBackColor = System.Drawing.Color.Blue; monthCalendar1.TrailingForeColor = System.Drawing.Color.Red; monthCalendar1.TitleForeColor = System.Drawing.Color.Yellow;
monthCalendar1->TitleBackColor = System::Drawing::Color::Blue; monthCalendar1->TrailingForeColor = System::Drawing::Color::Red; monthCalendar1->TitleForeColor = System::Drawing::Color::Yellow;
注释
从 Windows Vista 开始,根据主题,设置某些属性可能不会更改日历的外观。 例如,如果 Windows 设置为使用 Aero 主题,则设置BackColor、TitleBackColor、TitleForeColor或TrailingForeColor属性不起作用。 这是因为日历的更新版本以在运行时从当前操作系统主题派生的外观呈现。 如果要使用这些属性并启用早期版本的日历,可以禁用应用程序的视觉样式。 禁用视觉样式可能会影响应用程序中其他控件的外观和行为。 若要在 Visual Basic 中禁用视觉样式,请打开项目设计器并取消选中 “启用 XP 视觉样式 ”复选框。 若要在 C# 中禁用视觉样式,请打开Program.cs并注释掉
Application.EnableVisualStyles();
。 有关视觉样式的详细信息,请参阅 “启用视觉样式”。
显示控件底部的当前日期
将 ShowToday 属性设置为
true
。 以下示例在双击窗体时显示和省略今天的日期之间切换。Private Sub Form1_DoubleClick(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles MyBase.DoubleClick ' Toggle between True and False. MonthCalendar1.ShowToday = Not MonthCalendar1.ShowToday End Sub
private void Form1_DoubleClick(object sender, System.EventArgs e) { // Toggle between True and False. monthCalendar1.ShowToday = !monthCalendar1.ShowToday; }
private: System::Void Form1_DoubleClick(System::Object ^ sender, System::EventArgs ^ e) { // Toggle between True and False. monthCalendar1->ShowToday = !monthCalendar1->ShowToday; }
(Visual C#、Visual C++)将以下代码置于表单的构造函数中以注册事件处理程序。
this.DoubleClick += new System.EventHandler(this.Form1_DoubleClick);
this->DoubleClick += gcnew System::EventHandler(this, &Form1::Form1_DoubleClick);
显示周数
将 ShowWeekNumbers 属性设置为
true
。 可以在代码中或在“属性”窗口中设置此属性。周数显示在一周第一天左侧的单独列中。
MonthCalendar1.ShowWeekNumbers = True
monthCalendar1.ShowWeekNumbers = true;
monthCalendar1->ShowWeekNumbers = true;
另请参阅
- MonthCalendar 控件
- 如何:在 Windows 窗体 MonthCalendar 控件中选择日期范围
- 如何:在 Windows 窗体 MonthCalendar 控件中以粗体显示特定天数
- 如何:在 Windows 窗体 MonthCalendar 控件中显示多个月份