[AntdUI] 08 Calendar日历控件创建及事件处理

nanyi 13小时前 48 v 1.0.0.0 2026-06-21

 import win.ui;
import dotNet.AntdUI;
import dotNet;
import JSON;
/*DSG{{*/
var winform = win.form(text="AntdUI Calendar - 2026 Holidays";right=1120;bottom=720;border="thin")
winform.add(
custom_base={cls="custom";left=0;top=0;right=1122;bottom=720;ah=1;aw=1;db=1;disabled=1;dl=1;dr=1;dt=1;hide=1;z=1};
custom_btn_clear_badge={cls="custom";left=820;top=415;right=1085;bottom=457;dr=1;dt=1;z=8};
custom_btn_newyear={cls="custom";left=820;top=305;right=1085;bottom=347;dr=1;dt=1;z=6};
custom_btn_today={cls="custom";left=820;top=360;right=1085;bottom=402;dr=1;dt=1;z=7};
custom_calendar={cls="custom";left=28;top=75;right=790;bottom=680;db=1;dl=1;dr=1;dt=1;z=3};
custom_info={cls="custom";left=820;top=120;right=1085;bottom=280;dr=1;dt=1;z=5};
custom_lbl_switch={cls="custom";left=833;top=482;right=965;bottom=522;dr=1;dt=1;z=11};
custom_log={cls="custom";left=820;top=585;right=1085;bottom=680;db=1;dr=1;dt=1;z=10};
custom_log_title={cls="custom";left=820;top=545;right=1085;bottom=578;dr=1;dt=1;z=9};
custom_side_title={cls="custom";left=820;top=78;right=1085;bottom=112;dr=1;dt=1;z=4};
custom_switch_chinese={cls="custom";left=958;top=484;right=1013;bottom=519;dr=1;dt=1;z=12};
custom_title={cls="custom";left=28;top=20;right=650;bottom=58;dl=1;dt=1;z=2}
)
/*}}*/

// Message.success needs an AntdUI form or control as owner.
var baseForm = AntdUI.BaseForm();
baseForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
baseForm.Dock = System.Windows.Forms.DockStyle.Fill;
dotNet.setParent(baseForm, winform.custom_base);

// ---------- Fonts ----------
var fontTitle = System.Drawing.Font("Microsoft YaHei", 14, System.Drawing.FontStyle.Bold);
var fontNormal = System.Drawing.Font("Microsoft YaHei", 12);
var fontTip = System.Drawing.Font("Microsoft YaHei", 9);

// ---------- Helpers ----------
var showSuccess = function(text){
    AntdUI.Message.success(baseForm, text);
}

var addHostLabel = function(host,text,font){
    var label = AntdUI.Label(host);
    label.Text = text;
    label.Font = font || fontNormal;
    label.Dock = System.Windows.Forms.DockStyle.Fill;
    return label;
}

var fmtDate = function(dt){
    if(!dt) return "";
    return dt.ToString("yyyy-MM-dd");
}

var getBadgeColor = function(kind){
    if(kind == "holiday") return System.Drawing.Color.FromArgb(255,245,34,45);
    return System.Drawing.Color.FromArgb(255,22,119,255);
}

// ---------- 2026 Holiday JSON demo data ----------
var holidayJson = `[
    {"date":"2026-01-01","name":"元旦","kind":"holiday","note":"New Year's Day"},
    {"date":"2026-02-16","name":"除夕","kind":"holiday","note":"Chinese New Year's Eve"},
    {"date":"2026-02-17","name":"春节","kind":"holiday","note":"Spring Festival"},
    {"date":"2026-04-05","name":"清明","kind":"holiday","note":"Qingming Festival"},
    {"date":"2026-05-01","name":"劳动节","kind":"holiday","note":"Labor Day"},
    {"date":"2026-06-19","name":"端午","kind":"holiday","note":"Dragon Boat Festival"},
    {"date":"2026-09-25","name":"中秋","kind":"holiday","note":"Mid-Autumn Festival"},
    {"date":"2026-10-01","name":"国庆","kind":"holiday","note":"National Day"}
]`;

var holidayData = JSON.parse(holidayJson);

// Used to display details when a date is selected.
var holidayMap = {};
for(i,item in holidayData){
    holidayMap[item.date] = item;
}

var makeBadgeList = function(){
    // DateBadge is AntdUI badge data item. Create .NET List<AntdUI.DateBadge>.
    var list = dotNet.list("AntdUI.DateBadge");

    for(i,item in holidayData){
        var badge = AntdUI.DateBadge(
            item.date,
            item.name,
            getBadgeColor(item.kind)
        );
        badge.Radius = 4;
        list.Add(badge);
    }

    return list;
}

var getDateInfo = function(dateStr){
    var item = holidayMap[dateStr];
    if(!item){
        return dateStr + '\r\n' + "No holiday badge for this date.";
    }

    return dateStr + '\r\n'
        + "Name: " + item.name + '\r\n'
        + "Type: Holiday" + '\r\n'
        + "Note: " + item.note;
}

// ---------- Titles ----------
addHostLabel(winform.custom_title,"Calendar: 2026 Holiday Badges",fontTitle);
addHostLabel(winform.custom_side_title,"Properties / Actions",fontTitle);
addHostLabel(winform.custom_log_title,"Event log",fontTitle);

// ---------- Calendar ----------
var calendar = AntdUI.Calendar(winform.custom_calendar);
calendar.Dock = System.Windows.Forms.DockStyle.Fill;
calendar.Font = fontNormal;

// Initial date.
calendar.Value = System.DateTime(2026,2,17);

// Full calendar view is better for standalone Calendar demo.
calendar.Full = true;

// Show Chinese lunar information.
calendar.ShowChinese = true;

// Show the Today button in the calendar panel.
calendar.ShowButtonToDay = true;

calendar.Radius = 8;

// Generate badges from JSON data.
calendar.SetBadge(makeBadgeList());

// ---------- Info and log ----------
var lblInfo = AntdUI.Label(winform.custom_info);
lblInfo.Font = fontTip;
lblInfo.Dock = System.Windows.Forms.DockStyle.Fill;

var lblLog = AntdUI.Label(winform.custom_log);
lblLog.Font = fontTip;
lblLog.Dock = System.Windows.Forms.DockStyle.Fill;
lblLog.Text = "";

var logText = "";
var appendLog = function(text){
    logText = time().format("%H:%M:%S") + "  " + text + '\r\n' + logText;
    lblLog.Text = logText;
}

var updateInfoByDate = function(dt){
    lblInfo.Text = "Properties:" + '\r\n'
        + "• Value" + '\r\n'
        + "• Full calendar view" + '\r\n'
        + "• ShowChinese" + '\r\n'
        + "• ShowButtonToDay" + '\r\n'
        + "• SetBadge(JSON holiday data)" + '\r\n\r\n'
        + getDateInfo(fmtDate(dt));
}

updateInfoByDate(calendar.Value);
appendLog("Calendar initialized: " + fmtDate(calendar.Value));

// ---------- Events ----------
calendar.DateChanged = function(sender,e){
    var dt = e.get_Value();
    updateInfoByDate(dt);
    appendLog("DateChanged: " + fmtDate(dt));
}

calendar.ItemMouseClick = function(sender,e){
    var dt = e.get_Item();
    var text = e.get_Text();
    updateInfoByDate(dt);
    appendLog("ItemMouseClick: " + fmtDate(dt) + " text=" + text);
}

// ---------- Actions ----------
var setCalendarDate = function(dt){
    calendar.Value = dt;
    calendar.SetDate(dt);
    updateInfoByDate(dt);
    appendLog("Set date: " + fmtDate(dt));
}

// ---------- Buttons ----------
var btnNewYear = AntdUI.Button(winform.custom_btn_newyear);
btnNewYear.Text = "Happy New Year";
btnNewYear.Font = fontNormal;
btnNewYear.Type = AntdUI.TTypeMini.Primary;
btnNewYear.Dock = System.Windows.Forms.DockStyle.Fill;
btnNewYear.Click = function(sender,e){
    setCalendarDate(System.DateTime(2026,2,17));
    showSuccess("Happy New Year.");
}

var btnToday = AntdUI.Button(winform.custom_btn_today);
btnToday.Text = "Today";
btnToday.Font = fontNormal;
btnToday.Dock = System.Windows.Forms.DockStyle.Fill;
btnToday.Click = function(sender,e){
    var dt = System.DateTime.Now.Date;
    setCalendarDate(dt);
    showSuccess("Jump to today");
}

var btnClearBadge = AntdUI.Button(winform.custom_btn_clear_badge);
btnClearBadge.Text = "Clear badges";
btnClearBadge.Font = fontNormal;
btnClearBadge.Type = AntdUI.TTypeMini.Warn;
btnClearBadge.Dock = System.Windows.Forms.DockStyle.Fill;
btnClearBadge.Click = function(sender,e){
    calendar.SetBadge(dotNet.list("AntdUI.DateBadge"));
    appendLog("Badges cleared");
    showSuccess("Badges cleared");
}

// ---------- Switch ----------
addHostLabel(winform.custom_lbl_switch,"Show Chinese",fontNormal);

var switchChinese = AntdUI.Switch(winform.custom_switch_chinese);
switchChinese.Dock = System.Windows.Forms.DockStyle.Fill;
switchChinese.Font = fontTip;
switchChinese.Checked = calendar.ShowChinese;
switchChinese.CheckedChanged = function(sender,e){
    calendar.ShowChinese = e.get_Value();
    appendLog("ShowChinese = " + tostring(calendar.ShowChinese));
}

// ---------- Dispose ----------
winform.onClose = function(){
    fontTitle.Dispose();
    fontNormal.Dispose();
    fontTip.Dispose();
}

winform.show();
win.loopMessage();

下期预告:Alert警告提示控件

最新回复 (0)
返回