How to detect if monitor was on or off with C#: "AnswerHow to detect if monitor was on or off with C#
*
Monday, June 01, 2009 8:48 AMRiteru Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
Sign In to Vote
0
Sign In to Vote
I'm trying to write a software to detect the status of monitor (or perhap HD LCD television screen) in order to create a report about the availability of the output screen.
- I've tried to use WMI (Win32_DesktopMonitor ) to check availability but it alway return 3 which mean On, no matter my computer LCD screen is On or Off;
Anyone have any idea on detecting the monitor status?
PS. The LCD screen i'm using connected to my PC via DVI port, I'm not sure yet which port will be used in the real run (DVI, Serial , etc) so I'm trying to find a solution that can support all this port.
o ReplyReply
o QuoteQuote
Answers
*
Tuesday, June 02, 2009 12:32 PMJagadeesan Kandasamy Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
Answer
Sign In to Vote
0
Sign In to Vote
I hope this might be helpful...
Try to use with GetDesktopWindow(), The below code is just to switch on / off the moniter, from this you can try to the status as well.
[DllImport(
'user32.dll')]
static
extern IntPtr SendMessage(IntPtr hWnd, uint Msg,
IntPtr wParam, IntPtr lParam);
//Or
[DllImport(
'user32.dll', SetLastError = true)]
static
extern bool PostMessage(IntPtr hWnd, uint Msg,
IntPtr wParam, IntPtr lParam);
//Or
[DllImport(
'user32.dll')]
static
extern bool PostThreadMessage(uint idThread, uint Msg,
UIntPtr wParam, IntPtr lParam);
//Or
[DllImport(
'user32.dll', SetLastError=true, CharSet=CharSet.Auto)]
static
extern bool SendMessageCallback(IntPtr hWnd, uint Msg,
UIntPtr wParam, IntPtr lParam,
SendMessageDelegate lpCallBack,
UIntPtr dwData);
//Or
[DllImport(
'user32.dll', SetLastError=true, CharSet=CharSet.Auto)]
static
extern bool SendNotifyMessage(IntPtr hWnd, uint Msg,
UIntPtr wParam, IntPtr lParam);
//Or
[DllImport(
'user32.dll')]
private
static extern IntPtr GetDesktopWindow();
//Or
[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);
const
int SC_MONITORPOWER = 0xF170;
const
int WM_SYSCOMMAND = 0x0112;
const
int MONITORON = -1;
const
int MONITOROFF = 2;
const
int MONITORSTANBY = 1;
int
HWND_BROADCAST = 0xffff; //the message is sent to all top-level windows in the system
int
HWND_TOPMOST = -1;
int
HWND_TOP = 0; //
int
HWND_BOTTOM = 1; //limited use
int
HWND_NOTOPMOST = -2; //
Form frm =
new Form();
Frm.Handle();
SendMessage(ValidHWND, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOROFF );
SendMessage(ValidHWND, WM_SYSCOMMAND, SC_MONITORPOWER, MONITORON);
Regards, Jagadeesan
o Marked As Answer byBin-ze ZhaoMicrosoft, ModeratorFriday, June 05, 2009 2:04 AM
o
o ReplyReply
o QuoteQuote
All Replies
*
Tuesday, June 02, 2009 12:32 PMJagadeesan Kandasamy Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
Answer
Sign In to Vote
0
Sign In to Vote
I hope this might be helpful...
Try to use with GetDesktopWindow(), The below code is just to switch on / off the moniter, from this you can try to the status as well.
[DllImport(
'user32.dll')]
static
extern IntPtr SendMessage(IntPtr hWnd, uint Msg,
IntPtr wParam, IntPtr lParam);
//Or
[DllImport(
'user32.dll', SetLastError = true)]
static
extern bool PostMessage(IntPtr hWnd, uint Msg,
IntPtr wParam, IntPtr lParam);
//Or
[DllImport(
'user32.dll')]
static
extern bool PostThreadMessage(uint idThread, uint Msg,
UIntPtr wParam, IntPtr lParam);
//Or
[DllImport(
'user32.dll', SetLastError=true, CharSet=CharSet.Auto)]
static
extern bool SendMessageCallback(IntPtr hWnd, uint Msg,
UIntPtr wParam, IntPtr lParam,
SendMessageDelegate lpCallBack,
UIntPtr dwData);
//Or
[DllImport(
'user32.dll', SetLastError=true, CharSet=CharSet.Auto)]
static
extern bool SendNotifyMessage(IntPtr hWnd, uint Msg,
UIntPtr wParam, IntPtr lParam);
//Or
[DllImport(
'user32.dll')]
private
static extern IntPtr GetDesktopWindow();
//Or
[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);
const
int SC_MONITORPOWER = 0xF170;
const
int WM_SYSCOMMAND = 0x0112;
const
int MONITORON = -1;
const
int MONITOROFF = 2;
const
int MONITORSTANBY = 1;
int
HWND_BROADCAST = 0xffff; //the message is sent to all top-level windows in the system
int
HWND_TOPMOST = -1;
int
HWND_TOP = 0; //
int
HWND_BOTTOM = 1; //limited use
int
HWND_NOTOPMOST = -2; //
Form frm =
new Form();
Frm.Handle();
SendMessage(ValidHWND, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOROFF );
SendMessage(ValidHWND, WM_SYSCOMMAND, SC_MONITORPOWER, MONITORON);
Regards, Jagadeesan
o Marked As Answer byBin-ze ZhaoMicrosoft, ModeratorFriday, June 05, 2009 2:04 AM
o
– Enviado usando a Barra de Ferramentas Google"
segunda-feira, 22 de novembro de 2010
Assinar:
Postar comentários (Atom)
Nenhum comentário:
Postar um comentário