C# – Turn Off Computer Screen

email me

Tested in Visual Studio Community 2017.

Create new project > Select Windows Form App (.NET Framework) > Drag button to form > Add code to Form1.cs > Compile & Run

using System.Runtime.InteropServices;

public int WM_SYSCOMMAND = 0x0112;
public int SC_MONITORPOWER = 0xF170;

[DllImport("user32.dll")]
private static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);

private void button1_Click(object sender, System.EventArgs e)
{

SendMessage( this.Handle.ToInt32(), WM_SYSCOMMAND, SC_MONITORPOWER, 2 );
}


Output

 


Notes

On -1

Off 2

Standby 1

 

tags: form, button, MrNetTek