Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I have a simple form application which has a webbrowser control in it.
Is there a way to get an event or notification when the mouse cursor changes, when moved over the content in the webbrowser control? If yes, please give me some pointers.

PS: I don't want to parse the html document under the current mouse position and then see if it is a href etc.



Also, I am trying to understand how the mouse cursor change happens. As we have seen, when we move the mouse over a URL it changes to a hand symbol, when move it to a search tool box, it changes to a vertical line symbol (I).

I am wondering about the process or flow behind it. Like somebody notifies the OS that cursor has to change now, and then OS in turn notifies the Mouse Driver etc. But I am not very much sure about this flow. If anybody can throw some light on this is really appreciated.

Please let me know if it is not the right forum for this question.

Thanks in advance.
Posted

A WinForm application has a 'message pump' activated when the 'Main static method in the 'Project class is run, and launches a Form.

So, all 'messages,' like MousePosition, KeyDown/Up/Press, are routed to the running Form as long as it has 'focus.'

Many WinForms Controls have default settings for the Cursor when the Mouse enters certain areas: like a TextBox: the default behavior when you move the Mouse inside the area where you can enter Text is to change to a text-insertion cursor.

All WinForms objects that descend from 'Control expose a 'CursorChanged' event. By writing an event handler for that event and connecting it to a WinForm Control (Form, Control, UserControl), you can change that behavior, or read-out the current value that reflects the visual appearance of the Cursor.

In the case of the WebBrowser Control, however, even though the 'CursorChanged' event does appear in the list Property Browser, if you define a handler for that event, you'll get a compile-time error ... I suspect you already have found this, and that's why you are asking.

You can 'get away with' writing over-rides of the WebBrowser CursorChanged Event if you define a class that inherits from WebBrowser, and there will be no compile-time error, and no run-time error, but, at run-time you'll find they are never called.

If you want to pursue this, I think you'll have to get into the 'dark world' of using the MSHTML .dll, and over-riding WebBrowser events: see Microsoft[^] for an example using .NET 3.5.

I have never tried to see if using MSHTML will let you get notifications of the 'CursorChanged' event. You certainly can get a 'MouseDown' event, though, using this technique, and my guess is that if you can get a MouseDown event intercepted you could read the current value of the Cursor ... that's a hypothesis !

good luck, Bill
 
Share this answer
 
Comments
HalliHaida 11-Aug-11 1:34am    
Thanks for the information Bill.

I am still not getting the process on flow of events in changing the cursor. Though the controls have default settings, how does the mouse the pointer get changed from let's say arrow-head cursor to text-insertion cursor as soon as it moves into the text box area. What all events/things happen behind in changin this cursor? I am trying to understand this out of curiosity.
I think you may be 'closer' to understanding the dynamics of how cursor and mouse are handled in WinForms than you may think.

Try putting a TextBox on a WinForm, and then changing the 'Cursor property from the default (I-Beam) to some other value: now run the WinForm application, and observe that the cursor changes, but you can still enter text in the TextBox. And then, think about what must have happened 'behind the scenes' for the cursor to have automatically changed.

Both mouse movement, and keyboard events which, after all, originate from external devices, are handled at a 'lower-level' in Windows ... they have to be for responsiveness ... using hardware interrupts. So the Windows OS receives these messages with a high-level of priority for handling.

Every instance of a WinForm Control you create, at Design-Time or Run-Time, will register itself automatically to receive certain events from the 'message-pump' of the WinForm Application which routes events it 'receives' from the Windows OS .

When you 'hook-up' an EventHandler for a Control in your code, you essentially subscribe to the WinForm application's message-pump's messages for that Control which the control publishes.

WinForms Controls vary in the extent to which they expose Events, directly just by creating an instance of them, and in whether or not you can, via sub-classing, over-ride certain events, methods, etc., that are not exposed by creating an instance.

In the case of using the WebBrowser Control in WinForms, there are special issues relating to the fact that what you are interacting with, as a programmer, is a 'wrapper' over a very complex software entity, InternetExplorer, that has its own Window Management and Message Pump system going on, and that is constrained, for security reasons, in many ways.

Suggestions:

1. see the Visual Studio documentation for these topics

a. How Keyboard Input Works
b. WndProc
c. PreProcessMessage
d. KeyBoard Input
e. Mouse Input in a Windows Forms Application
f. Mouse Pointers in Windows Forms
g. How Mouse Input Works in Windows Forms
h. Control.MousePosition

2. WebBrowser docs

a. WebBrowser Customization I[^]

b. WebBrowser Customization II[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900