Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Kill a screensaver...

Kill a screensaver...

Scheduled Pinned Locked Moved C / C++ / MFC
questioncomalgorithms
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Matt Eckerson
    wrote on last edited by
    #1

    Hello all, I have a dialog app that sits in the tray and pops up a dialog to remind the user of something every hour. It obviously does no good if a screensaver is running.:) I did some searching around and found ways of interrupting it and found some good stuff from Bob Moore's site:

    void CMCAlarmDlg::KillScreenSaver()
    {
    HWND hwnd;

    if (hwnd = ::FindWindow ("WindowsScreenSaverClass", NULL))
    {
    	::PostMessage(hwnd, WM\_CLOSE, 0, 0);
    	// or tried 
                //::SendMessage(hwnd, WM\_CLOSE, 0, 0);
    }
    else
    {
    	if (hwnd = ::FindWindow ("Default Screen Saver", NULL))
    	{
    		::PostMessage(hwnd, WM\_CLOSE, 0, 0);
    	}
    }
    

    }

    This does work on Win2K, but not on NT 4. I've tried several other things (tried this one too: http://support.microsoft.com/default.aspx?scid=kb;en-us;Q140723)but haven't had any success. Several people have mentioned how unreliable FindWindow() can be. My question is, is there another way to interrupt the screensaver by a keyboard or a mouse? Bob Moore mentions doing this himself with hooks, but doesn't elaborate. Hooks are a bit beyond my beginners skill level anyway. Or has anyone had good luck with something that works across the board (9x, NT, 2K, XP) Ideas? :-D Thank you all (again) in advance. Matt

    A 1 Reply Last reply
    0
    • M Matt Eckerson

      Hello all, I have a dialog app that sits in the tray and pops up a dialog to remind the user of something every hour. It obviously does no good if a screensaver is running.:) I did some searching around and found ways of interrupting it and found some good stuff from Bob Moore's site:

      void CMCAlarmDlg::KillScreenSaver()
      {
      HWND hwnd;

      if (hwnd = ::FindWindow ("WindowsScreenSaverClass", NULL))
      {
      	::PostMessage(hwnd, WM\_CLOSE, 0, 0);
      	// or tried 
                  //::SendMessage(hwnd, WM\_CLOSE, 0, 0);
      }
      else
      {
      	if (hwnd = ::FindWindow ("Default Screen Saver", NULL))
      	{
      		::PostMessage(hwnd, WM\_CLOSE, 0, 0);
      	}
      }
      

      }

      This does work on Win2K, but not on NT 4. I've tried several other things (tried this one too: http://support.microsoft.com/default.aspx?scid=kb;en-us;Q140723)but haven't had any success. Several people have mentioned how unreliable FindWindow() can be. My question is, is there another way to interrupt the screensaver by a keyboard or a mouse? Bob Moore mentions doing this himself with hooks, but doesn't elaborate. Hooks are a bit beyond my beginners skill level anyway. Or has anyone had good luck with something that works across the board (9x, NT, 2K, XP) Ideas? :-D Thank you all (again) in advance. Matt

      A Offline
      A Offline
      Alexander Wiseman
      wrote on last edited by
      #2

      Hello, There is a nice little function which simulates a mouse event (by sending a mouse message to the active window) called 'mouse_event'. Sending a MOUSE_MOVE message to a screen saver would make it kill itself, because it automatically closes itself wheenever there is any mouse activity. MSDN said that on Windows NT and 2000, 'mouse_event' has been superseded by 'SendInput', so here is the information for both functions (you might want to check the OS, so you can use the correct function): 'mouse_event':

      VOID mouse_event(
      DWORD dwFlags, // motion and click options
      DWORD dx, // horizontal position or change
      DWORD dy, // vertical position or change
      DWORD dwData, // wheel movement
      ULONG_PTR dwExtraInfo // application-defined information
      }

      dwFlags: look up this function on MSDN to see all the options; the one you want is MOUSEEVENTF_MOVE, to simulate a mouse movement. MOUSEEVENTF_ABSOLUTE can be combined with this flag (e.g. MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE) to let you specify absolute coordinates in 'dx' and 'dy'. dx: if MOUSEEVENTF_ABSOLUTE is set in the firest parameter, this specifies the absolute position along the 'x' axis of the mouse. Othwerwise, this specifies how many points you want to move along the 'x' axis. dy: same as 'dx' only for the 'y' axis dwData: this specifies mouse wheel movement; you don't need to worry about this, because you won't be using the MOUSEEVENTF_WHEEL option. Just set it to 0 or NULL. dwExtraInfo: used to send a value with the message. Set this to 0 or NULL for your purposes. 'SetInput':

      UINT SetInput(
      UINT nInputs, // count of input events
      LPINPUT pInputs, // array of input events
      int cbSize // size of structure
      }

      nInputs: number of input events (mouse events) that you want to send pInputs: a pointer to an array of INPUT structures. An input structure looks like this:

      typedef struct tagINPUT {
      DWORD type;
      union {
      MOUSEINPUT mi;
      KEYBDINPUT ki;
      HARDWAREINPUT hi;
      };
      } INPUT, *PINPUT;

      For your purposes, you want to use the 'mi' variable, so you would set 'type' equal to INPUT_MOUSE. The MOUSEINPUT structure contains almost identical variables to the 'mouse_event' function. To send a movement command, you would set the 'dwFlags' variable of the structure to: MOUSEEVENTF_MOVE or MOUSEEVENTF_ABSOLUTE, and the 'dx' and 'dy' variables to the coordinates of your choice. Every other var

      M 1 Reply Last reply
      0
      • A Alexander Wiseman

        Hello, There is a nice little function which simulates a mouse event (by sending a mouse message to the active window) called 'mouse_event'. Sending a MOUSE_MOVE message to a screen saver would make it kill itself, because it automatically closes itself wheenever there is any mouse activity. MSDN said that on Windows NT and 2000, 'mouse_event' has been superseded by 'SendInput', so here is the information for both functions (you might want to check the OS, so you can use the correct function): 'mouse_event':

        VOID mouse_event(
        DWORD dwFlags, // motion and click options
        DWORD dx, // horizontal position or change
        DWORD dy, // vertical position or change
        DWORD dwData, // wheel movement
        ULONG_PTR dwExtraInfo // application-defined information
        }

        dwFlags: look up this function on MSDN to see all the options; the one you want is MOUSEEVENTF_MOVE, to simulate a mouse movement. MOUSEEVENTF_ABSOLUTE can be combined with this flag (e.g. MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE) to let you specify absolute coordinates in 'dx' and 'dy'. dx: if MOUSEEVENTF_ABSOLUTE is set in the firest parameter, this specifies the absolute position along the 'x' axis of the mouse. Othwerwise, this specifies how many points you want to move along the 'x' axis. dy: same as 'dx' only for the 'y' axis dwData: this specifies mouse wheel movement; you don't need to worry about this, because you won't be using the MOUSEEVENTF_WHEEL option. Just set it to 0 or NULL. dwExtraInfo: used to send a value with the message. Set this to 0 or NULL for your purposes. 'SetInput':

        UINT SetInput(
        UINT nInputs, // count of input events
        LPINPUT pInputs, // array of input events
        int cbSize // size of structure
        }

        nInputs: number of input events (mouse events) that you want to send pInputs: a pointer to an array of INPUT structures. An input structure looks like this:

        typedef struct tagINPUT {
        DWORD type;
        union {
        MOUSEINPUT mi;
        KEYBDINPUT ki;
        HARDWAREINPUT hi;
        };
        } INPUT, *PINPUT;

        For your purposes, you want to use the 'mi' variable, so you would set 'type' equal to INPUT_MOUSE. The MOUSEINPUT structure contains almost identical variables to the 'mouse_event' function. To send a movement command, you would set the 'dwFlags' variable of the structure to: MOUSEEVENTF_MOVE or MOUSEEVENTF_ABSOLUTE, and the 'dx' and 'dy' variables to the coordinates of your choice. Every other var

        M Offline
        M Offline
        Matt Eckerson
        wrote on last edited by
        #3

        Excellent! This looks exactly like what I need. I'll try this out and let you know. Thanks Alexander for your time. Matt

        A 1 Reply Last reply
        0
        • M Matt Eckerson

          Excellent! This looks exactly like what I need. I'll try this out and let you know. Thanks Alexander for your time. Matt

          A Offline
          A Offline
          Alexander Wiseman
          wrote on last edited by
          #4

          Great! Let me know if it works out for you! :-D Sincerely, Alexander Wiseman Est melior esse quam videri It is better to be than to seem

          M 1 Reply Last reply
          0
          • A Alexander Wiseman

            Great! Let me know if it works out for you! :-D Sincerely, Alexander Wiseman Est melior esse quam videri It is better to be than to seem

            M Offline
            M Offline
            Matt Eckerson
            wrote on last edited by
            #5

            Hello Alexander, Well after implementing your idea, I've come to find out that it works great on Win2k, but not on NT. I didn't have the opportunity to test it on XP, but I've since found a newsgroup posting that suggests that it does (his code and sugguestions were very similar). I ended up using this: http://www.mooremvp.freeserve.co.uk/Win32/framed\_tip022.htm; and modified it a bit 'til it started working. Like I said, yours worked great for 2k (and probably XP). I've stored your reply for future use if needed. You might test it on NT and see what your results are. If you find something, let me know since it's alot easier than doing it like the link above shows. Can't thank you enough for your help. Your instructions were comprehensive and easy to follow. Thanks again, Matt

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups