using ryCommon;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static ryControls.FhChinaCalendar;
using static ScintillaNETV2.Style;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Header;
namespace ryControls
{
///
/// 日历控件,不含月份和年份选择
///
[DefaultEvent("OnDateTimeChanged")]
[Description("农历日历控件")]
[ToolboxBitmap(typeof(System.Windows.Forms.MonthCalendar))]
public partial class PaChinaCalendar : UserControl
{
///
///
///
public PaChinaCalendar() : base()
{
InitializeComponent();
this.DoubleBuffered = true;
}
///
/// 选择的日期变化时激发
///
[Description("选择的日期变化时激发")]
public event EventHandler OnDateTimeChanged;
private int _itemSize = 80;
///
/// 单项大小
///
[Description("单项大小")]
public int ItemSize
{
get
{
return _itemSize;
}
set
{
if (_itemSize == value) { return; }
_itemSize =value;
this.Invalidate();
}
}
private string holiday_folder = ".\\SysDb\\Holidays";
///
/// 放假数据表
///
public string HolidayFolder
{
get
{
return holiday_folder;
}
set
{
if (holiday_folder == value) { return; }
holiday_folder = value;
this.Invalidate();
}
}
private DateTime _DateTime = DateTime.Now;
///
/// 当前选择的时间
///
[Description("当前选择的时间")]
public DateTime DateTime
{
get
{
return _DateTime;
}
set
{
if (_DateTime.Date == value.Date) { return; }
_DateTime = value;
this.Invalidate();
OnDateTimeChanged?.Invoke(this, new EventArgs());
}
}
private Font _DayFont = new Font("微软雅黑", 14,FontStyle.Bold);
///
/// 阳历天字体
///
[Description("阳历天字体")]
public Font DayFont
{
get
{
return _DayFont;
}
set
{
if (_DayFont == value) { return; }
_DayFont = value;
this.Invalidate();
}
}
private Font _ChinaDayFont = new Font("微软雅黑", 10);
///
/// 农历天字体
///
[Description("农历天字体")]
public Font ChinaDayFont
{
get
{
return _ChinaDayFont;
}
set
{
if (_ChinaDayFont == value) { return; }
_ChinaDayFont = value;
this.Invalidate();
}
}
private int _HeaderHeight = 30;
///
/// 标头高度(星期高度)
///
[Description("标头高度(星期高度)")]
public int HeaderHeight
{
get
{
return _HeaderHeight;
}
set
{
if (_HeaderHeight == value) { return; }
_HeaderHeight = value;
this.Invalidate();
}
}
private int _CellSpace = 1;
///
/// 列间距
///
[Description("列间距")]
public int CellSpace
{
get
{
return _CellSpace;
}
set
{
if (_CellSpace == value) { return; }
_CellSpace = value;
this.Invalidate();
}
}
private int _RowSpace = 1;
///
/// 行间距
///
[Description("行间距")]
public int RowSpace
{
get
{
return _RowSpace;
}
set
{
if (_RowSpace == value) { return; }
_RowSpace = value;
this.Invalidate();
}
}
private Font _HeaderFont = new Font("微软雅黑",11);
///
/// 星期字体
///
[Description("星期字体")]
public Font HeaderFont
{
get
{
return _HeaderFont;
}
set
{
if (_HeaderFont == value) { return; }
_HeaderFont = value;
this.Invalidate();
}
}
private Color _HolidayForeColor = Color.Orange;
///
/// 节日颜色
///
[Description("节日颜色")]
public Color HolidayForeColor
{
get
{
return _HolidayForeColor;
}
set
{
if (_HolidayForeColor == value) { return; }
_HolidayForeColor = value;
this.Invalidate();
}
}
private int _Mode = 0;
///
/// 模式(0表示日模式,1表示月份展示模式,2表示年份展示模式)
///
[Description("模式(0表示日模式,1表示月份展示模式,2表示年份展示模式)")]
public int Mode
{
get
{
return _Mode;
}
set
{
if (_Mode == value) { return; }
if(value < 0 && value > 2) { return; }
_Mode = value;
ClearPaint = true;
this.Invalidate();
}
}
private bool ClearPaint { get; set; } = false;
private int _PaddingLeft = 0;
///
/// 左边距
///
[Description("左边距")]
public int PaddingLeft
{
get
{
return _PaddingLeft;
}
set
{
if (_PaddingLeft == value) { return; }
_PaddingLeft = value;
this.Invalidate();
}
}
private int _PaddingTop = 0;
///
/// 顶部边距
///
[Description("顶部边距")]
public int PaddingTop
{
get
{
return _PaddingTop;
}
set
{
if (_PaddingTop == value) { return; }
_PaddingTop = value;
this.Invalidate();
}
}
///
///
///
///
protected override void OnSizeChanged(EventArgs e)
{
this.Invalidate();
base.OnSizeChanged(e);
}
///
/// 单击某个日期事件
///
public event ClickItemHandler OnClickItem;
///
///
///
///
protected override void OnMouseDown(MouseEventArgs e)
{
//获取点击的是第几行(1-6行)
using (lock_dayitem.Read())
{
for (int i = 0; i < list_rect.Count; i++)
{
if (list_rect[i].Rect.Contains(e.Location))
{
DateTime = list_rect[i].Date;
OnClickItem?.Invoke(this, list_rect[i].Date);
break;
}
}
}
base.OnMouseDown(e);
}
///
///
///
///
protected override void OnKeyDown(KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Right:
DateTime = DateTime.AddDays(1);
break;
case Keys.Left:
DateTime = DateTime.AddDays(-1);
break;
case Keys.Up://方向键不反应
DateTime = DateTime.AddDays(-7);
break;
case Keys.Down:
DateTime = DateTime.AddDays(7);
break;
case Keys.Enter:
OnClickItem?.Invoke(this, DateTime);
break;
}
base.OnKeyDown(e);
}
///
///
///
///
///
protected override bool IsInputKey(Keys keyData)
{
// 确保UserControl接收所有键盘输入
if (keyData == Keys.Up || keyData == Keys.Down || keyData == Keys.Left || keyData == Keys.Right)
{
return true;
}
return base.IsInputKey(keyData);
}
///
/// 是否处于设计模式
///
///
public bool IsDesignMode()
{
bool returnFlag = this.DesignMode;
#if DEBUG
if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
{
returnFlag = true;
}
else if (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv")
{
returnFlag = true;
}
#endif
return returnFlag;
}
private DateTime dt_start { get; set; } = DateTime.Now;
private readonly List list_rect = [];
private readonly rySafe.UsingLock