显示不符合 ISO-8601 标准的周数的 MonthCalendar 控件

MonthCalendar control displaying non ISO-8601 compliant week numbers

我遇到了 MonthCalendar Windows 表单 UI 控件的问题。启用 ShowWeekNumbers 属性 后,它会将 2016 年的第 1 周显示为包含 1 月 1 日(即星期五)的那一周。这不符合 ISO-8601,ISO-8601 规定一年的第一周是包含该年第一个星期四的那一周(第一个 4 天的一周)。

MonthCalendar 控件 (SysMonthCal32) 是通用控件库 (comctl32.dll) 的一部分。它在显示周数时使用 MCS_WEEKNUMBERS 样式。在 MSDN 站点的 Month Calendar Control Styles 页面上,它在 MSC_WEEKNUMBERS 的描述中提供了以下声明: "Week 1 is defined as the first week that contains at least four days." 不幸的是,这与我在使用该控件时遇到的情况相反。

这里是 photo of the MonthCalendar control,显示了上述问题。

周编号的计算由操作系统的用户区域设置决定。它不能通过修改主执行线程的 CurrentCulture 和 CurrentUICulture 属性来影响。如此 Microsoft Support article:

This behavior occurs because the DateTimePicker control and the MonthCalendar control are Microsoft Windows common controls. Therefore, the operating system's user locale determines the user interface of these controls.

遗憾的是,无法在应用程序运行时设置操作系统的用户区域设置。为实现 ISO-8601 合规性,自定义控件将是必要的。这些 culture-aware MonthCalendar and DateTimePicker controls on CodeProject 应该做得很好。

我收到了关于不相关号码的投诉。 运行 填写 Windows 表格申请时显示的工作周。

具体来说,问题出在 2016 年 1 月 1 日和 2 日被识别为下一周,在本例中为 2016 年的第一周 see the application displaying the wrong no. of the week, meaning 2 instead of 1

虽然在大多数日历中,对于奥地利、德国、瑞士等国家/地区,2016 年的第一周被认为是从 1 月 4 日开始的那一周。

解决此问题的快速解决方案是在客户端 PC 上添加 iFirstweekofYear 寄存器中的 2 而不是 0(通过 运行ning regedit->HKEY_CURRENT_USER->Control Panel- 访问此寄存器- >国际->iFirstWeekOfYear)

现在,当我再次 运行 应用程序时,会显示一年中正确的星期: see the application displaying the correct no. of the week, meaning 1 instead of 2

希望对您有所帮助。