Why does TDateTimePicker.Checked always return True in Windows 7?

I have an application built into Delphi 2007 with a TDateTimePicker form in the form. This time ShowCheckbox has the ShowCheckbox property set to True , which next to the date or time displays a checkbox that is automatically selected whenever the date is selected by the user or when the date or time is changed by code. The status of this flag can also be controlled manually by the user, and its status can be determined by the Checked property.

The following code shows how to determine the state of this flag in the OnChange event:

 procedure TForm1.FormCreate(Sender: TObject); begin DateTimePicker1.ShowCheckbox := True; end; procedure TForm1.DateTimePicker1Change(Sender: TObject); begin ShowMessage('Checked: ' + BoolToStr(DateTimePicker1.Checked, True)); end; 

The above code works as expected in Windows XP, but in Windows 7, the Checked property always returns True regardless of the actual state of this flag.

Why does the Checked property always return True, even if the check box is not selected? Is there any way to fix or trick this somehow?

PS My application uses Windows themes

+6
source share
1 answer

This is a known issue in the implementation of the Delphi date picker control (fixed in Delphi 2009, as @Remy pointed out in his comment). To request the Date Picker, either DTM_GETSYSTEMTIME or DateTime_GetSystemtime , which internally sends this message, should be used. If the message (or macro) returns GDT_VALID and GDT_VALID used (in Delphi, when the ShowCheckbox property is True), this indicates that the control is checked and this control contains a valid time.

Here is an example of using the specified macro to determine the state of a flag:

 uses CommCtrl; procedure TForm1.DateTimePicker1Change(Sender: TObject); var SysTime: SYSTEMTIME; begin if DateTime_GetSystemTime(DateTimePicker1.Handle, @SysTime) = GDT_VALID then ShowMessage('Check box is checked!') else ShowMessage('Check box is not checked!'); end; 

So, you can make a helper function like this to bypass the incorrect Delphi implementation:

 uses CommCtrl; function IsDateTimePickerChecked(ADateTimePicker: TDateTimePicker): Boolean; var SysTime: SYSTEMTIME; begin Result := DateTime_GetSystemTime(ADateTimePicker.Handle, @SysTime) = GDT_VALID; end; procedure TMyForm.ButtonOneClick(Sender: TObject); begin if IsDateTimePickerChecked(DateTimePicker1) then ShowMessage('Check box is checked!') else ShowMessage('Check box is not checked!'); end; 
+9
source

Source: https://habr.com/ru/post/927843/


All Articles