我想用API修改系统时间,有个问题请教???(100分)

  • 主题发起人 主题发起人 liyonggang
  • 开始时间 开始时间
L

liyonggang

Unregistered / Unconfirmed
GUEST, unregistred user!
如何取出datetimepicker中的值(年、月、日),分别赋给字符变量S1、S2、S3<br>最好给出代码,我是初学者,谢谢!
 
可以用procedure DecodeDate(Date: TDateTime; var Year, Month, Day: Word);来取出Date中的年、月、日,Delphi中的例子:<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; Present: TDateTime;<br>&nbsp; Year, Month, Day: Word;<br>&nbsp;begin<br>&nbsp; Present:= Now;<br>&nbsp; DecodeDate(Present, Year, Month, Day);<br>&nbsp; Label1.Caption := 'Today is Day ' + IntToStr(Day) + ' of Month '<br>&nbsp; &nbsp; + IntToStr(Month) + ' of Year ' + IntToStr(Year);<br>end;<br>
 
var<br>&nbsp;s1, s2, s3: Word;<br>begin<br>&nbsp;DecodeDate(DataTimerPicker.Date, s1, s2, s3); &nbsp;
 
var<br>&nbsp; hh,mm,ss,ms:word;<br>begin<br>&nbsp; DecodeTime(DateTime,hh,mm,ss,ms);<br>&nbsp; hx:=hh;<br>&nbsp; mx:=mm;<br>&nbsp; sx:=ss;<br>end;<br>设置系统时间 change the system time?<br><br>{1.} <br><br>{ <br>&nbsp; For Windows 9X/ME/NT/2000/XP: <br><br>&nbsp; The SetLocalTime function fails if the calling process does not have <br>&nbsp; the SE_SYSTEMTIME_NAME privilege. This privilege is disabled by default. <br>&nbsp; Use the AdjustTokenPrivileges function to enable this privilege. <br>} <br><br>function SetPCSystemTime(dDateTime: TDateTime): Boolean; <br>const <br>&nbsp; SE_SYSTEMTIME_NAME = 'SeSystemtimePrivilege'; <br>var <br>&nbsp; hToken: THandle; <br>&nbsp; ReturnLength: DWORD; <br>&nbsp; tkp, PrevTokenPriv: TTokenPrivileges; <br>&nbsp; luid: TLargeInteger; <br>&nbsp; dSysTime: TSystemTime; <br>begin <br>&nbsp; Result := False; <br>&nbsp; if (Win32Platform = VER_PLATFORM_WIN32_NT) then <br>&nbsp; begin <br>&nbsp; &nbsp; if OpenProcessToken(GetCurrentProcess, <br>&nbsp; &nbsp; &nbsp; TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then <br>&nbsp; &nbsp; begin <br>&nbsp; &nbsp; &nbsp; try <br>&nbsp; &nbsp; &nbsp; &nbsp; if not LookupPrivilegeValue(nil, SE_SYSTEMTIME_NAME, luid) then Exit; <br>&nbsp; &nbsp; &nbsp; &nbsp; tkp.PrivilegeCount := 1; <br>&nbsp; &nbsp; &nbsp; &nbsp; tkp.Privileges[0].luid := luid; <br>&nbsp; &nbsp; &nbsp; &nbsp; tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED; <br>&nbsp; &nbsp; &nbsp; &nbsp; if not AdjustTokenPrivileges(hToken, False, tkp, SizeOf(TTOKENPRIVILEGES), <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PrevTokenPriv, ReturnLength) then <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit; <br>&nbsp; &nbsp; &nbsp; &nbsp; if (GetLastError &lt;&gt; ERROR_SUCCESS) then <br>&nbsp; &nbsp; &nbsp; &nbsp; begin <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise Exception.Create(SysErrorMessage(GetLastError)); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit; <br>&nbsp; &nbsp; &nbsp; &nbsp; end; <br>&nbsp; &nbsp; &nbsp; finally <br>&nbsp; &nbsp; &nbsp; &nbsp; CloseHandle(hToken); <br>&nbsp; &nbsp; &nbsp; end; <br>&nbsp; &nbsp; end; <br>&nbsp; end; <br>&nbsp; DateTimeToSystemTime(dDateTime, dSysTime); <br>&nbsp; Result := Windows.SetLocalTime(dSysTime); <br>end; <br><br>{************************************************************} <br><br>{2.} <br><br>procedure TForm1.Button1Click(Sender: TObject); <br>var <br>&nbsp; SystemTime: TSystemTime; <br>&nbsp; NewTime, NewDate: string; <br>begin <br>&nbsp; NewTime := '13:58:00'; <br>&nbsp; NewDate := '02.02.2001'; // or '02/02/01' <br>&nbsp; DateTimeToSystemTime(StrToDate(NewDate) + StrToTime(NewTime), SystemTime); <br>&nbsp; SetLocalTime(SystemTime); <br>&nbsp; // Tell windows, that the Time changed! <br>&nbsp; PostMessage(HWND_BROADCAST, WM_TIMECHANGE, 0, 0); // * <br>end; <br><br>{ <br>&nbsp;Windows 2000 and later: An application should not broadcast <br>&nbsp;the WM_TIMECHANGE message because the system will broadcast <br>&nbsp;this message when the application changes the system time. <br>} <br><br>{************************************************************} <br><br>{3.} <br><br>function SetSystemTime(DateTime: TDateTime): Boolean; <br>{ (c) by UNDO } <br>var <br>&nbsp; tSetDati: TDateTime; <br>&nbsp; vDatiBias: Variant; <br>&nbsp; tTZI: TTimeZoneInformation; <br>&nbsp; tST: TSystemTime; <br>begin <br>&nbsp; GetTimeZoneInformation(tTZI); <br>&nbsp; vDatiBias := tTZI.Bias / 1440; <br>&nbsp; tSetDati := DateTime + vDatiBias; <br>&nbsp; with tST do <br>&nbsp; begin <br>&nbsp; &nbsp; wYear := StrToInt(FormatDateTime('yyyy', tSetDati)); <br>&nbsp; &nbsp; wMonth := StrToInt(FormatDateTime('mm', tSetDati)); <br>&nbsp; &nbsp; wDay := StrToInt(FormatDateTime('dd', tSetDati)); <br>&nbsp; &nbsp; wHour := StrToInt(FormatDateTime('hh', tSetDati)); <br>&nbsp; &nbsp; wMinute := StrToInt(FormatDateTime('nn', tSetDati)); <br>&nbsp; &nbsp; wSecond := StrToInt(FormatDateTime('ss', tSetDati)); <br>&nbsp; &nbsp; wMilliseconds := 0; <br>&nbsp; end; <br>&nbsp; Result := Windows.SetSystemTime(tST); <br>end; <br><br>这些资料,你看看吧<br>
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部