Turn On/Off monitor using Windows API

標題 : 用Windows API 開/關螢幕

Windows API I am refering to the famous win32 API. I have once brought a few books about win32. I didn’t finish reading them since after I finish the first chapter, I recognize that win32 is a horrible beast. And also .NET is published.
I recall that the introduction said there are three main .dll in win32 api. We are going to use one of them, the user32.dll.

Windows API我指的是著名的win32 API.我曾經買了幾本關於win32的書.但我沒有把它們看完因為讀完第一章後,我發覺win32真是一隻可怕的怪獸.而且,那時候.NET剛出來.
我記得書上的介紹說win32 api主要是三個.dll.我們將會用到其中一個, user32.dll

The first step is, how do we use the ability in user32.dll? We have to import it first. It is like linking .dll in C++. Specify which .dll you are linking and then specify the function prototype which will map a function in the .dll to a method you actually can invoke in C#.


public class SomeClass
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg,
              IntPtr wParam, IntPtr lParam);

The SendMessage() function send a message to windows system and tells the system to do something.
The function needs 4 parameters. hWnd is the handle to the window which will receive the message.
Msg is the message to be sent. wParam and lParam are both parameters for the message. One can see wParam as parameter to the message and lParam is parameter to wParam.

There are many options to get the hWnd.
1. For windows form application, you can use this.Handle.
1. 如果是視窗程式,可以用this.Handle取得.
2. Use system pre-defined handle,
2. 使用系統定義好的處理常式,

int HWND_BROADCAST = 0xffff;
int HWND_TOPMOST    = -1;
int HWND_TOP        = 0;
int HWND_BOTTOM     = 1;

3. Use the API to grap the desktop handle.
3. 使用API去抓桌面的處理常式

private static extern IntPtr GetDesktopWindow();

4. Create a new one
4. 創建一個新的

Form frm = new Form();

5. Use the API to grap an available one
5. 用API抓一個能用的

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);


[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent,
IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

If you use null as class and or title, you will get the topmost one.
And then comes the pre-defined message values.

//for Msg and wParam
const int SC_MONITORPOWER = 0xF170;
const int WM_SYSCOMMAND = 0x0112;
//for lParam
const int MONITOR_ON = -1;
const int MONITOR_OFF = 2;
const int MONITOR_STANBY = 1;

Put them all together like this.


You can not turn your monitor ON/OFF. But that is another story if you want to know the current monitor state.


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s