在组合框选择上更改DateTimePicker样式失败 [英] Change DateTimePicker style on combobox selection fails
问题描述
我正在尝试更改组合框' CBS_SELCHANGE
通知中 DateTimePicker
控件的样式和日期格式。
当用户选择一个选项时,日期时间选择器应具有旋转控制并且仅显示年份。如果用户选择其他选项,则应将样式重置为短日期样式,日期格式应将日期显示为 dd.mm.yyyy
。
以下是我到目前为止的代码:
I am trying to change the style and date format of the DateTimePicker
control on combobox' CBS_SELCHANGE
notification.
When user selects one option, datetime picker should have spin control and show only year. If user selects other options, style should be reset to short date style and date format should show date as dd.mm.yyyy
.
Here is the code I have so far:
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_COMBO5:
{
if (HIWORD(wParam) == CBN_SELENDOK)
{
int iSelected = ComboBox_GetCurSel((HWND)lParam);
switch (i)
{
case 1:
{
DWORD_PTR dwStyle =
GetWindowLongPtr(GetDlgItem(hDlg,
IDC_DATETIMEPICKER1), GWL_STYLE);
// remove short date style
dwStyle &= ~DTS_SHORTDATEFORMAT;
// add spin control
dwStyle |= DTS_UPDOWN;
SetWindowLongPtr(GetDlgItem(hDlg,
IDC_DATETIMEPICKER1), GWL_STYLE, dwStyle);
// set date format
DateTime_SetFormat(GetDlgItem(hDlg,
IDC_DATETIMEPICKER1), L"yyyy");
}
break;
case -1:
case 0:
case 2:
default:
{
DWORD_PTR dwStyle =
GetWindowLongPtr(GetDlgItem(hDlg,
IDC_DATETIMEPICKER1), GWL_STYLE);
// remove spin control
dwStyle &= ~DTS_UPDOWN;
// restore short date format
dwStyle |= DTS_SHORTDATEFORMAT;
SetWindowLongPtr(GetDlgItem(hDlg,
IDC_DATETIMEPICKER1), GWL_STYLE, dwStyle);
// set date format
DateTime_SetFormat(GetDlgItem(hDlg,
IDC_DATETIMEPICKER1), L"dd'|'MM'|'yyyy");
}
break;
}
}
}
break;
// other cases...
格式变换正确,但旋转控制根本没有添加。
通过调试器,我可以看到它通过我的代码和抛出没有例外 - >我去了 Debug-> Exceptions
并检查了 Thrown
下的所有内容。
我使用Visual Studio 2013在Windows 7 x86上工作。
我的目标是XP向前。
你能帮我修改我的代码以便我能达到预期的行为吗?
Format gets changed properly, but spin control doesn't get added at all.
Running through debugger, I can see that it goes through my code and throws no exceptions -> I went to Debug->Exceptions
and checked everything under Thrown
.
I work on Windows 7 x86, using Visual Studio 2013.
I am targeting XP onward.
Can you help me to fix my code so I can achieve desired behavior?
推荐答案
理查德是对的,控件中的某些样式无法改变在运行时,但必须在创建时初始化。
您可以动态地使用
a)重新创建这样的控制行为,或者b $ bb)在同一个地方创建2个控件,只有一个是可见的,另一个是隐藏的。它需要一些可怕的代码(交换机处于活动状态),但它可以工作。
Richard is right, that some style in controls cant be changed in runtime, but must initialized in creation time.
You can work around it with
a) dynamically recreating such control behavior, or
b) create 2 controls in the same place and only one is visible and the other is hidden. It needs some horrific code (switch which is active) but it works.
这篇关于在组合框选择上更改DateTimePicker样式失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!