|
I rated this as a 5 (to my surprise), because I like simple solutions to problems. I know finding the solution is not always easy, which you did. I hope some one adds this to a FAQs some where.
INTP
Every thing is relative...
|
|
|
|
|
|
I can use SendMessage or PostMessage to turn on or turn off the monitor. The problem is I have multiple VGA cards that link to multiple monitors. I just want to turn off the individual one. Can anybody help me?
|
|
|
|
|
Hi, I think this is not possible using the standard WinAPI.
|
|
|
|
|
Hi,
This is nice exe and working perfectly.But i tried using C# and found its not workking , i think i am missing somewhere plz guid me .
I used the code as folows.
The name space I used is :
using System.Runtime.InteropServices;
Now I declared the varrbale
const int MONITOR_ON = -1;
const int MONITOR_OFF = 2;
const int WM_SYSCOMMAND = 0x0112;
const int SC_MONITORPOWER = 0xF170;
const UInt32 ES_CONTINUOUS = 0x80000000;
//declared methods
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);
[DllImport("user32.dll")]
static extern IntPtr GetDesktopWindow();
// written following code in page load.
System.Threading.Thread.Sleep(2000);
PostMessage(GetDesktopWindow(), WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF);
System.Threading.Thread.Sleep(2000);
Please help me out.
Regards,
Lalit Narayan Dubey
S/W Developer , Noida
|
|
|
|
|
Try this
<br />
using System.Runtime.InteropServices;<br />
[DllImport("user32.dll")]<br />
private static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);<br />
public int WM_SYSCOMMAND = 0x0112;<br />
public int SC_MONITORPOWER = 0xF170;<br />
private System.Windows.Forms.Button button1;<br />
private void button1_Click(object sender, System.EventArgs e)<br />
{<br />
int result = 0;<br />
result = SendMessage(ActiveForm.Handle.ToInt32(), WM_SYSCOMMAND, SC_MONITORPOWER, 2);<br />
}<br />
|
|
|
|
|
Hi,
Its really working great.thanks. I tried one thing more to have the desktop alwayas live. from control pannel setting i set turn off monitor in 1 min.
and written the code like this in button click
private void button1_Click(object sender, System.EventArgs e)
{
int result = 0;
//
// get handle to active form
//
result = SendMessage(ActiveForm.Handle.ToInt32(), WM_SYSCOMMAND, SC_MONITORPOWER, -1);
//
}
but it dont work, the monitor get power down.
What else need to change in application. please help me out here too.
Regards,
Lalit Narayan Dubey
S/W Developer,Noida
|
|
|
|
|
Hmmm, would you like to disable monitor power off function?
Well, use this code but it's not C# code.
#include <windows.h><br />
<br />
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);<br />
<br />
int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)<br />
{<br />
static TCHAR szAppName[] = TEXT("Disabler");<br />
HWND hwnd;<br />
MSG msg;<br />
WNDCLASS wndclass;<br />
<br />
wndclass.style = CS_HREDRAW | CS_VREDRAW;<br />
wndclass.lpfnWndProc = WndProc;<br />
wndclass.cbClsExtra = 0;<br />
wndclass.cbWndExtra = 0;<br />
wndclass.hInstance = hInstance;<br />
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);<br />
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);<br />
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);<br />
wndclass.lpszMenuName = NULL;<br />
wndclass.lpszClassName = szAppName;<br />
<br />
if (!RegisterClass(&wndclass)) <br />
{<br />
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);<br />
return 0;<br />
}<br />
<br />
hwnd = CreateWindow(szAppName, TEXT("Screensaver && Monitor Power Saving are temporary disabled..."),<br />
WS_OVERLAPPEDWINDOW,<br />
150, 150,<br />
500, 250,<br />
NULL, NULL, hInstance, NULL);<br />
<br />
ShowWindow(hwnd, nCmdShow);<br />
UpdateWindow(hwnd);<br />
<br />
while(GetMessage(&msg, NULL, 0, 0)) <br />
{<br />
TranslateMessage(&msg);<br />
DispatchMessage(&msg);<br />
}<br />
<br />
return ((int) msg.wParam);<br />
}<br />
<br />
<br />
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)<br />
{<br />
switch (message)<br />
{<br />
case WM_SYSCOMMAND:<br />
<br />
switch (wParam) <br />
{ <br />
case SC_SCREENSAVE:<br />
case SC_MONITORPOWER:<br />
<br />
return 0;<br />
}<br />
<br />
break;<br />
<br />
case WM_DESTROY:<br />
<br />
PostQuitMessage(0);<br />
<br />
return 0;<br />
}<br />
<br />
return DefWindowProc(hwnd, message, wParam, lParam);<br />
}<br />
|
|
|
|
|
Works great ... in c++ ... how do i get it to work in vb ?
Stoopid SendMessage declare in vb doesnt work
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
any ideas ?
:: For all the hours you save writing user progs in vb, you spend twice as long implenting one tiny windows API call ...
|
|
|
|
|
Oh, see it can be done in VB
Note the commented line that *doesnt* work ...
(Tested on Win2k sp4)
Private Declare Function SendMessageVal Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Sub WakeMonitor()
'SendMessageVal &HFFFF, &H112, &HF170, -1 ' Doesnt work !&@!
SendMessageVal &HFFFF, &H112, 61808, -1
End Sub
|
|
|
|
|
Oops, that works only for the monitor off command - the monitor on command works momentarily and then monitor switches off again, unless it is immediately followed by a MsgBox() command to open a modal message box - which of course stops our app from doing what it's supposed to.
Once again, vb has proved to be a complete piece of ****
What now ?
|
|
|
|
|
At last
Well, the monitor off command may not work in a vb app, but the following seems to be a workaround - it sticks a SHIFT_DOWN and a SHIFT_UP keypress sequence into the keyboard buffer. Ive only tested it on a machine with a keyboard attached so dont know if it works on keyboardless systems ...
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
keybd_event &H1, &H45, 1, 0
keybd_event &H1, &H45, 1 + 2, 0
|
|
|
|
|
In VB &HF170 is an integer and evaluates to -3728
If you append another "&", vb knows it is a long: &HF170& = 61808
(That's why the first code didn't work.)
Try using the following to turn the monitor On:
SendKeys "{PRTSC}" 'simulate a keypress to wake the computer up
an application will never receive that keypress, so it is completely safe
(see sendkeys documentation)
|
|
|
|
|
Public Const SC_MONITORPOWER As Long = &HF170&
Public Const MONITOR_ON = -1
Public Const MONITOR_LOW = 1
Public Const MONITOR_OFF = 2
Public Declare Function SendMessageByNum Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, byval lParam As Long) As Long
Public Sub TurnOnMonitors()
SendMessageBynum GetDesktopWindow, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_ON
End Sub
Public Sub TurnOffMonitors()
SendMessageBynum GetDesktopWindow, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF
End Sub
Public Sub DimMonitors()
SendMessageBynum GetDesktopWindow, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_LOW
End Sub
Note: If you have more than 1 monitor, it will turn on or off all of them. I am seeking for the solution to turn on or turn off individual monitor
|
|
|
|
|
...that you could even do this. Thanks for sharing!
Cheers,
Tom Archer - Archer Consulting Group
"So look up ahead at times to come, despair is not for us. We have a world and more to see, while this remains behind." - James N. Rowe
|
|
|
|
|
I thought everyone knew this!
Just goes to show how a 'simple' article that to one indvidual is obvious and a waste of time, can be of great use to others.
Which is one of the reasons why I like CP!
"An education isn't how much you have committed to memory, or even how much you know. It's being able to differentiate between what you do know and what you don't." - Anatole France
|
|
|
|
|
Definitely. One thing I often struggle with is what to put in a book as I'm always thinking "everybody knows this". I don't honestly know if I'll ever have a need for this - hence my lack of knowledge of its existence - but it's cool to know just in case.
Tom Archer - Visual C++ MVP
Archer Consulting Group
|
|
|
|
|
Well, new articles are on the way... but I'm very lazy in the summer.
|
|
|
|
|
You are right, Ted. I was searching codeproject for similar article. Therefor I had made a decision and I wrote this hint.
|
|
|
|
|
Wha-a! Great! Thank you I don't know that it is posible!
Wormhole is the God divided by zero
|
|
|
|
|
|