偶然翻到以前下载的例子,觉得还不错;在这里记录一下防止丢失,也顺带分享一下;例子是c#自定义日期卡片控件,控件是基于winfrom写的,类似一vista风格的时钟控件,c#自定义日期卡片代码也不多,主要包含农历转换类和自定义矩形类,直接复制粘贴就能用。
首先我们新建winform自定义控件,然后贴上如下代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace CNPOPSOFT.Controls
{
public partial class VistaCalendar : UserControl
{
Timer timer = new Timer();
bool useToday = true;
DateTime curDate;
string strYearMonth, strDay, strDayOfWeek, strCnYearInfo;
Font fontDefault = new Font("微软雅黑", 9);
Font fontDay = new Font("微软雅黑", 46);
Font fontCnYearInfo = new Font("微软雅黑", 7.5f);
public enum VistaCalendarStyle
{
Default, Red, Purple, Green, Gray, Brown, Blue
}
private VistaCalendarStyle style = VistaCalendarStyle.Default;
/// <summary>
/// 样式
/// </summary>
public VistaCalendarStyle Style
{
get { return style; }
set { style = value; this.Invalidate(); }
}
public VistaCalendar()
{
InitializeComponent();
curDate = DateTime.Today;
UpdateCurrentDateInfo();
timer.Interval = 6000; //1分钟刷新一次
timer.Enabled = true;
timer.Tick += new EventHandler(timer_Tick);
this.BackColor = Color.Transparent;
this.DoubleBuffered = true;
this.Paint += new PaintEventHandler(VistaCalendar_Paint);
this.Resize += new EventHandler(VistaCalendar_Resize);
this.MouseUp += new MouseEventHandler(VistaCalendar_MouseUp);
}
void VistaCalendar_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
contextMenuStrip1.Show(this, e.Location);
}
}
void timer_Tick(object sender, EventArgs e)
{
if (useToday && curDate != DateTime.Today)
{
curDate = DateTime.Today;
UpdateCurrentDateInfo();
this.Invalidate();
}
}
/// <summary>
/// 更新当前日期信息
/// </summary>
private void UpdateCurrentDateInfo()
{
strYearMonth = curDate.Year.ToString() + "年 " + curDate.Month.ToString() + "月";
strDay = curDate.Day.ToString();
strDayOfWeek = GetDayOfWeekString(curDate);
ChineseCalendarInfo cn = new ChineseCalendarInfo(curDate);
string cnInfo = cn.LunarYearSexagenary;
cnInfo += cn.LunarYearAnimal + "年 ";
cnInfo += cn.LunarMonthText + "月";
cnInfo += cn.LunarDayText;
strCnYearInfo = cnInfo;
}
/// <summary>
/// 获取星期的中文字符串
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
private string GetDayOfWeekString(DateTime date)
{
string weekday;
int dayOfWeek = (int)date.DayOfWeek;
if (dayOfWeek == 1)
weekday = "星期一 ";
else if (dayOfWeek == 2)
weekday = "星期二 ";
else if (dayOfWeek == 3)
weekday = "星期三 ";
else if (dayOfWeek == 4)
weekday = "星期四 ";
else if (dayOfWeek == 5)
weekday = "星期五 ";
else if (dayOfWeek == 6)
weekday = "星期六 ";
else
weekday = "星期日 ";
return weekday;
}
private CustomRectangle positionRect = new CustomRectangle();
/// <summary>
/// 位置矩形
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public CustomRectangle PositionRect
{
get { return positionRect; }
set { positionRect = value; }
}
void VistaCalendar_Resize(object sender, EventArgs e)
{
if (positionRect == null) positionRect = new CustomRectangle();
positionRect.Width = (int)ImageBg.Width;
positionRect.Height = (int)ImageBg.Height;
positionRect.Top = (int)(this.ClientSize.Height < ImageBg.Height ? 0 : (this.ClientSize.Height - ImageBg.Height) / 2f);
positionRect.Left = (int)(this.ClientSize.Width < ImageBg.Width ? 0 : (this.ClientSize.Width - ImageBg.Width) / 2f);
this.Invalidate();
}
internal SolidBrush FontBrush
{
get
{
if (style == VistaCalendarStyle.Green)
return new SolidBrush(Color.Black);
else
return new SolidBrush(Color.White);
}
}
void VistaCalendar_Paint(object sender, PaintEventArgs e)
{
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
//背景
e.Graphics.DrawImage(ImageBg, positionRect.ToRectangle());
//绘制日期
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
e.Graphics.DrawString(strYearMonth, fontDefault, FontBrush,
new Rectangle((int)positionRect.X + 10, (int)positionRect.Y + 26, (int)positionRect.Width - 20, 20), format);
e.Graphics.DrawString(strDayOfWeek, fontDefault, FontBrush,
new Rectangle((int)positionRect.X + 10, (int)positionRect.Y + 106, (int)positionRect.Width - 20, 20), format);
e.Graphics.DrawString(strCnYearInfo, fontCnYearInfo, FontBrush,
new Rectangle((int)positionRect.X + 10, (int)positionRect.Y + 122, (int)positionRect.Width - 20, 20), format);
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
e.Graphics.DrawString(strDay, fontDay, new SolidBrush(Color.Black),
new Rectangle((int)positionRect.X + 12, (int)positionRect.Y + 40, (int)positionRect.Width - 20, 80), format);
e.Graphics.DrawString(strDay, fontDay, new SolidBrush(Color.White),
new Rectangle((int)positionRect.X + 10, (int)positionRect.Y + 38, (int)positionRect.Width - 20, 80), format);
}
/// <summary>
/// 背景图片
/// </summary>
private Bitmap ImageBg
{
get
{
switch (style)
{
case VistaCalendarStyle.Blue:
return global::CNPOPSOFT.Controls.Properties.Resources.Calendar_bg_blue;
case VistaCalendarStyle.Brown:
return global::CNPOPSOFT.Controls.Properties.Resources.Calendar_bg_brown;
case VistaCalendarStyle.Gray:
return global::CNPOPSOFT.Controls.Properties.Resources.Calendar_bg_gray;
case VistaCalendarStyle.Green:
return global::CNPOPSOFT.Controls.Properties.Resources.Calendar_bg_green;
case VistaCalendarStyle.Purple:
return global::CNPOPSOFT.Controls.Properties.Resources.Calendar_bg_purple;
case VistaCalendarStyle.Red:
return global::CNPOPSOFT.Controls.Properties.Resources.Calendar_bg_red;
default:
return global::CNPOPSOFT.Controls.Properties.Resources.Calendar_bg;
}
}
}
private void mnu炫菲橘黄_Click(object sender, EventArgs e)
{
this.style = VistaCalendarStyle.Default;
this.Invalidate();
}
private void mnu蔚然天蓝_Click(object sender, EventArgs e)
{
this.style = VistaCalendarStyle.Blue;
this.Invalidate();
}
private void mnu烟熏黛紫_Click(object sender, EventArgs e)
{
this.style = VistaCalendarStyle.Purple;
this.Invalidate();
}
private void mnu时尚晶红_Click(object sender, EventArgs e)
{
this.style = VistaCalendarStyle.Red;
this.Invalidate();
}
private void mnu流光荧绿_Click(object sender, EventArgs e)
{
this.style = VistaCalendarStyle.Green;
this.Invalidate();
}
private void mnu榛情咖啡_Click(object sender, EventArgs e)
{
this.style = VistaCalendarStyle.Brown;
this.Invalidate();
}
private void mnu沉稳雅灰_Click(object sender, EventArgs e)
{
this.style = VistaCalendarStyle.Gray;
this.Invalidate();
}
private void mnu转到上一月_Click(object sender, EventArgs e)
{
useToday = false;
curDate = curDate.AddMonths(-1);
UpdateCurrentDateInfo();
this.Invalidate();
}
private void mnu转到上一天_Click(object sender, EventArgs e)
{
useToday = false;
curDate = curDate.AddDays(-1);
UpdateCurrentDateInfo();
this.Invalidate();
}
private void mnu转到今天_Click(object sender, EventArgs e)
{
useToday = true;
curDate = DateTime.Today;
UpdateCurrentDateInfo();
this.Invalidate();
}
private void mnu转到下一天_Click(object sender, EventArgs e)
{
useToday = false;
curDate = curDate.AddDays(1);
UpdateCurrentDateInfo();
this.Invalidate();
}
private void mnu转到下一月_Click(object sender, EventArgs e)
{
useToday = false;
curDate = curDate.AddMonths(1);
UpdateCurrentDateInfo();
this.Invalidate();
}
}
}
然后新建农历转换类库代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Text.RegularExpressions;
namespace CNPOPSOFT.Controls
{
internal class ChineseCalendarInfo
{
private DateTime m_SolarDate;
private int m_LunarYear, m_LunarMonth, m_LunarDay;
private bool m_IsLeapMonth = false;
private string m_LunarYearSexagenary = null, m_LunarYearAnimal = null;
private string m_LunarYearText = null, m_LunarMonthText = null, m_LunarDayText = null;
private string m_SolarWeekText = null, m_SolarConstellation = null, m_SolarBirthStone = null;
#region 构造函数
public ChineseCalendarInfo()
: this(DateTime.Now.Date)
{
}
/// <summary>
/// 从指定的阳历日期创建中国日历信息实体类
/// </summary>
/// <param name="date">指定的阳历日期</param>
public ChineseCalendarInfo(DateTime date)
{
m_SolarDate = date;
LoadFromSolarDate();
}
private void LoadFromSolarDate()
{
m_IsLeapMonth = false;
m_LunarYearSexagenary = null;
m_LunarYearAnimal = null;
m_LunarYearText = null;
m_LunarMonthText = null;
m_LunarDayText = null;
m_SolarWeekText = null;
m_SolarConstellation = null;
m_SolarBirthStone = null;
m_LunarYear = calendar.GetYear(m_SolarDate);
m_LunarMonth = calendar.GetMonth(m_SolarDate);
int leapMonth = calendar.GetLeapMonth(m_LunarYear);
if (leapMonth == m_LunarMonth)
{
m_IsLeapMonth = true;
m_LunarMonth -= 1;
}
else if (leapMonth > 0 && leapMonth < m_LunarMonth)
{
m_LunarMonth -= 1;
}
m_LunarDay = calendar.GetDayOfMonth(m_SolarDate);
CalcConstellation(m_SolarDate, out m_SolarConstellation, out m_SolarBirthStone);
}
#endregion
#region 日历属性
/// <summary>
/// 阳历日期
/// </summary>
public DateTime SolarDate
{
get { return m_SolarDate; }
set
{
if (m_SolarDate.Equals(value))
return;
m_SolarDate = value;
LoadFromSolarDate();
}
}
/// <summary>
/// 星期几
/// </summary>
public string SolarWeekText
{
get
{
if (string.IsNullOrEmpty(m_SolarWeekText))
{
int i = (int)m_SolarDate.DayOfWeek;
m_SolarWeekText = ChineseWeekName[i];
}
return m_SolarWeekText;
}
}
/// <summary>
/// 阳历星座
/// </summary>
public string SolarConstellation
{
get { return m_SolarConstellation; }
}
/// <summary>
/// 阳历诞生石
/// </summary>
public string SolarBirthStone
{
get { return m_SolarBirthStone; }
}
/// <summary>
/// 阴历年份
/// </summary>
public int LunarYear
{
get { return m_LunarYear; }
}
/// <summary>
/// 阴历月份
/// </summary>
public int LunarMonth
{
get { return m_LunarMonth; }
}
/// <summary>
/// 是否阴历闰月
/// </summary>
public bool IsLeapMonth
{
get { return m_IsLeapMonth; }
}
/// <summary>
/// 阴历月中日期
/// </summary>
public int LunarDay
{
get { return m_LunarDay; }
}
/// <summary>
/// 阴历年干支
/// </summary>
public string LunarYearSexagenary
{
get
{
if (string.IsNullOrEmpty(m_LunarYearSexagenary))
{
int y = calendar.GetSexagenaryYear(this.SolarDate);
m_LunarYearSexagenary = CelestialStem.Substring((y - 1) % 10, 1) + TerrestrialBranch.Substring((y - 1) % 12, 1);
}
return m_LunarYearSexagenary;
}
}
/// <summary>
/// 阴历年生肖
/// </summary>
public string LunarYearAnimal
{
get
{
if (string.IsNullOrEmpty(m_LunarYearAnimal))
{
int y = calendar.GetSexagenaryYear(this.SolarDate);
m_LunarYearAnimal = Animals.Substring((y - 1) % 12, 1);
}
return m_LunarYearAnimal;
}
}
/// <summary>
/// 阴历年文本
/// </summary>
public string LunarYearText
{
get
{
if (string.IsNullOrEmpty(m_LunarYearText))
{
m_LunarYearText = Animals.Substring(calendar.GetSexagenaryYear(new DateTime(m_LunarYear, 1, 1)) % 12 - 1, 1);
StringBuilder sb = new StringBuilder();
int year = this.LunarYear;
int d;
do
{
d = year % 10;
sb.Insert(0, ChineseNumber[d]);
year = year / 10;
} while (year > 0);
m_LunarYearText = sb.ToString();
}
return m_LunarYearText;
}
}
/// <summary>
/// 阴历月文本
/// </summary>
public string LunarMonthText
{
get
{
if (string.IsNullOrEmpty(m_LunarMonthText))
{
m_LunarMonthText = (this.IsLeapMonth ? "闰" : "") + ChineseMonthName[this.LunarMonth - 1];
}
return m_LunarMonthText;
}
}
/// <summary>
/// 阴历月中日期文本
/// </summary>
public string LunarDayText
{
get
{
if (string.IsNullOrEmpty(m_LunarDayText))
m_LunarDayText = ChineseDayName[this.LunarDay - 1];
return m_LunarDayText;
}
}
#endregion
/// <summary>
/// 根据指定阳历日期计算星座&诞生石
/// </summary>
/// <param name="date">指定阳历日期</param>
/// <param name="constellation">星座</param>
/// <param name="birthstone">诞生石</param>
public static void CalcConstellation(DateTime date, out string constellation, out string birthstone)
{
int i = Convert.ToInt32(date.ToString("MMdd"));
int j;
if (i >= 321 && i <= 419)
j = 0;
else if (i >= 420 && i <= 520)
j = 1;
else if (i >= 521 && i <= 621)
j = 2;
else if (i >= 622 && i <= 722)
j = 3;
else if (i >= 723 && i <= 822)
j = 4;
else if (i >= 823 && i <= 922)
j = 5;
else if (i >= 923 && i <= 1023)
j = 6;
else if (i >= 1024 && i <= 1121)
j = 7;
else if (i >= 1122 && i <= 1221)
j = 8;
else if (i >= 1222 || i <= 119)
j = 9;
else if (i >= 120 && i <= 218)
j = 10;
else if (i >= 219 && i <= 320)
j = 11;
else
{
constellation = "未知星座";
birthstone = "未知诞生石";
return;
}
constellation = Constellations[j];
birthstone = BirthStones[j];
#region 星座划分
//白羊座: 3月21日------4月19日 诞生石: 钻石
//金牛座: 4月20日------5月20日 诞生石: 蓝宝石
//双子座: 5月21日------6月21日 诞生石: 玛瑙
//巨蟹座: 6月22日------7月22日 诞生石: 珍珠
//狮子座: 7月23日------8月22日 诞生石: 红宝石
//处女座: 8月23日------9月22日 诞生石: 红条纹玛瑙
//天秤座: 9月23日------10月23日 诞生石: 蓝宝石
//天蝎座: 10月24日-----11月21日 诞生石: 猫眼石
//射手座: 11月22日-----12月21日 诞生石: 黄宝石
//摩羯座: 12月22日-----1月19日 诞生石: 土耳其玉
//水瓶座: 1月20日-----2月18日 诞生石: 紫水晶
//双鱼座: 2月19日------3月20日 诞生石: 月长石,血石
#endregion
}
#region 阴历转阳历
/// <summary>
/// 获取指定年份春节当日(正月初一)的阳历日期
/// </summary>
/// <param name="year">指定的年份</param>
private static DateTime GetLunarNewYearDate(int year)
{
DateTime dt = new DateTime(year, 1, 1);
int cnYear = calendar.GetYear(dt);
int cnMonth = calendar.GetMonth(dt);
int num1 = 0;
int num2 = calendar.IsLeapYear(cnYear) ? 13 : 12;
while (num2 >= cnMonth)
{
num1 += calendar.GetDaysInMonth(cnYear, num2--);
}
num1 = num1 - calendar.GetDayOfMonth(dt) + 1;
return dt.AddDays(num1);
}
/// <summary>
/// 阴历转阳历
/// </summary>
/// <param name="year">阴历年</param>
/// <param name="month">阴历月</param>
/// <param name="day">阴历日</param>
/// <param name="IsLeapMonth">是否闰月</param>
public static DateTime GetDateFromLunarDate(int year, int month, int day, bool IsLeapMonth)
{
if (year < 1902 || year > 2100)
throw new Exception("只支持1902~2100期间的农历年");
if (month < 1 || month > 12)
throw new Exception("表示月份的数字必须在1~12之间");
if (day < 1 || day > calendar.GetDaysInMonth(year, month))
throw new Exception("农历日期输入有误");
int num1 = 0, num2 = 0;
int leapMonth = calendar.GetLeapMonth(year);
if (((leapMonth == month + 1) && IsLeapMonth) || (leapMonth > 0 && leapMonth <= month))
num2 = month;
else
num2 = month - 1;
while (num2 > 0)
{
num1 += calendar.GetDaysInMonth(year, num2--);
}
DateTime dt = GetLunarNewYearDate(year);
return dt.AddDays(num1 + day - 1);
}
/// <summary>
/// 阴历转阳历
/// </summary>
/// <param name="date">阴历日期</param>
/// <param name="IsLeapMonth">是否闰月</param>
public static DateTime GetDateFromLunarDate(DateTime date, bool IsLeapMonth)
{
return GetDateFromLunarDate(date.Year, date.Month, date.Day, IsLeapMonth);
}
#endregion
#region 从阴历创建日历
/// <summary>
/// 从阴历创建日历实体
/// </summary>
/// <param name="year">阴历年</param>
/// <param name="month">阴历月</param>
/// <param name="day">阴历日</param>
/// <param name="IsLeapMonth">是否闰月</param>
public static ChineseCalendarInfo FromLunarDate(int year, int month, int day, bool IsLeapMonth)
{
DateTime dt = GetDateFromLunarDate(year, month, day, IsLeapMonth);
return new ChineseCalendarInfo(dt);
}
/// <summary>
/// 从阴历创建日历实体
/// </summary>
/// <param name="date">阴历日期</param>
/// <param name="IsLeapMonth">是否闰月</param>
public static ChineseCalendarInfo FromLunarDate(DateTime date, bool IsLeapMonth)
{
return FromLunarDate(date.Year, date.Month, date.Day, IsLeapMonth);
}
/// <summary>
/// 从阴历创建日历实体
/// </summary>
/// <param name="date">表示阴历日期的8位数字,例如:20070209</param>
/// <param name="IsLeapMonth">是否闰月</param>
public static ChineseCalendarInfo FromLunarDate(string date, bool IsLeapMonth)
{
Regex rg = new System.Text.RegularExpressions.Regex(@"^\d{7}(\d)$");
Match mc = rg.Match(date);
if (!mc.Success)
{
throw new Exception("日期字符串输入有误!");
}
DateTime dt = DateTime.Parse(string.Format("{0}-{1}-{2}", date.Substring(0, 4), date.Substring(4, 2), date.Substring(6, 2)));
return FromLunarDate(dt, IsLeapMonth);
}
#endregion
private static ChineseLunisolarCalendar calendar = new ChineseLunisolarCalendar();
public const string ChineseNumber = "〇一二三四五六七八九";
public const string CelestialStem = "甲乙丙丁戊己庚辛壬癸";
public const string TerrestrialBranch = "子丑寅卯辰巳午未申酉戌亥";
public const string Animals = "鼠牛虎兔龙蛇马羊猴鸡狗猪";
public static readonly string[] ChineseWeekName = new string[] { "星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
public static readonly string[] ChineseDayName = new string[] {
"初一","初二","初三","初四","初五","初六","初七","初八","初九","初十",
"十一","十二","十三","十四","十五","十六","十七","十八","十九","二十",
"廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"};
public static readonly string[] ChineseMonthName = new string[] { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" };
public static readonly string[] Constellations = new string[] { "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座", "水瓶座", "双鱼座" };
public static readonly string[] BirthStones = new string[] { "钻石", "蓝宝石", "玛瑙", "珍珠", "红宝石", "红条纹玛瑙", "蓝宝石", "猫眼石", "黄宝石", "土耳其玉", "紫水晶", "月长石,血石" };
}
}
最后新建自定义矩形类
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
//5/1/a/spx
namespace CNPOPSOFT.Controls
{
[Serializable]
public class CustomRectangle
{
public CustomRectangle()
{
}
public CustomRectangle(float x, float y, float width, float height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
#region 属性定义区
private float x;
/// <summary>
/// X坐标
/// </summary>
public float X
{
get { return x; }
set { x = value; }
}
private float y;
/// <summary>
/// Y坐标
/// </summary>
public float Y
{
get { return y; }
set { y = value; }
}
/// <summary>
/// X坐标
/// </summary>
public float Left
{
get { return x; }
set { x = value; }
}
/// <summary>
/// Y坐标
/// </summary>
public float Top
{
get { return y; }
set { y = value; }
}
private float width;
/// <summary>
/// 宽度
/// </summary>
public float Width
{
get { return width; }
set { width = value; }
}
private float height;
/// <summary>
/// 高度
/// </summary>
public float Height
{
get { return height; }
set { height = value; }
}
/// <summary>
/// 返回右边X轴坐标(等于X+Width)
/// </summary>
public float Right
{
get { return x + width; }
}
/// <summary>
/// 返回底部Y轴坐标(等于Y+Height)
/// </summary>
public float Bottom
{
get { return y + height; }
}
/// <summary>
/// 返回矩形中点
/// </summary>
public CustomPoint CenterPoint
{
get { return new CustomPoint(x + width / 2, y + height / 2); }
}
#endregion
#region 方法区
/// <summary>
/// 返回一个克隆的对象
/// </summary>
/// <returns></returns>
public CustomRectangle Clone()
{
return new CustomRectangle(x, y, width, height);
}
/// <summary>
/// 转化为Rectangle对象
/// </summary>
/// <returns></returns>
public Rectangle ToRectangle()
{
return new Rectangle((int)x, (int)y, (int)width, (int)height);
}
/// <summary>
/// 转化为RectangleF对象
/// </summary>
/// <returns></returns>
public RectangleF ToRectangleF()
{
return new RectangleF(x, y, width, height);
}
/// <summary>
/// 返回一个左上角坐标点
/// </summary>
/// <returns></returns>
public Point ToPoint()
{
return new Point((int)x, (int)y);
}
/// <summary>
/// 返回一个左上角坐标点
/// </summary>
/// <returns></returns>
public PointF ToPointF()
{
return new PointF(x, y);
}
/// <summary>
/// 判断指定坐标的点是否在矩形内部
/// </summary>
/// <param name="ptX">X坐标</param>
/// <param name="ptY">Y坐标</param>
/// <returns></returns>
public bool IsPointInRectangle(int ptX, int ptY)
{
return (ptX >= x && ptX <= (x + width) && ptY >= y && ptY <= (y + height));
}
/// <summary>
/// 判断指定坐标的点是否在矩形内部
/// </summary>
/// <param name="ptX">X坐标</param>
/// <param name="ptY">Y坐标</param>
/// <returns></returns>
public bool IsPointFInRectangle(float ptX, float ptY)
{
return (ptX >= x && ptX <= (x + width) && ptY >= y && ptY <= (y + height));
}
public static CustomRectangle ToCustomRectangle(RectangleF re)
{
CustomRectangle cus = new CustomRectangle();
cus.X = re.X;
cus.Y = re.Y;
cus.Width = re.Width;
cus.Height = re.Height;
return cus;
}
/// <summary>
/// 根据Rectangle产生一个CustomRectangle对象
/// </summary>
/// <param name="rect"></param>
/// <returns></returns>
public static CustomRectangle FromRectangle(Rectangle rect)
{
return new CustomRectangle(rect.Left, rect.Top, rect.Width, rect.Height);
}
/// <summary>
/// 根据RectangleF产生一个CustomRectangle对象
/// </summary>
/// <param name="rect"></param>
/// <returns></returns>
public static CustomRectangle FromRectangleF(RectangleF rect)
{
return new CustomRectangle(rect.Left, rect.Top, rect.Width, rect.Height);
}
/// <summary>
/// 获取边框的圆角矩形路径(用于绘图)
/// </summary>
/// <returns></returns>
public GraphicsPath GetRoundRectBorderPath(float radus)
{
GraphicsPath path = new GraphicsPath();
path.AddArc(x, y, radus * 2, radus * 2, 180, 90);
path.AddArc(Right - radus * 2, y, radus * 2, radus * 2, 270, 90);
path.AddArc(Right - radus * 2, Bottom - radus * 2, radus * 2, radus * 2, 0, 90);
path.AddArc(x, Bottom - radus * 2, radus * 2, radus * 2, 90, 90);
path.CloseFigure();
return path;
}
/// <summary>
/// 获取六边形绘图路径
/// </summary>
/// <returns></returns>
public GraphicsPath GetHexagonBorderPath()
{
GraphicsPath path = new GraphicsPath();
List<PointF> pts = new List<PointF>();
pts.Add(new PointF(x + width / 2f, y));
pts.Add(new PointF(Right, y + height / 4f));
pts.Add(new PointF(Right, y + height * 3f / 4f));
pts.Add(new PointF(x + width / 2f, Bottom));
pts.Add(new PointF(x, y + height * 3f / 4f));
pts.Add(new PointF(x, y + height / 4f));
path.AddPolygon(pts.ToArray());
return path;
}
/// <summary>
/// 获取倒三角形路径
/// </summary>
/// <returns></returns>
public GraphicsPath GetTrianglePath()
{
GraphicsPath path = new GraphicsPath();
List<PointF> pts = new List<PointF>();
pts.Add(new PointF(x, y));
pts.Add(new PointF(Right, y));
pts.Add(new PointF(x + width / 2f, Bottom));
path.AddPolygon(pts.ToArray());
return path;
}
/// <summary>
/// 根据鼠标位置确定高亮区域,1、2、3、4表示中间4个区域,其他为-1
/// </summary>
/// <param name="pt"></param>
/// <returns></returns>
public int GetHotPosition(Point pt)
{
Point pt1, pt2, pt3, pt4, pt0;
pt1 = new Point((int)(x + width / 6), (int)(y + height / 6));
pt2 = new Point((int)(Right - width / 6), (int)(y + height / 6));
pt3 = new Point((int)(Right - width / 6), (int)(Bottom - height / 6));
pt4 = new Point((int)(x + width / 6), (int)(Bottom - height / 6));
pt0 = CenterPoint.ToPoint();
GraphicsPath path1 = new GraphicsPath();
path1.AddPolygon(new Point[] { pt1, pt2, pt0 });
if (path1.IsVisible(pt)) return 1;
GraphicsPath path2 = new GraphicsPath();
path2.AddPolygon(new Point[] { pt2, pt3, pt0 });
if (path2.IsVisible(pt)) return 2;
GraphicsPath path3 = new GraphicsPath();
path3.AddPolygon(new Point[] { pt3, pt4, pt0 });
if (path3.IsVisible(pt)) return 3;
GraphicsPath path4 = new GraphicsPath();
path4.AddPolygon(new Point[] { pt4, pt1, pt0 });
if (path4.IsVisible(pt)) return 4;
return -1;
}
/// <summary>
/// 获取高亮矩形区域
/// </summary>
/// <param name="hotPosition">1,2,3,4;0表示全选</param>
/// <returns></returns>
public Rectangle GetSubRectangle(int hotPosition)
{
if (hotPosition == 1)
return new Rectangle((int)x, (int)y, (int)width, (int)(height / 2));
else if (hotPosition == 2)
return new Rectangle((int)(x + width / 2), (int)y, (int)(width / 2), (int)height);
else if (hotPosition == 3)
return new Rectangle((int)x, (int)(y + height / 2), (int)width, (int)(height / 2));
else if (hotPosition == 4)
return new Rectangle((int)x, (int)y, (int)(width / 2), (int)height);
else
return ToRectangle();
}
#endregion
}
}
写完后生成一下项目或者类库,在工具栏左侧会出现时钟控件,这时只要将它拖曳到窗口即可。