Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: Create a screen-saver feature when application is inactive?

Tags:

c#

wpf

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?

like image 211
Unknown Coder Avatar asked Dec 09 '25 09:12

Unknown Coder


2 Answers

I dont know if there is something nativ, but you can make it.

  • Open another window without border
  • Make it the top of the screen and maximized it (fullscreen).
  • Run a video (or any kind of media/animation) in it. (your promotional slides)
  • Check if someone press any key, touch the screen, or move the mouse to close it.

Dont forget to add a timer to launch it.

By the way, I just google your question and find a lot of sample :

  • WPFScreensaver CodePlex
  • Basic Screen saver in WPF
  • WPF Screen Saver Visual Studio Template!
  • WPF Custom Screen Saver Art
  • Making a C# screensaver
  • Creating a Screen Saver with C#
like image 76
aloisdg Avatar answered Dec 11 '25 21:12

aloisdg


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
}
like image 42
J.H. Avatar answered Dec 11 '25 21:12

J.H.