4

I am trying to write a simple application to activate my screensaver when the mouse in at the top right corner of the screen. I have found an answer to controlling the screensaver from C# however I am having trouble working out how to do a "hot corner" type check for the mouse position. This is the only part I am stuck with, any help would be appreciated.

This Activates the screensaver

[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
    private static extern IntPtr GetDesktopWindow();

[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int  lParam);

private const int SC_SCREENSAVE = 0xF140;
private const int WM_SYSCOMMAND = 0x0112;

public static void SetScreenSaverRunning()
{
  SendMessage(GetDesktopWindow(), WM_SYSCOMMAND, SC_SCREENSAVE, 0);
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
CybrHwk
  • 95
  • 4

3 Answers3

4

You could use the System.Windows.Form.Screen class to get the current resolution (take a look at this answer). Then use Cursor.Position.Property to determine where the cursor is currently located (i.e. is it within the boundaries of some predefined rectangle that should activate it).

Community
  • 1
  • 1
Jason Down
  • 21,731
  • 12
  • 83
  • 117
1

I have made the exact same thing, only it loads in the top left. What I did was just make the form size 1px by 1px with no border, and just activate the screensaver when the mouse stays over the form for a second. Doing it this way requires that you find all ways to keep the form on top of everything.

Another option would be mouse hooking and just watching for (0,0) mouse position, or for the top right - (0, screen.width)

makman99
  • 1,045
  • 14
  • 18
  • I do like the top left corner approach. It simplifies things quite a bit. – Jason Down Dec 24 '11 at 17:26
  • Went for the low level mouse hook, which is working now, thanks for the suggestion. Just about got the basics working, screensaver is now activating when the mouse is in the top right, just need to add code to be able to change which corner is the "hot corner" – CybrHwk Dec 24 '11 at 18:50
  • Does your process's memory increase every time the screen saver turns on? When I tried the mouse hook a while ago I remember the memory usage just getting larger and larger which is why I kept with 1x1 form. – makman99 Dec 24 '11 at 23:21
  • Now that you mention it, the memory goes up everytime I move the mouse! – CybrHwk Dec 25 '11 at 00:11
  • I have tried other classes that also do hooking and they are also leaking exactly 8k after the mouse has moved for a while... I am going to remove the screen saver control stuff to see if it goes up just receiving the events to try an pin point the memory leak. – CybrHwk Dec 25 '11 at 14:29
  • Further investigation I am beginning to this its Windows 7 Aero that could be causing it. I've now tried various classes for doing the hooking and every single one causes an 8k memory leak after you have moved the mouse a significant distance – CybrHwk Dec 25 '11 at 20:55
  • @CybrHwk: It's certainly possible to write a Windows hook that doesn't leak; I've done it several times. If you think yours has a memory leak problem, post the code. You might want to open a new question for this. I have a sneaking suspicion that what you're regarding as a memory leak is actually just the result of .NET's garbage collection. – Cody Gray - on strike Dec 26 '11 at 09:48
  • The adding of 8k does stop after a bit. In my application it went from 3,800k to 4,880k and then stopped. – makman99 Dec 26 '11 at 19:13
  • Yeah, you guys are right it does stop after a period of time... is a bit odd however, considering the program doesn't do very much! – CybrHwk Dec 27 '11 at 20:20
0

You could also try ScrHots from Lucian Wischik. It's freeware and does exactly what you need, and also has hot-corners for "never activate the screensaver" capability. All four corners can be programmed to do either function. I've used this one for years, and it works great.

http://www.wischik.com/scr/savers.html (ScrHots3, under the "Utilities" section)

Hope this helps someone.

weasel5i2
  • 119
  • 1
  • 3