关机这个:使用时PowerOff(True)强制关闭Windows procedure TAPIMisc.PowerOff(Force:boolean=false); var hToken : THandle; tkp : TTokenPrivileges; tkpo : TTokenPrivileges; zero : DWORD; begin if Win32Platform=VER_PLATFORM_WIN32_NT then // we've got to do a whole buch of things begin zero := 0; if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then begin MessageBox( 0, 'PowerOff Error', 'OpenProcessToken() Failed', MB_OK ); Exit; end; // if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then begin MessageBox( 0, 'PowerOff Error', 'OpenProcessToken() Failed', MB_OK ); Exit; end; // if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) // SE_SHUTDOWN_NAME if not LookupPrivilegeValue( nil, 'SeShutdownPrivilege' , tkp.Privileges[ 0 ].Luid ) then begin MessageBox( 0, 'PowerOff Error', 'LookupPrivilegeValue() Failed', MB_OK ); Exit; end; // if not LookupPrivilegeValue( nil, 'SeShutdownPrivilege' , tkp.Privileges[0].Luid ) 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, 'PowerOff Error', 'AdjustTokenPrivileges() Failed', MB_OK ); Exit; end // if Boolean( GetLastError() ) else if Force then ExitWindowsEx( EWX_Force or EWX_POWEROFF, 0 ) else ExitWindowsEx( EWX_POWEROFF, 0 ); end // if OSVersion = 'Windows NT' else // just shut the machine down if Force then ExitWindowsEx( EWX_Force or EWX_POWEROFF, 0 ) else ExitWindowsEx( EWX_POWEROFF, 0 ); end;
能否不锁屏,而锁住键盘和鼠标,即禁止输入。同时跳出对话框。我想这个能实现吧。 锁键盘和鼠标: // Import BlockInput function form user32.dll: // BlockInput Funktion von user32.dll importieren: function BlockInput (fBlockInput : boolean) : DWord; stdcall; external 'user32.DLL'; {block input/ blockieren} procedure TForm1.Button1Click(Sender: TObject); begin BlockInput(true); end; {unblock input / Blockierung aufheben} procedure TForm1.Button2Click(Sender: TObject); begin BlockInput(false); end;
[Shell]
Command=2
IconFile=explorer.exe,3[Taskbar]
Command=ToggleDesktop这样可以返回到桌面。然后封锁热键,将你的程序设为最前面并且最大化并且BorderStyle=bsNone,这样就实现了锁屏。
关机则使用ExitWindowsEx这个API
锁定热键
SystemParametersInfo(SPI_SCREENSAVERRUNNING,FALSE,nil,0);
取消锁定。根据测试,在XP/2k/nt下面不能用。
锁定热键的方法我倒是用了。
但是,如果当前有大型游戏正在运行,就会出现问题。
蓝屏啦、错误信息、死机这些情况都有。
我的系统的WinMe.关机时也差不多有这种情况。
Process32First
Process32Next
procedure TAPIMisc.PowerOff(Force:boolean=false);
var
hToken : THandle;
tkp : TTokenPrivileges;
tkpo : TTokenPrivileges;
zero : DWORD;
begin
if Win32Platform=VER_PLATFORM_WIN32_NT then // we've got to do a whole buch of things
begin
zero := 0;
if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then
begin
MessageBox( 0, 'PowerOff Error', 'OpenProcessToken() Failed', MB_OK );
Exit;
end; // if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)
if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then
begin
MessageBox( 0, 'PowerOff Error', 'OpenProcessToken() Failed', MB_OK );
Exit;
end; // if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)
// SE_SHUTDOWN_NAME
if not LookupPrivilegeValue( nil, 'SeShutdownPrivilege' , tkp.Privileges[ 0 ].Luid ) then
begin
MessageBox( 0, 'PowerOff Error', 'LookupPrivilegeValue() Failed', MB_OK );
Exit;
end; // if not LookupPrivilegeValue( nil, 'SeShutdownPrivilege' , tkp.Privileges[0].Luid )
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, 'PowerOff Error', 'AdjustTokenPrivileges() Failed', MB_OK );
Exit;
end // if Boolean( GetLastError() )
else if Force then ExitWindowsEx( EWX_Force or EWX_POWEROFF, 0 )
else ExitWindowsEx( EWX_POWEROFF, 0 );
end // if OSVersion = 'Windows NT'
else // just shut the machine down
if Force then ExitWindowsEx( EWX_Force or EWX_POWEROFF, 0 )
else ExitWindowsEx( EWX_POWEROFF, 0 );
end;
锁键盘和鼠标:
// Import BlockInput function form user32.dll: // BlockInput Funktion von user32.dll importieren: function BlockInput (fBlockInput : boolean) : DWord; stdcall; external 'user32.DLL'; {block input/ blockieren} procedure TForm1.Button1Click(Sender: TObject);
begin
BlockInput(true);
end; {unblock input / Blockierung aufheben} procedure TForm1.Button2Click(Sender: TObject);
begin
BlockInput(false);
end;