Prevent Monitor Standby and ScreenSaver Activation
-
Hi, I'm writing a MediaPlayer so I need to keep the monitor on and screen saver disabled during the life of my application. I understand that I need to handle certain windows messages SC_SCREENSAVE, SC_MONITORPOWER but I'm not sure how. So Please Advice
Thanks...
-
I think the below code will help you
//Using InteropServices we import Dlls User32.dll
using System.Threading;
using System.Runtime.InteropServices;//Inside Class
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool SystemParametersInfo(
int uAction, int uParam, ref bool lpvParam,
int flags);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool SystemParametersInfo(
int uAction, int uParam, ref int lpvParam,
int flags);private const int SPI_GETSCREENSAVERACTIVE = 16;
private const int SPI_SETSCREENSAVERACTIVE = 17;//Method Start with Bool return Type which returns Screen Saver is Acitvate or deActivate
bool isActive = false;SystemParametersInfo(SPI_GETSCREENSAVERACTIVE, 0,
ref isActive, 0);
return isActive;
//Method start here with parameter int Active to Acitvate or deActivate Screen Saver
int nullVar = 0;SystemParametersInfo(SPI_SETSCREENSAVERACTIVE,
Active, ref nullVar, SPIF_SENDWININICHANGE); -
If I've understood it correctly then the code needs to be called again and again inside a Thread and keep check if ScreenSaver is Active and if so Deactivate it. I believe there as to be a cleaner method. I've been suggested to handle Windows Messages. Please look at the code below, it also doesn't work but uses a different approach
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void PostQuitMessage(int returnVal);
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 0x0112:
//case WM_SYSCOMMAND:
switch (m.WParam.ToInt32())
{
// disable screen saver and monitor power-saving mode
case 0xF140:
//case SC_SCREENSAVE:
case 0xF170:
//case SC_MONITORPOWER:
return;
}
break;
//case WM_DESTROY:
case 0003:
PostQuitMessage(0);
return;
}
//return DefWindowProc(hwnd, message, wParam, lParam);
base.DefWndProc(ref m);
} -
Is it Throwing any error?
and where you are calling this(WndProc()) method?
-
Thanks
-
Thanks
for the Reply. -
Thanks
for the -
Thanks
for the Reply. I -
Thanks
for the Reply. The WndProc method mentioned belongs to my -
Thanks
for the Reply. The WndProc method mentioned belongs to my mainform. No Exceptions are raised -
Thanks
for the Reply. The -
Thanks
for the Reply. The WndProc -
Thanks
for the Reply. The WndProc method mentioned belongs to my mainform. No Exceptions are raised but my application just exits without anything, Runs fine without that. -
Thanks
for the Reply. The WndProc method mentioned belongs to my mainform. No Exceptions are raised but my application just exits without anything, Runs fine without that. A -
Thanks
for the Reply. The WndProc method -
Thanks
for the Reply. The WndProc method mentioned belongs to my mainform. No Exceptions are raised but my application just exits without anything, Runs fine -
Thanks
for the Reply. The WndProc method mentioned belongs to my mainform. No Exceptions are raised but my application just exits without anything, Runs fine without that. I've posted my query -
Thanks
for the Reply. The WndProc method mentioned belongs to my mainform. NO -
Thanks
for the Reply. The WndProc method mentioned is -
Thanks
for the Reply. The WndProc method mentioned belongs to my mainform. No Exceptions are raised but my application just exits without anything, Runs fine without that. I've posted my query on -
Thanks
for the Reply. The WndProc method mentioned belong s to -
Thanks
for the Reply. The WndProc method mentioned belongs to my mainform. No Exceptions are raised but -
Thanks
for the Reply. The WndProc method mentioned belongs to my mainform. No Exceptions are -
Thanks
for the Reply. The WndProc method mentioned belongs to my mainform. No Exceptions are raised but my -
Thanks
for the Reply. The WndProc method mentioned belongs to my mainform. No Exceptions are raised but my application -
Thanks
for the Reply. The WndProc method mentioned belongs to my mainform. No Exceptions are raised but my application just -
Thanks
for the Reply. The WndProc method mentioned belongs to my mainform. No Exceptions are raised but my application just exits without anything, Runs fine without that. I've -
Thanks
for the Reply. The WndProc method mentioned belongs to my mainform. No Exceptions are raised but my application just exits -
Thanks
for the Reply. The WndProc method mentioned belongs to my mainform. No Exceptions are raised but my application just exits without -
Thanks
for the Reply. The WndProc method mentioned belongs to my mainform. No Exceptions are raised but my application just exits without anything. Runs -
Thanks
for the Reply. The WndProc method mentioned belongs to my mainform. No Exceptions are raised but my application just exits without anything, Runs fine without that. I've posted my -
There seems to be some Problem here. Please visit http://www.codeproject.com/script/comments/forums.asp?msg=2272176&forumid=1649#xx2272176xx
I've posted the same question there. And Yes I'm using the mentioned WndProc method in my MainForm and no Exceptions are Thrown, My App Just Exits ;-(
-
Just want to ask.... Why so many replies?
-
switch (m.Msg)
{
case 0x0112:
//case WM_SYSCOMMAND:
switch (m.WParam.ToInt32())
{
// disable screen saver and monitor power-saving mode
case 0xF140:I think problem may be in the bolded areas. (It’s difficult to get the solution for a homework project.)
-
Sorry About so many Messages. The Quick Reply Module of DeveloperFusion did this out of my single Message. I have notified them about this. And Here is the Solution
const int WM_SYSCOMMAND = 0x0112, SC_SCREENSAVE = 0xF140, SC_MONITORPOWER = 0xF170;
protected override void WndProc(ref Message m)
{
if( m.Msg == WM_SYSCOMMAND ) //Intercept System Command
{
if( m.WParam.ToInt32() == SC_SCREENSAVE || m.WParam.ToInt32() == SC_MONITORPOWER )
{ //Intercept ScreenSaver and Monitor Power Messages
//MessageBox.Show("Reached SC/PowerCondition");
return; //
}
}
base.WndProc(ref m);
} -
You got the solution? That's good.
I have a question, Can i know why this condition?
if( m.WParam.ToInt32() == SC_SCREENSAVE || m.WParam.ToInt32() == SC_MONITORPOWER )
-
Before Activating ScreenSaver Windows Sends a WM_SYSCOMMAND Message with wParam as SC_SCREENSAVE for Screen Saver Activation. We Just Intercept those Messages before they can be processed by our Window (we simply return). Same is true for wParam = SC_MONITORPOWER Message. I also dont know much about that.
Navigate to Following links to Know More
SC_SCREENSAVE/SC_MONITORPOWER
http://www.delphicorner.f9.co.uk/articles/wapi11.htm
http://www.chami.com/tips/delphi/121196D.html
http://www.pixvillage.com/blogs/devblog/archive/2007/02/27/6493.aspxWM_SYSCOMMAND
http://msdn2.microsoft.com/en-us/library/ms646360.aspx
Post a reply
C# forum discussion
-
USB Drive Activity Logger!
by coque0912 (7 replies)
-
Exporting data from a repeater to an Excel sheet
by TomBlack (28 replies)
-
Calculate Date of Birth from Age
by k.prashanth (7 replies)
-
Windows Service, getting outOf memory exception
by programmerdude2341 (3 replies)
-
How to Make Default Button?
by puspitaonlinebooks (24 replies)
C# tutorials
- 5 Things You Should Know About Developing Windows 8 Apps with C#
- Taking the Single Responsibility Principle Seriously
- An Introduction To Practical AOP Using PostSharp
- Web Testing with MbUnit and WatiN Part 3: Testing Asynchronous AJAX Calls
- Web Testing with MbUnit and WatiN Part 1: Keeping Your Tests Legible
Quick links
Recent activity
- arif ahmad replied to How to receive data in web ...
- William Thompson replied to What is the name of the Win...
- Sameera Piyadigamage replied to Point of Sale Developers: H...
- Scott Carline replied to 4 x C# Developers for large...
- Rajendra Dhakal replied to Restore SQL Server text dat...
- cloud rainda replied to How to convert between TS f...
Enter your message below
Sign in or Join us (it's free).