Link to home
Start Free TrialLog in
Avatar of piattnd
piattnd

asked on

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

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?
Avatar of tomlau
tomlau

This might be a long shot but have you tried putting onMouseOut in the body tag?
Avatar of piattnd

ASKER

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.
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.
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..

}
Avatar of piattnd

ASKER

Let me clarify something:

This is a C# application, not a WPF or website or HTML code, all C#.
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

Avatar of piattnd

ASKER

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.
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
Avatar of piattnd

ASKER

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

Avatar of piattnd

ASKER

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

Avatar of piattnd

ASKER

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.
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

Avatar of piattnd

ASKER

Thanks.  I'll give this a shot (hopefully here soon) and get back to you.
Avatar of piattnd

ASKER

No luck.  The web control and buttons still make the program thing you've scrolled out of the area.
Avatar of piattnd

ASKER

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 :(
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
Avatar of piattnd

ASKER

Ok,  yeah I see what you're saying and I think it will work.  I'm going to test it now.
Avatar of piattnd

ASKER

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?
Avatar of piattnd

ASKER

I just confirmed yours does the same thing, so I guess that's just a downside of doing it that way. :-/
use the bottom of the example as a reference (after selecting browser) and your favorites are visible looks like 5-10px
Avatar of piattnd

ASKER

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! :)
ASKER CERTIFIED SOLUTION
Avatar of r3nder
r3nder
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of piattnd

ASKER

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!