1 回答

TA貢獻1827條經驗 獲得超8個贊
1.要實現鎖定系統不讓別人用,可以調用系統鎖定API函數來實現
//引入API函數
[DllImport("user32 ")]
public static extern bool LockWorkStation();//這個是調用windows的系統鎖定
在需要的時候直接寫LockWorkStation();就可以啦!不信試試看!
2.API函數鎖定鍵盤及鼠標
[DllImport("user32.dll")]
static extern void BlockInput(bool Block);
需要的時候就直接寫:
BlockInput(true);//鎖定鼠標及鍵盤
BlockInput(false);//解除鍵盤鼠標鎖定
但是這種方式還是不能鎖定ctrl+alt+delete,也就是還可以打開任務管理器,怎么辦呢?
請看下面的方法:
3.屏蔽ctrl+alt+delete
FileStream fs = new FileStream(Environment.ExpandEnvironmentVariables("%windir%\\system32\\taskmgr.exe"), FileMode.Open);
//byte[] Mybyte = new byte[(int)MyFs.Length];
//MyFs.Write(Mybyte, 0, (int)MyFs.Length);
//MyFs.Close(); //用文件流打開任務管理器應用程序而不關閉文件流就會阻止打開任務管理器
利用windows鎖屏API:LockWorkStation。
如下代碼片段:
public Form1( bool aLock ) { if (aLock) { //鎖屏+關屏LockWorkStation();SendMessage( this.Handle, (uint)0x0112, (IntPtr)0xF170, (IntPtr)2 );} else { //禁止鼠標鍵盤動作+關屏BlockInput( true );System.Threading.Thread.Sleep( 10 );SendMessage( this.Handle, (uint)0x0112, (IntPtr)0xF170, (IntPtr)2 );BlockInput( false );} this.Close(); //Application.Exit();Environment.Exit( 0 );} //關屏[DllImport( "user32.dll", CharSet = CharSet.Auto )] static extern IntPtr SendMessage( IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam ); //禁止鼠標鍵盤動作[return: MarshalAs( UnmanagedType.Bool )][DllImport( "user32.dll", CharSet = CharSet.Auto, ExactSpelling = true )] public static extern bool BlockInput( [In, MarshalAs( UnmanagedType.Bool )] bool fBlockIt ); //鎖屏[DllImport( "user32.dll" )] public static extern bool LockWorkStation();需要指出的是,在退出程序時使用Environment.Exit( 0 );而非Application.Exit();否則會出錯哦~~提示類似:“***遇到錯誤,需要關閉”。還有就是修改一下Main:static void Main(string[] args) { //Application.EnableVisualStyles(); //Application.SetCompatibleTextRenderingDefault( false ); if (args == null || args.Length == 0) { //禁止鼠標鍵盤動作+關屏Application.Run( new Form1( false ) );} else { //鎖屏+關屏Application.Run( new Form1( true ) );}}
- 1 回答
- 0 關注
- 212 瀏覽
添加回答
舉報