Reputation: 6741
I am writing a WPF application for a kiosk. The kiosk will go unused for extended periods so I want to create a "screen saver"-like feature where I can show some promotional slides that are then interrupted when a person touches the screen and starts the application. How can I go about doing this?
Upvotes: 2
Views: 3968
Reputation: 22008
I believe there is no problem to display screensaver. One can simply display a black window on top of everything:
<Window WindowStyle="None" WindowState="Maximized" Background="Black" Topmost="True" ... >
The actual problem is when to display such window. There are many possibilities and one which I like the most is utilizing winapi GetLastInputInfo
method, which returns ms
since last input. Use preferable way of polling (e.g. DispatcherTimer
) and if time exceeds threshold - display window, otherwise - hide window.
When dealing with winapi from C# use pinvoke.net for interop info and often readymade functions, e.g. GetLastInputTime.
Below is a complete snippet for a copy/paste use:
[DllImport("user32.dll")]
static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
[StructLayout(LayoutKind.Sequential)]
struct LASTINPUTINFO
{
[MarshalAs(UnmanagedType.U4)]
public UInt32 cbSize;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dwTime;
}
// returns seconds since last input
long GetIdleTime()
{
var info = new LASTINPUTINFO { cbSize = (uint)Marshal.SizeOf(typeof(LASTINPUTINFO)) };
GetLastInputInfo(ref info);
return (Environment.TickCount - info.dwTime) / 1000;
}
Usage:
var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1), IsEnabled = true };
timer.Tick += (s, e) =>
{
if (GetIdleTime() > 10*60) // 10 min
ShowWindow();
else
HideWindow();
};
Upvotes: 0
Reputation: 4322
It sounds like a screensaver would do what you want (like others have said) but you asked for a screen-saver like feature. I'm assuming that you don't need help making a window and showing the slideshow. Probably more like when to show it.
I had written an app (WPF) for a client several years ago (4+?) that needed to track when a user was and was not actively doing something in the application. Google (searches - not Google itself) and I came up with the following. I hope you find it useful. To use it:
AppActivityTimer activityTimer; // In my app, this is a member variable of the main window
activityTimer = new AppActivityTimer(
30*1000, // plain timer, going off every 30 secs - not useful for your question
5*60*1000, // how long to wait for no activity before firing OnInactive event - 5 minutes
true); // Does mouse movement count as activity?
activityTimer.OnInactive += new EventHandler(activityTimer_OnInactive);
activityTimer.OnActive += new PreProcessInputEventHandler(activityTimer_OnActive);
activityTimer.OnTimePassed += new EventHandler(activityTimer_OnTimePassed);
void activityTimer_OnTimePassed(object sender, EventArgs e)
{
// Regular timer went off
}
void activityTimer_OnActive(object sender, PreProcessInputEventArgs e)
{
// Activity detected (key press, mouse move, etc) - close your slide show, if it is open
}
void activityTimer_OnInactive(object sender, EventArgs e)
{
// App is inactive - activate your slide show - new full screen window or whatever
// FYI - The last input was at:
// DateTime idleStartTime = DateTime.Now.Subtract(activityTimer.InactivityThreshold);
}
And the AppActivityTimer class itself:
public class AppActivityTimer
{
#region Events - OnActive, OnInactive, OnTimePassed
public event System.Windows.Input.PreProcessInputEventHandler OnActive;
public event EventHandler OnInactive;
public event EventHandler OnTimePassed;
#endregion
#region TimePassed
private TimeSpan _timePassed;
private DispatcherTimer _timePassedTimer;
#endregion
#region Inactivity
public TimeSpan InactivityThreshold { get; private set; }
private DispatcherTimer _inactivityTimer;
private Point _inactiveMousePosition = new Point(0, 0);
private bool _MonitorMousePosition;
#endregion
#region Constructor
/// <summary>
/// Timers for activity, inactivity and time passed.
/// </summary>
/// <param name="timePassedInMS">Time in milliseconds to fire the OnTimePassed event.</param>
/// <param name="IdleTimeInMS">Time in milliseconds to be idle before firing the OnInactivity event.</param>
/// <param name="WillMonitorMousePosition">Does a change in mouse position count as activity?</param>
public AppActivityTimer(int timePassedInMS, int IdleTimeInMS, bool WillMonitorMousePosition)
{
_MonitorMousePosition = WillMonitorMousePosition;
System.Windows.Input.InputManager.Current.PreProcessInput += new System.Windows.Input.PreProcessInputEventHandler(OnActivity);
// Time Passed Timer
_timePassedTimer = new DispatcherTimer();
_timePassed = TimeSpan.FromMilliseconds(timePassedInMS);
// Start the time passed timer
_timePassedTimer.Tick += new EventHandler(OnTimePassedHandler);
_timePassedTimer.Interval = _timePassed;
_timePassedTimer.IsEnabled = true;
// Inactivity Timer
_inactivityTimer = new DispatcherTimer();
InactivityThreshold = TimeSpan.FromMilliseconds(IdleTimeInMS);
// Start the inactivity timer
_inactivityTimer.Tick += new EventHandler(OnInactivity);
_inactivityTimer.Interval = InactivityThreshold;
_inactivityTimer.IsEnabled = true;
}
#endregion
#region OnActivity
void OnActivity(object sender, System.Windows.Input.PreProcessInputEventArgs e)
{
System.Windows.Input.InputEventArgs inputEventArgs = e.StagingItem.Input;
if (inputEventArgs is System.Windows.Input.MouseEventArgs || inputEventArgs is System.Windows.Input.KeyboardEventArgs)
{
if (inputEventArgs is System.Windows.Input.MouseEventArgs)
{
System.Windows.Input.MouseEventArgs mea = inputEventArgs as System.Windows.Input.MouseEventArgs;
// no button is pressed and the position is still the same as the application became inactive
if (mea.LeftButton == System.Windows.Input.MouseButtonState.Released &&
mea.RightButton == System.Windows.Input.MouseButtonState.Released &&
mea.MiddleButton == System.Windows.Input.MouseButtonState.Released &&
mea.XButton1 == System.Windows.Input.MouseButtonState.Released &&
mea.XButton2 == System.Windows.Input.MouseButtonState.Released &&
(_MonitorMousePosition == false ||
(_MonitorMousePosition == true && _inactiveMousePosition == mea.GetPosition(Application.Current.MainWindow)))
)
return;
}
// Reset idle timer
_inactivityTimer.IsEnabled = false;
_inactivityTimer.IsEnabled = true;
_inactivityTimer.Stop();
_inactivityTimer.Start();
if (OnActive != null)
OnActive(sender, e);
}
}
#endregion
#region OnInactivity
void OnInactivity(object sender, EventArgs e)
{
// Fires when app has gone idle
_inactiveMousePosition = System.Windows.Input.Mouse.GetPosition(Application.Current.MainWindow);
_inactivityTimer.Stop();
if (OnInactive != null)
OnInactive(sender, e);
}
#endregion
#region OnTimePassedHandler
void OnTimePassedHandler(object sender, EventArgs e)
{
if (OnTimePassed != null)
OnTimePassed(sender, e);
}
#endregion
}
Upvotes: 4
Reputation: 23541
I dont know if there is something nativ, but you can make it.
Dont forget to add a timer to launch it.
By the way, I just google your question and find a lot of sample :
Upvotes: 6