1. 程式人生 > >delphi中DateTimePicker同時修改日期和時間的實現

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.修改以後要重新編譯這個原始檔