Solved

How to sense mouseover across a web control in C# program

Posted on 2010-09-01
23
735 Views
Last Modified: 2012-05-10
Experts:

I am developing a C# program to replace an existing tool currently in production.  One of the requirements I have is to perform a refresh "keepalive" on a website loaded inside the Web Browser control.  To accomplish this, I've creaed a timer to refresh the page every 3 minutes.

Along with the timer, I want to sense whether the mouse is inside the Web Browser Control indicating the user is most likely active, and therefore stopping the timer (starting the timer again when the mouse leaves the control).

I tried inserting a panel, putting the Web control inside the panel, and assigning mouseEnter and mouseLeave events, but the web control (even though inside the Panel object) does not appear to be impacted by these settings.

Anyone have an idea how I can accomplish this?
0
Comment
Question by:piattnd
  • 14
  • 7
  • 2
23 Comments
 
LVL 1

Expert Comment

by:tomlau
ID: 33582272
This might be a long shot but have you tried putting onMouseOut in the body tag?
0
 
LVL 12

Author Comment

by:piattnd
ID: 33582462
the onMouseOut event doesn't exist in C#, only MouseEnter and MouseLeave.  I can apply that tag to the main form, but that only applies to areas where the form itself is exposed with no buttons or other controls over top of it.
0
 
LVL 1

Expert Comment

by:tomlau
ID: 33582664
I was assuming that you are setting the timer on the client side and make it easy by capturing the whole body area with the onmouseout event.  I just tried this and works:

<html>
<html>
<body onmouseout="alert('hello')">
</body>
</html>

You can set the body with run as server and add that as an attribute.
0
 
LVL 6

Expert Comment

by:r3nder
ID: 33582689
try this - use an object like text - or what ever is the largest object in your page - or in JavaScript with this use a "Hotspot" the size of the page
private void txt1_MouseHover(object sender, EventArgs e)

{

//ur code..

}
0
 
LVL 12

Author Comment

by:piattnd
ID: 33583999
Let me clarify something:

This is a C# application, not a WPF or website or HTML code, all C#.
0
 
LVL 6

Expert Comment

by:r3nder
ID: 33586607
Then use this - this will cover the entire c# form - not just the web browser control

private void SetCock()



        {



            Point mousePointer = this.PointToClient(Cursor.Position);



            if (this.ClientRectangle.Contains(mousePointer))



            {



                //ur code;



            }



            else



            {



                //ur code;

            }



        }



//and this for the mouse function



 private void MainForm_MouseEnter(object sender, EventArgs e)



        {



            this.SetClock();



        }



 



        /// <summary>



        /// 



        /// </summary>



        /// <param name="sender"></param>



        /// <param name="e"></param>



        private void MainForm_MouseLeave(object sender, EventArgs e)



        {



            this.SetClock();

            //actually you will want to stop start pause whatever



        }

Open in new window

0
 
LVL 12

Author Comment

by:piattnd
ID: 33590913
I've already tried MouseEnter and MouseLeave on the main form.  It only impacts areas of which you have no content other than the main form.

For example, say you have a button in the middle of the form.  If you scroll over that button, it will think you've scrolled out of (or mouseleave) the main form.
0
 
LVL 6

Expert Comment

by:r3nder
ID: 33591035
I use this to set the opacity on my form - if mouse out -form is set to .4% opacity on over it sets the opacity to 1.0 - works great for me - send me a snippet of the timer code and I will check it and send it back with what I find oh and sorry for the typo
0
 
LVL 12

Author Comment

by:piattnd
ID: 33591308
Below is the snippet for my timer tick event, stop and start timers.  Timer should stop/reset when mouse moves into the window and start when it moves out of the window.

The Web Browser control doesn't have the "MouseEnter" or "MouseLeave" events, so for now I've drawn a panel the same size as the Web Browser control, put the Web Browser control inside the Panel, and set the MouseEnter and MouseLeave events on the panel.
private void refresh_timer_Tick(object sender, EventArgs e)

        {

            webBrowser1.Refresh();

        }



        private void stop_refresh(object sender, EventArgs e)

        {

            Timer_input.Text = "stopped";

            refresh_timer.Stop();

            refresh_timer.Enabled = false;

        }



        private void start_refresh(object sender, EventArgs e)

        {

            Timer_input.Text = "started";

            refresh_timer.Enabled = true;

            refresh_timer.Start();

        }

Open in new window

0
 
LVL 12

Author Comment

by:piattnd
ID: 33591334
Here is the snippet for the Panel and Web control elements:
// 

            // panel1

            // 

            this.panel1.BackColor = System.Drawing.Color.Transparent;

            this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;

            this.panel1.Controls.Add(this.webBrowser1);

            this.panel1.ForeColor = System.Drawing.Color.Transparent;

            this.panel1.Location = new System.Drawing.Point(0, 0);

            this.panel1.Margin = new System.Windows.Forms.Padding(0);

            this.panel1.Name = "panel1";

            this.panel1.Size = new System.Drawing.Size(935, 592);

            this.panel1.TabIndex = 9;

            this.panel1.MouseLeave += new System.EventHandler(this.start_refresh);

            this.panel1.MouseEnter += new System.EventHandler(this.stop_refresh);

            // 

            // webBrowser1

            // 

            this.webBrowser1.IsWebBrowserContextMenuEnabled = false;

            this.webBrowser1.Location = new System.Drawing.Point(-2, -2);

            this.webBrowser1.Margin = new System.Windows.Forms.Padding(0);

            this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);

            this.webBrowser1.Name = "webBrowser1";

            this.webBrowser1.ScrollBarsEnabled = false;

            this.webBrowser1.Size = new System.Drawing.Size(935, 592);

            this.webBrowser1.TabIndex = 11;

            this.webBrowser1.Url = new System.Uri("https://someurl.com", System.UriKind.Absolute);

            this.webBrowser1.WebBrowserShortcutsEnabled = false;

Open in new window

0
 
LVL 12

Author Comment

by:piattnd
ID: 33591483
Oh, and FYI if I put the panel over top of the web browser, all the code works properly, but I can't see the web browser window.  If I put the web browser window inside the panel with the mouseenter mouseleave events, the events aren't picked up.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Expert Comment

by:r3nder
ID: 33593648
OK - here is the code and a visual refence to tell them (mentally) that the app is not in use.
reinitialize you ticker and add the code for the ticker it should do you
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;



namespace ClockandOpacity

{

    public partial class MainForm : Form

    {

        public MainForm()

        {

            InitializeComponent();

            SetOpacity();

            Init();

            

        }

        private void Init()

        {

            // Align the form to Top-Right corner

            this.Left = Screen.FromControl(this).WorkingArea.Width - this.Width;

            this.Top = 0;

        }

        private void SetOpacity()

        {

            Point mousePointer = this.PointToClient(Cursor.Position);



            if (this.ClientRectangle.Contains(mousePointer))

            {

                this.Opacity = 1.0;

               // Timer_input.Text = "stopped";

               // refresh_timer.Stop();

               // refresh_timer.Enabled = false;



            }

            else

            {

                this.Opacity = 0.4;

              //  TTimer_input.Text = "started";

              //  refresh_timer.Enabled = true;

              //  refresh_timer.Start();





            }

        }

        

        private void MainForm_MouseEnter(object sender, EventArgs e)

        {

            this.SetOpacity();

        }

        private void MainForm_MouseLeave(object sender, EventArgs e)

        {

            this.SetOpacity();

        }



        

    }

}



//in your form1.esigner.cs add this to the initialize private void InitializeComponent()

this.MouseEnter += new System.EventHandler(this.MainForm_MouseEnter);

            this.MouseLeave += new System.EventHandler(this.MainForm_MouseLeave);

Open in new window

0
 
LVL 12

Author Comment

by:piattnd
ID: 33598321
Thanks.  I'll give this a shot (hopefully here soon) and get back to you.
0
 
LVL 12

Author Comment

by:piattnd
ID: 33599840
No luck.  The web control and buttons still make the program thing you've scrolled out of the area.
0
 
LVL 12

Author Comment

by:piattnd
ID: 33599940
I tested it without the web control in there and it works like a charm, so I know the code works, but with the web control in there, it treats it like it's outside the program area :(
0
 
LVL 6

Expert Comment

by:r3nder
ID: 33599994
I am sending you a copy of one of my programs - look at how I leave space just outside of the main area of focus and make it so when the user goes to the app or leaves it they have to drag the mouse over an empty area
Skynet.Windows.Canoogle.exe
0
 
LVL 12

Author Comment

by:piattnd
ID: 33600127
Ok,  yeah I see what you're saying and I think it will work.  I'm going to test it now.
0
 
LVL 12

Author Comment

by:piattnd
ID: 33600167
Alright, I did what you said with the small border, however with 1 pixel around the outside, fast movement of the mouse over that area doesn't always trigger the mouse enter event.  How many pixels of a border do you have?
0
 
LVL 12

Author Comment

by:piattnd
ID: 33600192
I just confirmed yours does the same thing, so I guess that's just a downside of doing it that way. :-/
0
 
LVL 6

Expert Comment

by:r3nder
ID: 33600196
use the bottom of the example as a reference (after selecting browser) and your favorites are visible looks like 5-10px
0
 
LVL 12

Author Comment

by:piattnd
ID: 33600215
Ok, I'll do a colored border or something and see if it works better.  Almost ready to close this one out, thakns for your help! :)
0
 
LVL 6

Accepted Solution

by:
r3nder earned 500 total points
ID: 33600235
I hope everything else goes well in the app :) have a great weekend
0
 
LVL 12

Author Closing Comment

by:piattnd
ID: 33600326
Thanks a lot for your help.  I'm going to look into using the windows APIs for mouse detection to see if I get any better results, but thank you for the immediate solution!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now