看看这个吧,人家写的一个自动定时的小程序,很精典的<br>program AutoShutDown;<br><br>uses<br> Windows,<br> ShellAPI,<br> Sysutils,<br> Messages;<br><br>{$R *.RES}<br>var<br> WinClass:TWndClassA;<br> Handle:hwnd;<br> Inst,Button1, Label1, Edit1: Integer;<br> Msg: TMsg;<br> tid: TNotifyIconDataA;<br> sdt:tdatetime;<br> timerid:integer;<br> hFont: Integer;<br>const<br> AppName='AutoShutDown';<br> st='定时关机';<br> About=st+' 1.0, Keyes.chen@263.net, Keyes 1999.10.23';<br> gjs='设定关机时间';<br>procedure SetShutdownTime;<br>var<br> Textlength: Integer;<br> Text: PChar;i:tdatetime;<br>begin<br> TextLength := GetWindowTextLength(Edit1);<br> GetMem(Text, TextLength + 1);<br> GetWindowText(Edit1, Text, TextLength + 1);<br> try<br> i:=Strtodatetime(Text);<br> if i<=now then<br> begin<br> MessageBox(handle,'不对吧!太早了.','错误',mb_ok or MB_ICONERROR);<br> exit;<br> end;<br> sdt:=i;<br> timerid:=Settimer(handle,1000,1000,nil);<br> Showwindow(handle,sw_hide);<br> lstrcpy (tid.szTip,pchar(st+' 关机时间:'+Datetimetostr(sdt)));<br> Shell_NotifyIcon (nim_modify, @tid);<br> except<br> Killtimer(handle,timerid);<br> Messagebox(handle,pchar('关机时间设定错误'#13#10#13#10+'格式因该是:'+Datetimetostr(now)),AppName,Mb_ok or MB_ICONINFORMATION);<br> end;<br> FreeMem(Text, TextLength + 1);<br>end;<br>function WindowProc(hWnd, uMsg, wParam, lParam: Integer): Integer; stdcall;<br>var pt:tpoint;pm:Hmenu;<br>begin<br> result:=0;<br> Case uMsg of<br> wm_timer:<br> begin<br> if now>=sdt then<br> begin<br> Killtimer(handle,timerid);<br> ExitWindowsEx(EWX_SHUTDOWN or EWX_Force, 0);<br>// PostMessage(handle,wm_Destroy,0,0);<br> end;<br> end;<br> wm_User:<br> begin<br> Case lparam of<br> WM_LBUTTONDBLCLK:<br> begin<br> showwindow(handle,sw_restore);<br> setforegroundwindow(handle);<br> end;<br> wm_LButtonDown,wm_RButtonDown:<br> begin<br> GetCursorPos (pt);<br> pm := CreatePopupMenu;<br> AppendMenu (pm,0,Ord ('S'),gjs);<br> AppendMenu (pm, 0, Ord ('A'), '关于...');<br> AppendMenu (pm, mf_Separator, 0, Nil);<br> AppendMenu (pm, 0, Ord ('E'), '退出');<br> SetForegroundWindow (handle);<br> if TrackPopupMenu (pm, tpm_BottomAlign or tpm_RightAlign, pt.x,{GetDeviceCaps(dc,Vertres)}pt.y, 0, handle, Nil) then<br> SetForegroundWindow (handle);<br> DestroyMenu (pm)<br> end;<br> end;<br> end;<br> wm_Destroy:<br> begin<br> Shell_NotifyIcon (nim_Delete, @tid);<br> Killtimer(handle,timerid);<br> halt;<br> end;<br> wm_Command:<br> begin<br> if (lParam = Button1) then begin SetshutdownTime;exit end;<br> Case Loword(wParam) of<br> Ord ('A'): MessageBox (0, About, AppName, mb_ok or MB_ICONINFORMATION);<br> Ord ('E'): PostMessage (handle, wm_Close, 0, 0);<br> Ord ('S'):<br> begin<br> Showwindow(handle,sw_restore);<br> end;<br> end;<br> end;<br> end;<br> Result := DefWindowProc(hWnd, uMsg, wParam, lParam);<br>end;<br><br>begin<br> { ** Register Custom WndClass ** }<br> if FindWindow (AppName, Nil) <> 0 then begin<br> Messagebox(handle,'已经有一个AutoShutDown运行了',AppName,mb_ok or MB_ICONWARNING);<br> halt(0);<br> end;<br> Inst := hInstance;<br> with WinClass do<br> begin<br> style := CS_CLASSDC or CS_PARENTDC;<br> lpfnWndProc := @WindowProc;<br> hInstance := Inst;<br> hbrBackground := color_btnface + 1;<br> lpszClassname := AppName;<br> hCursor := LoadCursor(0, IDC_ARROW);<br> end;<br> RegisterClass(WinClass);<br> Handle := CreateWindowEx(WS_EX_WINDOWEDGE, AppName, 'AutoShutDown 1.00',<br> WS_VISIBLE {or WS_SIZEBOX} or WS_CAPTION or WS_SYSMENU,<br> 283, 238, 325, 65, 0, 0, Inst, nil);<br> Button1 := CreateWindow('Button', 'OK', WS_VISIBLE or WS_CHILD or BS_PUSHLIKE or BS_TEXT,<br> 236, 8, 75, 20, handle, 0, Inst, nil);<br> Label1 := Createwindow('Static', '', WS_VISIBLE or WS_CHILD or SS_LEFT,<br> 8, 12, 80, 13, Handle, 0, Inst, nil);<br> Edit1 := CreateWindowEx(WS_EX_CLIENTEDGE, 'Edit', Pchar(Datetimetostr(now)), WS_CHILD or WS_VISIBLE or<br> WS_BORDER {or ES_PASSWORD}, 88, 8, 141, 21, Handle, 0, Inst, nil);<br> hFont := CreateFont(-12, 0, 0, 0, 500, 0, 0, 0, GB2312_CHARSET,<br> OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,<br> DEFAULT_PITCH or FF_DONTCARE, '宋体');<br> if hFont <> 0 then<br> begin<br> SendMessage(Button1, WM_SETFONT, hFont, 0);<br> SendMessage(Label1, WM_SETFONT, hFont, 0);<br> SendMessage(Edit1, WM_SETFONT, hFont, 0);<br> end;<br> SetWindowText(Label1, pchar(gjs+':'));<br> SetFocus(Edit1);<br> UpdateWindow(Handle);<br> tid.cbSize := sizeof (tid);<br> tid.Wnd := handle;<br> tid.uID := 1;<br> tid.uFlags := nif_Message or nif_Icon or nif_Tip;<br> tid.uCallBackMessage := wm_User;<br> tid.hIcon := LoadIcon (hInstance, 'MAINICON');<br> lstrcpy (tid.szTip,st);<br> Shell_NotifyIcon (nim_Add, @tid);<br> while(GetMessage(Msg, Handle, 0, 0)) do<br> begin<br> TranslateMessage(msg);<br> DispatchMessage(msg);<br> end;<br>end.