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

Anyway.
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#.

不管怎樣.
第一步是,我們怎樣能利用到user32.dll的能力呢?我們要先匯入它.就好像C++匯入.dll差不多.指明你要用的.dll還有說明你要用的函式原型,這原型會把你的方法對應到.dll裡的函式.然後我們就可以在C#裡調用.

public class SomeClass
{
[DllImport("user32.dll")]
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.
SendMessage()這個函式會發送一個信息給系統,告訴它處理一些事情.
這個函式需要4個參數.hWnd是接收信息視窗的處理常式.Msg是要傳遞的信息.wParam和lParam都是信息的參數.你可以把wParam視為信息參數而lParam視為wParam的參數.

There are many options to get the hWnd.
要取得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;
int HWND_NOTOPMOST  = -2;

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

[DllImport("user32.dll")]
private static extern IntPtr GetDesktopWindow();

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

Form frm = new Form();
Frm.Handle();

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

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

Or

[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.
如果你用null作為類別名或/和標題,你會取得最上層的.
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.

SendMessage(ValidHWND, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_STATE);

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s