楼主: |
关键部分我看不懂 如何 关机的帮我注释1下谢谢 还有在托盘显示::代码如下 unit fmain;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls, ExtCtrls, Buttons, Spin,ShellAPI, Menus;
type TFclosetime = class(TForm) GroupBox1: TGroupBox; GroupBox2: TGroupBox; Rb1: TRadioButton; Rb2: TRadioButton; Rb3: TRadioButton; Rb4: TRadioButton; Rb5: TRadioButton; Label1: TLabel; cb1: TComboBox; cb2: TComboBox; Label2: TLabel; Label3: TLabel; Timer1: TTimer; Label4: TLabel; cb3: TComboBox; SpeedButton1: TSpeedButton; SpeedButton2: TSpeedButton; SpeedButton3: TSpeedButton; DateTimePicker1: TDateTimePicker; SpinEdit1: TSpinEdit; Timer2: TTimer; StatusBar1: TStatusBar; PopupMenu1: TPopupMenu; N1: TMenuItem; N2: TMenuItem; N3: TMenuItem; procedure SpeedButton2Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure SpeedButton1Click(Sender: TObject); procedure Rb4Click(Sender: TObject); procedure Rb5Click(Sender: TObject); procedure Timer1Timer(Sender: TObject); procedure Timer2Timer(Sender: TObject); procedure FormShow(Sender: TObject); procedure SpeedButton3Click(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure N1Click(Sender: TObject); procedure N2Click(Sender: TObject); procedure N3Click(Sender: TObject); private { Private declarations } procedure WndProc(var Msg:TMessage);override; procedure Myminisize(Sender:TObject); public gjleixing:string; IconData:TNotifyIconData; { Public declarations } end;
var Fclosetime: TFclosetime; daojishi:int64; riqi:string; shi,fen,miao:integer;
implementation uses fclose,about; {$R *.dfm}
procedure TFclosetime.WndProc(var Msg:TMessage); var p:TPoint; begin case msg.Msg of WM_USER+1: case msg.LParam of WM_LBUTTONDBLCLK: begin fmain.Fclosetime.Show; showwindow(application.Handle,SW_SHOWNORMAL); end; WM_RBUTTONDOWN: begin getcursorPos(p); popupmenu1.Popup(p.X, p.Y); end; end; end; inherited; end;
procedure Tfclosetime.Myminisize(sender:TObject); begin fmain.Fclosetime.Hide; showwindow(application.Handle,SW_Hide); end;
procedure TFclosetime.SpeedButton2Click(Sender: TObject); begin timer1.Enabled:=false; timer1.Enabled:=false; shell_notifyIcon(NIM_DELETE,@IconData); APPlication.ProcessMessages; Application.Terminate; end;
procedure TFclosetime.FormCreate(Sender: TObject); var hour,min,sec,msec:word; begin decodetime(now,hour,min,sec,msec); cb1.Text:=inttostr(hour); cb2.Text:=inttostr(min); cb3.Text:=inttostr(sec); //处理托盘 IconData.cbSize:=sizeof(Icondata); IconData.Wnd:=Handle; IconData.uID:=100; IconData.uFlags:=NIF_MESSAGE+NIF_ICON+NIF_TIP; IconData.uCallbackMessage:=WM_USER+1; IconData.hIcon:=Application.Icon.Handle; strpcopy(IconData.szTip,application.Title); Shell_notifyIcon(NIM_ADD,@IconData); application.OnMinimize:=Myminisize; //self.Height:=233; //self.Width:=395; end;
procedure TFclosetime.SpeedButton1Click(Sender: TObject); begin if rb1.Checked then gjleixing:='gj1'; if rb2.Checked then gjleixing:='gj2'; if rb3.Checked then gjleixing:='gj3'; if rb4.Checked then begin riqi:=datetostr(datetimepicker1.Date); shi:=strtoint(cb1.Text); fen:=strtoint(cb2.Text); miao:=strtoint(cb3.Text); timer1.Enabled:=true; statusbar1.SimpleText:='定时任务执行中...'; //fclose.Form1.Show; end;
if rb5.Checked then begin daojishi:=spinedit1.Value*60; timer2.Enabled:=true; statusbar1.SimpleText:='定时任务执行中...'; // fclose.Form1.Show; end; end;
procedure TFclosetime.Rb4Click(Sender: TObject); begin datetimepicker1.Enabled:=true; cb1.Enabled:=true; cb2.Enabled:=true; cb3.Enabled:=true; spinedit1.Enabled:=false; end;
procedure TFclosetime.Rb5Click(Sender: TObject); begin datetimepicker1.Enabled:=false; cb1.Enabled:=false; cb2.Enabled:=false; cb3.Enabled:=false; spinedit1.Enabled:=true; end;
procedure TFclosetime.Timer1Timer(Sender: TObject); var hour,min,sec,msec:word; begin decodetime(now,hour,min,sec,msec); if (riqi=datetostr(date)) and (hour=shi) and (min=fen) and (sec=miao) then begin // showmessage('马上关机'); timer1.Enabled:=false; fclose.Form1.Show; end; end;
procedure TFclosetime.Timer2Timer(Sender: TObject); begin daojishi:=daojishi-1; if daojishi<0 then begin //showmessage('开始关机'); timer2.Enabled:=false; fclose.Form1.Show; end; end;
procedure TFclosetime.FormShow(Sender: TObject); begin DateTimePicker1.Date:=date; end;
procedure TFclosetime.SpeedButton3Click(Sender: TObject); begin about.AboutBox.Show; end;
procedure TFclosetime.FormClose(Sender: TObject; var Action: TCloseAction); begin shell_notifyIcon(NIM_DELETE,@IconData); APPlication.ProcessMessages; application.Terminate; end;
procedure TFclosetime.N1Click(Sender: TObject); begin fmain.Fclosetime.Show; showwindow(application.Handle,SW_SHOWNORMAL); end;
procedure TFclosetime.N2Click(Sender: TObject); begin self.SpeedButton3.Click; end;
procedure TFclosetime.N3Click(Sender: TObject); begin self.SpeedButton2.Click; end;
end. 出处 本论坛的代码发布的兔子版 自动关机 ---------------------------------------------- - |
作者: |
|
2006-3-7 7:29:42 |
1楼: |
program Project1;
uses windows,messages,mmsystem,sysutils;
var WinClass: TWndClassA; Inst, Handle, Button1, Label1, Edit1,Edit2,button2: Integer; Msg: TMsg; hFont: Integer; h,m,s,ms:Word; ID_TIMER:Cardinal; ID1,id2:integer;
procedure TimedShutDown(Reboot:boolean); const SE_SHUTDOWN_NAME = 'SeShutdownPrivilege'; var hToken : THandle; tkp : TTokenPrivileges; tkpo : TTokenPrivileges; zero : DWORD; begin zero := 0; if not OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then begin MessageBox( 0, 'Exit Error', 'OpenProcessToken() Failed', MB_OK ); Exit; end; if not LookupPrivilegeValue( nil, 'SeShutdownPrivilege',tkp.Privileges[ 0 ].Luid ) then begin MessageBox( 0, 'Exit Error', 'LookupPrivilegeValue() Failed', MB_OK ); Exit; end; tkp.PrivilegeCount := 1; tkp.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED; AdjustTokenPrivileges( hToken, False, tkp, SizeOf( TTokenPrivileges ), tkpo, zero ); if Boolean( GetLastError()) then begin MessageBox( 0, 'Exit Error', 'AdjustTokenPrivileges() Failed', MB_OK ); Exit; end else if Reboot=true then begin PlaySound ('clowindow.wav', 0, SND_FILENAME or SND_ASYNC) ; sleep(5000); ExitWindowsEx( EWX_SHUTDOWN or EWX_POWEROFF or EWX_FORCE,0) ; end;
end; function geth:boolean; var Textlength: Integer; Text: PChar; begin GetMem(Text, 254); GetWindowText(Edit1, Text, TextLength + 1); if (Text = inttostr(h)) then Result:=true; end; function getm:boolean; var Textlength2: Integer; Text2: PChar; begin GetMem(Text2, 254); GetWindowText(Edit2, Text2, TextLength2 + 1); if (text2=inttostr(m)) then Result:=true; end;
function WindowProc(hWnd, uMsg, wParam, lParam: Integer): Integer; stdcall;
begin Result := DefWindowProc(hWnd, uMsg, wParam, lParam); { Checks for messages } case umsg of WM_CREATE: SetTimer(hwnd, ID_TIMER, 1000, nil); WM_HOTKEY : begin if wParam=ID1 then ShowWindow(handle,sw_show); end; WM_COMMAND: begin case LOWORD(wParam) of 1: BEGIN EnableWindow(edit1,False); EnableWindow(edit2,False); ShowWindow(handle,sw_hide); END; 2: begin EnableWindow(edit1,true); EnableWindow(edit2,true); end; end; end; WM_TIMER: begin DecodeTime(now,h,m,s,ms); if (geth) and (getm) then TimedShutDown(true); end; WM_DESTROY :begin UnRegisterHotKey(handle,ID1); UnRegisterHotKey(handle,ID2); KillTimer(hwnd,id_timer); PostQuitMessage(0); end; // end; end; begin Inst := hInstance; with WinClass do begin style := CS_CLASSDC or CS_PARENTDC ; lpfnWndProc := @WindowProc; hInstance := Inst; hbrBackground := color_btnface + 1; lpszClassname := 'AG_TESTWINDOW'; hCursor := LoadCursor(0, IDC_ARROW); end; { with } RegisterClass(WinClass); Handle := CreateWindowEx( WS_EX_OVERLAPPEDWINDOW, 'AG_TESTWINDOW', 'playclose', WS_VISIBLE or WS_SIZEBOX or WS_CAPTION or WS_SYSMENU , 363, 278, 305, 65, 0, 0, Inst, nil); { ** Create a button ** } Button1 := CreateWindow('Button', 'set', WS_VISIBLE or WS_CHILD or BS_PUSHLIKE or BS_TEXT, 190, 8, 50, 25, handle, 1, Inst, nil); Button2 := CreateWindow('Button', 'reset', WS_VISIBLE or WS_CHILD or BS_PUSHLIKE or BS_TEXT, 241, 8, 50, 25, handle, 2, Inst, nil); { ** Create a label (static) ** } Label1 := Createwindow('Static', '设定时间', WS_VISIBLE or WS_CHILD or SS_LEFT, 8, 12, 76, 13, Handle, 0, Inst, nil);
{ ** Create an edit field ** } Edit1 := CreateWindowEx(WS_EX_CLIENTEDGE , 'Edit', '19', WS_CHILD or WS_VISIBLE or WS_BORDER , 88, 8, 50, 21, Handle, 0, Inst, nil); Edit2 := CreateWindowEx(WS_EX_CLIENTEDGE, 'Edit', '29', WS_CHILD or WS_VISIBLE or WS_BORDER , 139, 8, 50, 21, Handle, 0, Inst, nil);
{ ** Create Font Handle ** } hFont := CreateFont(-11, 0, 0, 0, 400, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH or FF_DONTCARE, 'MS Sans Serif'); { Change fonts } if hFont <> 0 then begin SendMessage(Button1, WM_SETFONT, hFont, 0); SendMessage(Label1, WM_SETFONT, hFont, 0); SendMessage(Edit1, WM_SETFONT, hFont, 0); SendMessage(Edit2, WM_SETFONT, hFont, 0); SendMessage(Button2, WM_SETFONT, hFont, 0); end; ShowWindow(inst,sw_show); UpdateWindow(Handle); RegisterHotKey(Handle, ID1, MOD_CONTROL, VK_F10); //Ctrl+F1隐藏窗口 while(GetMessage(Msg, Handle, 0, 0)) do begin TranslateMessage(msg); DispatchMessage(msg); end; { with } end. ---------------------------------------------- - |
|
|