Solved

Tracking mouse movement in winforms

Posted on 2009-05-15
17
675 Views
Last Modified: 2012-05-07
Hi experts,

         In my attached file[code.txt], i am showing a screensaver to the user after the delay of 8 minutes.

         Now, i shud deactivate the screesaver on mouse click and mouse movement anywhere in the form. I have written the MouseCaptureChanged event to close the screensaver.

         How to exit the screensaver on a movement in mouse ????

         Please help me on this. I am a new bie in winforms...Thanks.
private void Screensaver_MouseCaptureChanged(object sender, EventArgs e)
        {
            this.Close();
        }

Open in new window

code.txt
0
Comment
Question by:srk1982
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 6
  • 2
17 Comments
 
LVL 13

Expert Comment

by:iHadi
ID: 24393219
Hi

try using :

Application.Exit(); instead of this.Close();
0
 

Author Comment

by:srk1982
ID: 24393271
Hi iHadi,

my question is

 How to exit the screensaver on a movement in mouse ????


Thanks...
0
 

Author Comment

by:srk1982
ID: 24393279
Hi iHadi,

my question is

 How to exit the screensaver on a movement in mouse ????

 I want to capture the mouse movement on the screen and if there is any movement just close the window.


Thanks...
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 13

Expert Comment

by:iHadi
ID: 24393322
try
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    Application.Exit();
    // or
    // this.Close();
}

Open in new window

0
 

Author Comment

by:srk1982
ID: 24393347
Hi iHadi,

     I tried that already...
     If i use that, the screensaver is vanishning in less than one second, even if there is no mouse movement and keyboard inputs.  :(
0
 
LVL 13

Expert Comment

by:iHadi
ID: 24393395
try the following:
bool beginDetect;
 
public Form1()
{
    InitializeComponent();
    timer1.Interval = 1000;
    timer1.Start();
}
 
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    if (beginDetect)
        Application.Exit();
}
 
private void timer1_Tick(object sender, EventArgs e)
{
    beginDetect = true;
    timer1.Stop();
}

Open in new window

0
 

Author Comment

by:srk1982
ID: 24393423
Hi iHadi,

    I am experiancing the same problem, the screensaver vanishes automatically.
    please see the below code.

    this.close() - exits the screensaver form.
    application.exit() - exits the whole application.

Thanks..
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WinDemo
{
    public partial class Screensaver : Form
    {
        bool beginDetect;
 
        public Screensaver()
        {
            InitializeComponent();
            timer1.Interval = 1000;
            timer1.Start();
 
            this.FormBorderStyle = FormBorderStyle.None;
            this.WindowState = FormWindowState.Maximized;
            this.Size = new Size(this.Size.Width * 2, this.Size.Height); 
        }
       
        private void Screensaver_MouseCaptureChanged_1(object sender, EventArgs e)
        {
            this.Close();
        }
 
        private void Screensaver_MouseMove(object sender, MouseEventArgs e)
        {
            if (beginDetect)
                this.Close();
        }
 
        private void timer1_Tick(object sender, EventArgs e)
        {
            beginDetect = true;
            timer1.Stop();
        }     
    } 
}

Open in new window

0
 
LVL 13

Expert Comment

by:iHadi
ID: 24393458

        private void Screensaver_MouseCaptureChanged_1(object sender, EventArgs e)
        {
            if (beginDetect)
                this.Close();
        }

Open in new window

0
 

Author Comment

by:srk1982
ID: 24393512
Hi :iHadi,

     MouseCaptureChanged event accepts only right clicks and not working for
mouse movements.
0
 
LVL 13

Expert Comment

by:iHadi
ID: 24393717
Then just ignore it and don't use it. The MouseMove event will be enough. If you are still having the problem, please post your full code or attach the form's files so I can have a look at it.
0
 

Author Comment

by:srk1982
ID: 24393741
Hi Hiadi,

     Please take a look at the posted code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WinDemo
{
    public partial class Screensaver : Form
    {
        bool beginDetect;
 
        public Screensaver()
        {
            InitializeComponent();
            timer1.Interval = 1000;
            timer1.Start();
 
            this.FormBorderStyle = FormBorderStyle.None;
            this.WindowState = FormWindowState.Maximized;
            this.Size = new Size(this.Size.Width * 2, this.Size.Height); 
        }
 
        private void Screensaver_MouseMove(object sender, MouseEventArgs e)
        {
            if (beginDetect)
                this.Close();
        }
 
        private void timer1_Tick(object sender, EventArgs e)
        {
            beginDetect = true;
            timer1.Stop();
        }     
    } 
}

Open in new window

0
 
LVL 13

Expert Comment

by:iHadi
ID: 24393776
The code is working great for me. Let me describe what I understand the code should do. When the program is run, the screensaver form appears in maximized state. If the mouse moves or clicks, the form should close. There is a delay of 1 second so the form displays and stabilize before reacting to mouse events.

Did I get everything right or is there any other requirements?
0
 
LVL 5

Expert Comment

by:burningmace
ID: 24393824
You should create a tollerance for MouseMove. If the movement exceeds n pixels, exit the program.
    Private LastPos As Point = Form.MousePosition
 
    Private Sub frmMain_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        Dim diff As Point = Point.Subtract(LastPos, Form.MousePosition)
        If Math.Abs(diff.X) > 10 Or Math.Abs(diff.Y) > 10 Then
            Application.Exit()
        End If
    End Sub

Open in new window

0
 

Author Comment

by:srk1982
ID: 24408548
Hi burningmace,

        I am getting the following error if i use ur code...

Error      1      The best overloaded method match for 'System.Drawing.Point.Subtract(System.Drawing.Point, System.Drawing.Size)' has some invalid arguments

Error      2      Argument '2': cannot convert from 'System.Drawing.Point' to 'System.Drawing.Size'
0
 

Author Comment

by:srk1982
ID: 24408557
Hi iHadi,

     Yes you are right. But when i write the mousemove event, the form is exiting without an mouse movement. wierd is'nt it !!!?
0
 

Author Comment

by:srk1982
ID: 24409799
SOMEONE PLEASE HELP ME ON THIS...
ITS URGENT
0
 
LVL 5

Accepted Solution

by:
burningmace earned 500 total points
ID: 24410799
Sorry, my code was bad. Forgot that the second parameter was a size and not a point. Do it this way:
    Private LastPos As Point = Form.MousePosition
 
    Private Sub frmMain_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
	Dim x_diff As Integer = Math.Abs(LastPos.X - Form.MousePosition.X)
	Dim y_diff As Integer = Math.Abs(LastPos.Y - Form.MousePosition.Y)
        If x_diff > 10 Or y_diff > 10 Then
            Application.Exit()
        End If
    End Sub

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ComboBox to String Not Working 5 42
c#  FTP ftpwebrequest URI invalid 6 39
Run software updates from the website 6 46
ASP.NET VB code for check and insert in a table 4 39
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question