How to Turn OFF the Display using C# -WINDOWS FORM

Turning OFF the Display using C#

first of all , you need to use that library :
Code:
using System.Runtime.InteropServices; // for dll import

Then add these under public class :
Code:
//Using the system pre-defined MSDN constants that can b used by the SendMessage()
       
        public int WM_SYSCOMMAND = 0x0112;
        public int SC_MONITORPOWER = 0xF170;

        //To call a DLL function from C#, you must provide this declaration .
        [DllImport("user32.dll")]
    private static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);

Finally when button clicked , add this function :
Code:
            // this will turn off display
            SendMessage(this.Handle.ToInt32(), WM_SYSCOMMAND, SC_MONITORPOWER, 2);


No comments:

Post a Comment