问题描述
在vb.net或c#应用程序的winforms环境中使用传统的DateTimePicker控件,我需要更改从正常的星期日到星期六到星期二到星期一显示星期的方式.我已经搜索了Google和Stack,却没有发现任何结果.创建自定义控件没有问题.我只是不知道从哪里开始.
Using the traditional DateTimePicker control in a winforms environment in a vb.net or c# application I need to change how the week is displayed from the normal Sunday through Saturday to Tuesday through Monday. I have scoured Google and Stack and found nothing on this. I have no issue with creating a custom control. I just don't know where to start.
推荐答案
您可以使用 SendMessage 发送 MCM_SETFIRSTDAYOFWEEK
消息发送至DateTimePicker的MonthCalendar(请在文档中阅读与此消息相关的注释).
You can use SendMessage to send a MCM_SETFIRSTDAYOFWEEK
message to the MonthCalendar of the DateTimePicker (read the notes related to this message in the Docs).
您首先发送 DTM_GETMONTHCAL
消息以获取MonthCalendar控件的句柄.如图所示,您可以在DateTimePicker的 DropDown
事件处理程序中发送此消息.
如果该句柄有效,则设置发送 MCM_SETFIRSTDAYOFWEEK
的第一天. lParam
值确定MonthCalendar中显示的第一天:
You first send a DTM_GETMONTHCAL
message to get the handle of the MonthCalendar control. As shown, you can send this message in the DropDown
event handler of the DateTimePicker.
If the handle is valid, set the first day sending MCM_SETFIRSTDAYOFWEEK
.
The lParam
value determines the first day shown in the MonthCalendar:
0
=星期一, 1
=星期二,等等
0
= Monday, 1
= Tuesday etc.
如果要构建自定义控件(最好是IMO),则可以在此处找到一个预构建的控件:
如何将DateTimePicker下拉菜单设置为仅选择年"或月"?
If you want to build a custom control (IMO, preferable), you can find one pre-built here:
How can I set the DateTimePicker dropdown to select Years or Months only?
它显示了如何处理DateTimePicker及其MonthCalendar下拉菜单,以更改当前的View和类似的任务.
It shows how to deal with the DateTimePicker and its MonthCalendar dropdown, to change the current View and similar tasks.
internal const int DTM_FIRST = 0x1000;
internal const int DTM_GETMONTHCAL = DTM_FIRST + 8;
internal const int MCM_FIRST = 0x1000;
internal const int MCM_SETFIRSTDAYOFWEEK = MCM_FIRST + 15;
internal enum MCWeekDay : int
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
internal static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
internal void MonthCalendarSetFirstDayOfWeek(IntPtr dtpHandle, MCWeekDay weekDay)
{
IntPtr hWndCal = SendMessage(dtpHandle, DTM_GETMONTHCAL, 0, 0);
if (hWndCal != IntPtr.Zero) {
SendMessage(hWndCal, MCM_SETFIRSTDAYOFWEEK, 0, (int)weekDay);
}
}
private void dateTimePicker1_DropDown(object sender, EventArgs e)
{
MonthCalendarSetFirstDayOfWeek((sender as Control).Handle, MCWeekDay.Tuesday);
}
VB.Net版本:
Imports System.Runtime.InteropServices
Friend Const DTM_FIRST As Integer = &H1000
Friend Const DTM_GETMONTHCAL As Integer = DTM_FIRST + 8
Friend Const MCM_FIRST As Integer = &H1000
Friend Const MCM_SETFIRSTDAYOFWEEK As Integer = MCM_FIRST + 15
Friend Enum MCWeekDay As Integer
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
End Enum
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
Friend Shared Function SendMessage(hWnd As IntPtr, Msg As Integer, wParam As Integer, lParam As Integer) As IntPtr
End Function
Friend Sub MonthCalendarSetFirstDayOfWeek(dtpHandle As IntPtr, weekDay As MCWeekDay)
Dim hWndCal As IntPtr = SendMessage(dtpHandle, DTM_GETMONTHCAL, 0, 0)
If hWndCal <> IntPtr.Zero Then
SendMessage(hWndCal, MCM_SETFIRSTDAYOFWEEK, 0, weekDay)
End If
End Sub
Private Sub DateTimePicker1_DropDown(sender As Object, e As EventArgs) Handles DateTimePicker1.DropDown
MonthCalendarSetFirstDayOfWeek(DirectCast(sender, Control).Handle, MCWeekDay.Tuesday)
End Sub
这篇关于在DateTimePicker控件中更改周显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!