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.
As follows:CodeFragment:
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, in the exitProgramUse environment. Exit (0) instead of 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.