delphi中DateTimePicker同时修改日期和时间的实现

第一种方法:

 

第一步: 修改属性

    1、kind -> dtktime 也可以是dtkdate 但是在用dtkdate的时候需要将datemode属性修改成dmUpDown

    2、parseinput -> true

第二步:在onUserInput事件中写一句代码

DateTimePicker1.DateTime:=DateAndTime;

 

注:以上方法虽然可以达到目的,但却会引发新的问题。(这时候控件中可以输入任意字符)

第二种方法:

1。修改delphi的包含Tdatatimepicker的单元ComCtrls.pas  

     搜索:TDateTimePicker.CNNotify(var   Message:   TWMNotify);  在这个函数中,代码:

 procedure   TDateTimePicker.CNNotify(var   Message:   TWMNotify);  
  var  
      DT:   TDateTime;  
      AllowChange:   Boolean;  
  begin  
      with   Message,   NMHdr^   do  
      begin  
          Result   :=   0;  
          case   code   of  
              DTN_CLOSEUP:  
                  begin  
                      FDroppedDown   :=   False;  
                      SetDate(SystemTimeToDateTime(FLastChange));  
                      if   Assigned(FOnCloseUp)   then   FOnCloseUp(Self);  
                  end;  
              DTN_DATETIMECHANGE:  
                  begin  
                      with   PNMDateTimeChange(NMHdr)^   do  
                      begin  
                          if   FDroppedDown   and   (dwFlags   =   GDT_VALID)   then  
                          begin  
                              FLastChange   :=   st;  
                              FDateTime   :=   SystemTimeToDateTime(FLastChange);  
                          end  
                          else   begin  
                              if   FShowCheckbox   and   IsBlankSysTime(st)   then  
                                  FChecked   :=   False  
                              else   if   dwFlags   =   GDT_VALID   then  
                              begin  
                                  FLastChange   :=   st;  
                                  DT   :=   SystemTimeToDateTime(st);  
                            //   if   Kind   =   dtkDate   then   SetDate(DT)  
                            //       else   SetTime(DT);  
                             
  //以上原代码注释掉  
  //My   change  
                                  SetDate(DT);  
                                  SetTime(DT);  
   
  //以上2行我更改  
                                  if   FShowCheckbox   then   FChecked   :=   True;  
                              end;  
                          end;  
                          Change;  
                      end;  
                  end;  
              DTN_DROPDOWN:  
                  begin  
                      DateTimeToSystemTime(Date,   FLastChange);  
                      FDroppedDown   :=   True;  
                      if   Assigned(FOnDropDown)   then   FOnDropDown(Self);  
                  end;  
              DTN_USERSTRING:  
                  begin  
                      with   PNMDateTimeString(NMHdr)^   do  
                      begin  
                          DT   :=   StrToDateTime(pszUserString);  
                          if   Assigned(FOnUserInput)   then  
                          begin  
                              AllowChange   :=   True;  
                              FOnUserInput(Self,   pszUserString,   DT,   AllowChange);  
                              dwFlags   :=   Ord(not   AllowChange);  
                          end  
                          else  
                              dwFlags   :=   Ord(False);  
                          DateTimeToSystemTime(DT,   st);  
                      end;  
                  end;  
          else  
              inherited;  
          end;  
      end;  
  end;

2.修改以后要重新编译这个源文件

你可能感兴趣的:(Delphi)