Recently found the windows Lock screen API: LockWorkStation
As a result, I integrated the previous screen-off API and made some things that I could choose to automatically lock the screen and turn off the screen.
The following code snippet:
Public Form1 (bool aLock ){
If (aLock ){
// Screen lock + screen off
LockWorkStation ();
SendMessage (this. Handle, (uint) 0x0112, (IntPtr) 0xF170, (IntPtr) 2 );
}
Else {
// Disable mouse and keyboard actions + screen off
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 );
}
// Close the screen
[DllImport ("user32.dll", CharSet = CharSet. Auto)]
Static extern IntPtr SendMessage (IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam );
// Disable mouse and keyboard actions
[Return: financialas (UnmanagedType. Bool)]
[DllImport ("user32.dll", CharSet = CharSet. Auto, ExactSpelling = true)]
Public static extern bool BlockInput ([In, financialas (UnmanagedType. Bool)] bool fBlockIt );
// Screen lock
[DllImport ("user32.dll")]
Public static extern bool LockWorkStation ();
It should be noted that Environment. Exit (0) is used when exiting the program, rather than Application. Exit (); otherwise, an error will occur ~~ The prompt is similar to: "*** an error occurs and you need to disable it ".
Also, modify Main:
Static void Main (string [] args ){
// Application. EnableVisualStyles ();
// Application. SetCompatibleTextRenderingDefault (false );
If (args = null | args. Length = 0 ){
// Disable mouse and keyboard actions + screen off
Application. Run (new Form1 (false ));
}
Else {
// Screen lock + screen off
Application. Run (new Form1 (true ));
}
}
In this way, the success is achieved...
The reason why you want to disable the mouse and keyboard is to turn off the screen successfully ...~~~ Nonsense...
Create a shortcut and add a parameter to lock the screen. Click to download source code
Source: http://1971ruru.cnblogs.com/