Prevent Monitor Standby and ScreenSaver Activation

csharp Kenya
  • 12 years ago

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

  • 12 years ago

    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);

  • 12 years ago

    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);
    }

      

  • 12 years ago

    Is it Throwing any error?

    and where you are calling this(WndProc()) method?

     

  • 12 years ago

    Thanks

  • 12 years ago

    Thanks

    for the Reply.
  • 12 years ago

    Thanks

    for the
  • 12 years ago

    Thanks

    for the Reply. I
  • 12 years ago

    Thanks

    for the Reply. The WndProc method mentioned belongs to my
  • 12 years ago

    Thanks

    for the Reply. The WndProc method mentioned belongs to my mainform. No Exceptions are raised
  • 12 years ago

    Thanks

    for the Reply. The
  • 12 years ago

    Thanks

    for the Reply. The WndProc
  • 12 years ago

    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.
  • 12 years ago

    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
  • 12 years ago

    Thanks

    for the Reply. The WndProc method
  • 12 years ago

    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
  • 12 years ago

    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
  • 12 years ago

    Thanks

    for the Reply. The WndProc method mentioned belongs to my mainform. NO
  • 12 years ago

    Thanks

    for the Reply. The WndProc method mentioned is
  • 12 years ago

    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
  • 12 years ago

    Thanks

    for the Reply. The WndProc method mentioned belong s to
  • 12 years ago

    Thanks

    for the Reply. The WndProc method mentioned belongs to my mainform. No Exceptions are raised but
  • 12 years ago

    Thanks

    for the Reply. The WndProc method mentioned belongs to my mainform. No Exceptions are
  • 12 years ago

    Thanks

    for the Reply. The WndProc method mentioned belongs to my mainform. No Exceptions are raised but my
  • 12 years ago

    Thanks

    for the Reply. The WndProc method mentioned belongs to my mainform. No Exceptions are raised but my application
  • 12 years ago

    Thanks

    for the Reply. The WndProc method mentioned belongs to my mainform. No Exceptions are raised but my application just
  • 12 years ago

    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
  • 12 years ago

    Thanks

    for the Reply. The WndProc method mentioned belongs to my mainform. No Exceptions are raised but my application just exits
  • 12 years ago

    Thanks

    for the Reply. The WndProc method mentioned belongs to my mainform. No Exceptions are raised but my application just exits without
  • 12 years ago

    Thanks

    for the Reply. The WndProc method mentioned belongs to my mainform. No Exceptions are raised but my application just exits without anything. Runs
  • 12 years ago

    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
  • 12 years ago

    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 ;-(

  • 12 years ago

    Just want to ask.... Why so many replies?

  • 12 years ago

    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.)

  • 12 years ago

    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);
    }

  • 12 years ago

    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 )

  • 12 years ago

    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.aspx

    WM_SYSCOMMAND
    http://msdn2.microsoft.com/en-us/library/ms646360.aspx

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“God could create the world in six days because he didn't have to make it compatible with the previous version.”