由于我们需要制作一个通用的关机程序,故要对当前的操作系统进行判断,这个比较简单,函数如下: view plaincopy to clipboardprint? function GetOperatingSystem: string;//获取操作系统信息 var osVerInfo: TOSVersionInfo; begin Result :=''; osVerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo); if GetVersionEx(osVerInfo) then case osVerInfo.dwPlatformId of VER_PLATFORM_WIN32_NT: begin Result := 'Windows NT/2000/XP' end; VER_PLATFORM_WIN32_WINDOWS: begin Result := 'Windows 95/98/98SE/Me'; end; end; end;
执行关机的主函数: procedure ShutDownComputer; begin if GetOperatingSystem='Windows NT/2000/XP' then begin Get_Shutdown_Privilege; //调用此函数会出现系统关机提示窗口,并允许用户取消关机动作 //InitiateSystemShutDown(nil,'关机提示:讨厌你所以关了你!',0,True,False); ExitWindowsEx(EWX_SHUTDOWN+EWX_FORCE+EWX_POWEROFF+EWX_FORCEIFHUNG,0); end else begin ExitWindowsEx(EWX_SHUTDOWN+EWX_FORCE+EWX_POWEROFF+EWX_FORCEIFHUNG,0); end; end; =========================================================================================================================}
使用:
procedure TShutDownForm.btn_PowerOffClick(Sender:Object); begin ShutDownComputer; end;
function GetOperatingSystem: string;//获取操作系统信息 var osVerInfo: TOSVersionInfo; begin Result :=''; osVerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo); if GetVersionEx(osVerInfo) then case osVerInfo.dwPlatformId of VER_PLATFORM_WIN32_NT: begin Result := 'Windows NT/2000/XP' end; VER_PLATFORM_WIN32_WINDOWS: begin Result := 'Windows 95/98/98SE/Me'; end; end; end;
执行关机的主函数: procedure ShutDownComputer; begin if GetOperatingSystem='Windows NT/2000/XP' then begin Get_Shutdown_Privilege; //调用此函数会出现系统关机提示窗口,并允许用户取消关机动作 //InitiateSystemShutDown(nil,'关机提示:讨厌你所以关了你!',0,True,False); ExitWindowsEx(EWX_SHUTDOWN+EWX_FORCE+EWX_POWEROFF+EWX_FORCEIFHUNG,0); end else begin ExitWindowsEx(EWX_SHUTDOWN+EWX_FORCE+EWX_POWEROFF+EWX_FORCEIFHUNG,0); end; end; =====================}
使用:
procedure TShutDownForm.btn_PowerOffClick(Sender:Object); begin ShutDownComputer; end;