C # implement automatic screen lock + off screen

Source: Internet
Author: User

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/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.