Solved

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

Posted on 2010-09-01
23
722 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

759 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

18 Experts available now in Live!

Get 1:1 Help Now