|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionIt's no more news that handling the monitor can be done using My goal here is to show a complete manual to handle this function for the above mentioned task, as I have tested different situations from different authors and once again I have discovered the lack of proper documentation for an actually simple task. API DeclarationOne first issue is declaring the [DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg,
IntPtr wParam, IntPtr lParam);
Where:
Handling the Handle ArgumentConsidering the above, here are your options: The best way is to use the active handler from the current Window Form, if you are developing a Windows-based application. But what happens when you are developing a *.dll Library or a Console Application, where you don’t have any currently used Windows Form????? Where you can't use the keyword Go back to the root of the problem, and remember that you need a valid HANDLER to accomplish the task. What options does one have??
Accessing the MonitorSo, we need to access the monitor. Considering the above, we will need to provide the const int SC_MONITORPOWER = 0xF170;
const int WM_SYSCOMMAND = 0x0112;
Monitor State SwitchSo far, we have discussed about how to create a valid call of the function, and how to tell the Interop engine the target we aim at. To complete the task, we still need to specify the verb symbolizing the action to be taken on the target. As you might guess, we will need a switch option: turn the monitor on/off. Good guess, with the correction that we actually have three states: on / off / stand-by: const int MONITOR_ON = -1;
const int MONITOR_OFF = 2;
const int MONITOR_STANBY = 1;
Final Call StructureThe final generic call will look like this: SendMessage(ValidHWND, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_STATE);
More specifically, turn the monitor ON, using: SendMessage(ValidHWND, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_ON);
Turn the monitor OFF, using: SendMessage(ValidHWND, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF);
Put the monitor to STAND BY, using: SendMessage(ValidHWND, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_STANDBY);
Similar API FunctionsIt shouldn't surprise you that turning the monitor on/off can be done using several other API function calls specially when you know that sending an interop message is up to certain engine needs. Here are the rest of the functions (with API declarations) that can have a similar effect: [DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(IntPtr hWnd, uint Msg,
IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
static extern bool PostThreadMessage(uint idThread, uint Msg,
UIntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern bool SendMessageCallback(IntPtr hWnd, uint Msg,
UIntPtr wParam, IntPtr lParam,
SendMessageDelegate lpCallBack, UIntPtr dwData);
[DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern bool SendNotifyMessage(IntPtr hWnd, uint Msg,
UIntPtr wParam, IntPtr lParam);
So, knowing this now, you should be able to choose between
History
|
||||||||||||||||||||||