Link to home
Start Free TrialLog in
Avatar of srtindall
srtindall

asked on

c# code to change mouse cursor from pointer to "hand" shape on Safari

does anyone have the
c# code to change mouse cursor from pointer to "hand" shape on Safari
Avatar of khan_webguru
khan_webguru
Flag of Australia image

With .NET you can easily change the current cursor by using the Cursors class. To change the cursor to a hourglass, we'll be using the WaitCursor:

 
this.Cursor = Cursors.WaitCursor;

Open in new window



To change it back to normal after the application has stopped loading:

 
this.Cursor = Cursors.Default;

Open in new window


Hope this will help you

Regards,

Asif Ahmed Khan
ASKER CERTIFIED SOLUTION
Avatar of khan_webguru
khan_webguru
Flag of Australia 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 Obadiah Christopher
My guess is this is an ASP.Net app. And if tht is the case I
Don't think these soln work. Is this a asp.net app?
I think u have 2 use css

.mousecursor{
Cursor:hand;
}
Well I went under the notion that it was a Windows application. However if this is asp.net, then this is not the answer.

Monitor cursor movement and flag when it goes over Safari:
    //Requires reference to System.Drawing
    public class MouseMonitor
    {
        [DllImport("user32.dll")]
        static extern IntPtr WindowFromPoint(POINT Point);
        [DllImport("user32.dll")]
        static extern bool GetCursorPos(out POINT lpPoint);
        [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
        static extern IntPtr GetParent(IntPtr hWnd);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern int GetWindowThreadProcessId(IntPtr handle, out uint processId);

        [StructLayout(LayoutKind.Sequential)]
        public struct POINT
        {
            public int X;
            public int Y;

            public POINT(int x, int y)
            {
                this.X = x;
                this.Y = y;
            }

            public static implicit operator Point(POINT p)
            {
                return new Point(p.X, p.Y);
            }

            public static implicit operator POINT(Point p)
            {
                return new POINT(p.X, p.Y);
            }
        }

        public class MouseFoundOverApplicationEventArgs : EventArgs 
        {
            IntPtr windowHandle;

            public MouseFoundOverApplicationEventArgs(IntPtr windowHandle)
            {
                this.windowHandle = windowHandle;
            }

            public IntPtr WindowHandle
            {
                get { return this.windowHandle; }
            }
        }

        public delegate void MouseFoundOverApplicationEventHandler(object sender, MouseFoundOverApplicationEventArgs e);

        public event MouseFoundOverApplicationEventHandler MouseFoundOverApplication;

        Timer timer = new Timer();
        string applicationName;

        public MouseMonitor(string applicationName)
        {
            this.timer = new Timer();
            this.timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            this.applicationName = applicationName;
        }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            IntPtr windowHandle = MouseMonitor.GetHandleOfWindow();
            if (MouseMonitor.WindowMatchesApplication(windowHandle, applicationName))
                this.OnMouseFoundOverApplication(new MouseFoundOverApplicationEventArgs(windowHandle));
            this.timer.Start();
        }

        public void Start(double interval = 500)
        {

            this.timer.AutoReset = false;
            this.timer.Interval = interval;
            this.timer.Start();
        }

        public void Stop()
        {
            if (this.timer.Enabled)
                this.timer.Stop();
        }

        protected void OnMouseFoundOverApplication(MouseFoundOverApplicationEventArgs e)
        {
            if (this.MouseFoundOverApplication != null)
                this.MouseFoundOverApplication(this, e);
        }


        private static IntPtr GetHandleOfWindow()
        {
            POINT location;
            MouseMonitor.GetCursorPos(out location);

            IntPtr window = MouseMonitor.WindowFromPoint(location);
            IntPtr windowParent = IntPtr.Zero;

            while (window != IntPtr.Zero)
            {
                windowParent = window;
                window = MouseMonitor.GetParent(window);
            }

            return windowParent;
        }

        private static bool WindowMatchesApplication(IntPtr windowHandle, string applicationName)
        {
            uint processID = 0;
            MouseMonitor.GetWindowThreadProcessId(windowHandle, out processID);
            Process process = Process.GetProcessById((int)processID);

            string executableName = Path.GetFileName(process.MainModule.FileName);
            return (applicationName.ToLower() == executableName.ToLower());
        }
    }

Open in new window


Then to start the monitor:
            this.monitor = new MouseMonitor("safari.exe");
            this.monitor.MouseFoundOverApplication += new MouseMonitor.MouseFoundOverApplicationEventHandler(monitor_MouseFoundOverApplication);
            this.monitor.Start();

Open in new window


Then to catch the event:
        void monitor_MouseFoundOverApplication(object sender, MouseMonitor.MouseFoundOverApplicationEventArgs e)
        {
            //TODO Add code to change cursor
        }

Open in new window


If this is the path you were going and need help finishing the change cursor, let me know. The information on changing the cursor is at: http://msdn.microsoft.com/en-us/library/ms648393%28VS.85%29.aspx
Hello Bro,

Also look into this

 
HCURSOR WINAPI SetCursor(
  __in_opt  HCURSOR hCursor
);

Open in new window


Regards,

Asif Ahmed Khan