How can I trap all mouse events on a control? - c#

I need to capture all mouse events on a .NET's WebBrowser, process them and prevent the WebBrowser from getting them. Is there any way to achieve this? I wonder if there is any way I can handle mouse events if the control is disabled.

You'll have to override WndProc() to intercept the mouse messages. Like this:
using System;
using System.Windows.Forms;
class MyBrowser : WebBrowser {
protected override void WndProc(ref Message m) {
if (m.Msg >= 0x200 && m.Msg <= 0x20a) {
// Handle mouse messages
//...
}
else base.WndProc(ref m);
}
}

There IS a solution to this. You need to capture the mouse events that are associated with the Document object that is associated with the webBrowser control.
After the DocumentCompleted event occurs, and inside you DocumentCompleted event handler, do the following:
myWebBrowser.Document.MouseDown += new HtmlElementEventHandler(myMouseDown);
and have the related handler:
void myMouseDown(object sender, HtmlElementEventArgs e)
{
your code to handle the mouse event... such as ...
if (e.MouseButtonsPressed == MouseButtons.Right)
{
}
}

Related

How to pass a MouseEventArgs with correct properties into event handlers?

I have a program which has a FlowLayoutPanel inside its Form. I'm coding on the FlowLayoutPanel's MouseMove event and everything works fine; except that it doesn't fire the MouseMove event while the cursor moves on its scroll bar.
I searched the web and found the following approach which uses a derived class from FlowLayoutPanel and then overrides its WndProc method:
class FlowLayoutPanelEx : FlowLayoutPanel
{
const int WM_NCMOUSEMOVE = 0x00A0;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_NCMOUSEMOVE)
{
base.OnMouseMove(null);
}
base.WndProc(ref m);
}
}
I'm having problem with raising (or calling) the base.OnMouseMove(). It requires a MouseEventArgs object to be passed in as parameter. I need to pass the correct MouseEventArgs because my event handler relies on it, but I don't know how to set/where to get the right properties (Buttons, X, Y, etc.) to pass the correct MouseMoveEvent.
Any help will be appreciated.
You can detect the mouse moving across the scrollbar by intercepting the WM_NCMOUSEMOVE message. Like this:
protected override void WndProc(ref Message m) {
if (m.Msg == 0xA0) { // WM_NCMOUSEMOVE.
var pos = this.PointToClient(new Point(m.LParam.ToInt32()));
var evt = new MouseEventArgs(Control.MouseButtons, 0, pos.X, pos.Y, 0);
OnMouseMove(evt);
}
base.WndProc(ref m);
}

How can I disable the Caps Lock warning with a password control?

I get this when I have Caps Lock on with a password control in focus. I would like to add my own warning instead. How can I disable this one? I don't mind P/Invoke or any native code but it has to be in C#.
In your form, override WndProc like so, which will intercept the EM_SHOWBALOONTIP message and prevent the control from receiving it:
protected override void WndProc(ref Message m)
{
if (m.Msg != 0x1503) //EM_SHOWBALOONTIP
base.WndProc(ref m);
}
The following code works for me, on the KeyDown event of a TextBox:
private void txtPassword_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.CapsLock)
{
e.SuppressKeyPress = true;
}
}

How to detect if the Control.Click event was by the mouse, keyboard, or something else?

How can I tell if a Control.Click event was triggered by the mouse or by the keyboard?
Edit:
Handling MouseClick and KeyPress does't work for me, because then how would I know if something else triggered the click? (e.g. PerformClick)
You can't. Use the Control.MouseClick event and the Control.KeyPress event so you can tell the source of the event. And remember that a space on the control with focus and a Ctrl+ key can generate a click on a button as well.
You can not tell, but you can use MouseClick and KeyPress if you need to know what originated the event.
void handler(object sender, EventArgs e)
{
bool mouseEvent = (e is MouseEventArgs);
bool keyEvent = (e is KeyEventArgs);
bool performClick = (e is EventArgs) && !keyEvent && !mouseEvent;
}

How do I implement a double-right-click for winforms?

This question seems to point to the existence of a windows event for a double-right-click. How to implement it in a C# windows form, however, is less than clear.
What's the best way to implement double-right-click on a control such as a button?
(I'm thinking I must use MouseDown and keep track of the time between clicks. Is there a better way?)
Override the WndProc function and listen for WM_RBUTTONDBLCLK, which as can be seen on this pinvoke page is 0x0206.
That pinvoke page also has some C# sample code for how to do it.
Whenever you see something about a windows message and/or windows API and you want to use it in C#, the pinvoke site is a good place to start looking.
Override Control.WndProc, and handle the WM_RBUTTONDBLCLK message manually.
MouseEventArgs contain property 'Button' that indicate wich button was clicked. So you can simply check this:
private void MouseDoubleClickEventHandler(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
DoSomthing();
} else if (e.Button == MouseButtons.Right)
{
DoSomethingElse();
}
}
I was able to implement this by inheriting from a button and overriding WndProc as ho1 and Reed suggested. Here's the inherited button:
public class RButton : Button
{
public delegate void MouseDoubleRightClick(object sender, MouseEventArgs e);
public event MouseDoubleRightClick DoubleRightClick;
protected override void WndProc(ref Message m)
{
const Int32 WM_RBUTTONDBLCLK = 0x0206;
if (m.Msg == WM_RBUTTONDBLCLK)
DoubleRightClick(this, null);
base.WndProc(ref m);
}
}
I added the button programatically to the form and subscribed to its new DoubleRightClick event. I'm not sure how exactly to generate the appropriate MouseEventArgs but since this is just a test case, it's not important.

RichTextBox & Disabling Mouse Scrolling

I want to use the mouse middle button to clear a RichTextBox, but it also activates a mouse scrolling functionality similar to what you find in web broswers. When the vertical scrollbar is visible (there's enough data) and you press the middle button in the mouse a scrolling cursor appears and you can scroll up or down by moving the cursor up or down. How do I disable the mouse scrolling?
Mouse scrolling seems to be a Windows (or mouse driver) feature, so how can I stop the MouseDown event (if the mouse middle button is pressed) from reaching whatever routine is responsible for the mouse scrolling?
Check for 0x207 and 0x208, middle button down and up:
using System;
using System.Windows.Forms;
class MyRtb : RichTextBox {
protected override void WndProc(ref Message m) {
if (m.Msg == 0x207) this.Clear();
else if (m.Msg != 0x208) base.WndProc(ref m);
}
}
No scrolling RichTextBox, just Inherit from RichTextBox and you done.
public class NoScrollRichTextBox : RichTextBox
{
const int WM_MOUSEWHEEL = 0x020A;
protected override void WndProc(ref Message m)
{
// This will completely ignore the mouse wheel, which will disable zooming as well
if (m.Msg != WM_MOUSEWHEEL)
base.WndProc(ref m);
}
}

Categories

Resources