0

I am creating a basic Windows Form Application. If I run my project (program start), how can I set my form to automatically minimize when a user doesn't interact with my form?

For example, when you watch some youtube video in full screen, it will display the bar player, when user doesn't do or move anything inside the player, the bar player will automatically hidden.

So, how can I create something similar like this? And how does it do that?

halfer
  • 19,824
  • 17
  • 99
  • 186
Teik Fai
  • 53
  • 7

3 Answers3

0

You could define a timer which calls your hiding operation after for instance 15 seconds. Then you attach eventhandlers for MouseMove, MouseClick and KeyPressed to your form. Every time such an event occurs, it resets your timer.

derpirscher
  • 14,418
  • 3
  • 18
  • 35
  • Hi Sir, could you teach me how? So what code should I have to write for the timer one? Sorry , I'm still new to c#. – Teik Fai Jul 17 '16 at 10:21
  • This is not a teaching site. There are tons of tutorials on c#, timers, eventhandlers ... just use the search engine of your joice. If you have problems with some specific code you wrote, we'll be happy to help you. – derpirscher Jul 17 '16 at 10:28
0

Here is simple example.

You can minimize From by set property WindowState to FormWindowState.Minimized.

To handle time when Form minimizes use Timer. On Tick event compare current time of the Timer to yours defined time. On user interrupt (input events as MouseMove, MouseClick, KeyPress, or you can choose just some of them) reset time to 0.

public partial class Form1 : Form
{

    System.Windows.Forms.Timer timer;
    int milliseconds;
    const int TIME_TO_MINIMIZE = 1000;

    public Form1()
    {
        InitializeComponent();

        this.MouseMove += new MouseEventHandler(InputAction);
        this.MouseClick += new MouseEventHandler(InputAction);
        this.KeyPress += new KeyPressEventHandler(InputAction);

        milliseconds = 0;

        timer = new System.Windows.Forms.Timer();
        timer.Interval = 100;
        timer.Tick += new EventHandler(timer_Tick);
        timer.Start();
    }

    private void timer_Tick(object sender, EventArgs e)
    {
        milliseconds += 100;
        if (milliseconds >= TIME_TO_MINIMIZE)
        {
            this.WindowState = FormWindowState.Minimized;
            milliseconds = 0;
        }
    }

    private void InputAction(object sender, EventArgs e)
    {
        milliseconds = 0;
    }
}
David Pivovar
  • 115
  • 1
  • 10
0

You can do it in following way:

public partial class frmTimer : Form
{
    System.Windows.Forms.Timer timer;

    public frmTimer()
    {
        InitializeComponent();

        timer = new Timer();
        timer.Enabled = true;
        timer.Interval = 10 * 1000;
        timer.Tick += Timer_Tick;
        timer.Start();

        // attach your events on which you want to reset your events
        this.MouseMove += FrmTimer_MouseMove;
        this.MouseDown += FrmTimer_MouseDown;
        this.KeyDown += FrmTimer_KeyDown;
    }

    private void FrmTimer_KeyDown(object sender, KeyEventArgs e)
    {
        timer.Stop();
        timer.Start();
    }

    private void FrmTimer_MouseDown(object sender, MouseEventArgs e)
    {
        timer.Stop();
        timer.Start();
    }

    private void FrmTimer_MouseMove(object sender, MouseEventArgs e)
    {
        timer.Stop();
        timer.Start();
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        if (this.WindowState != FormWindowState.Minimized)
            this.WindowState = FormWindowState.Minimized;
    }

}

I hope this will help you. :)

Hitesh
  • 3,508
  • 1
  • 18
  • 24