On Tue, 05 Jun 2007 12:03:26 -0700, Adrian < <x@xx.xxwrote:
>you should be able to do what you want by
overriding the OnMouse... methods() in the control. It does mean you
have
to create your own custom control that inherits from TextBox, but it
should work fine. If you want to disable all mouse interaction, then
simply write the overrides and then do nothing in them. If you just
want
to disable selection, then you may be able to do that by making the
OnMouseDown() method do nothing, and then in OnMouseUp() call both the
base OnMouseDown() method and the base OnMouseUp() method with the same
event args (so that it looks to the base class as if the mouse was
clicked
in a single spot).
[...]
It didn't work I'm afraid to say.
Sorry about that. The theory was sound, but it turns out (unbeknownst to
me) that the TextBox class does not actually do its real work in the
OnMouse... methods. After thinking about it a moment, I realize that this
may be because the TextBox class doesn't really implement most of the
behavior, but instead just uses the built-in Windows text edit control.
With that in mind, I looked to see if you could accomplish the same thing
at a lower level, and lo and behold you can. Here is some code that does
basically what I was talking about (it goes in the control class derived
from TextBox, and you need to use the derived class as the control in your
form):
const int WM_MOUSEMOVE = 0x0200;
const int WM_LBUTTONDOWN = 0x0201;
const int WM_LBUTTONUP = 0x0202;
protected override void WndProc(ref Message m)
{
bool fHandled = false;
switch (m.Msg)
{
case WM_LBUTTONDOWN:
// Ignore the left mouse button down event
fHandled = true;
break;
case WM_MOUSEMOVE:
// Ignore any mouse move events
fHandled = true;
break;
case WM_LBUTTONUP:
// Before we process the left mouse button up event,
// send a left mouse button down event with the same
// mouse location as the mouse up event, to simulate
// an actual click at the location.
Message mT = m; // copy the current message
mT.Msg = WM_LBUTTONDOWN; // change the message ID
base.WndProc(ref mT); // send the simulated message
break;
}
if (!fHandled)
{
base.WndProc(ref m);
}
}
Because this version of the implementation operates on the underlying
Windows messages themselves, it means that even when the text selection is
being handled by a non-.NET piece of code, the mouse messages are still
intercepted before that code handles them.
Note that the above disables selection, but allows the user to reposition
the caret. If you want no mouse interaction at all, just set "fHandled"
in the WM_LBUTTONUP case as well, and take out the code that's in that
case now.
Also note that the above is really just proof-of-concept. I have not
verified that hiding the mouse messages like this does not also result in
some other funny behavior, that may or may not be desired. You should
think about the exact behavior and do thorough testing before using
anything like this in real-world code (especially since I did neither :)).
Hope that helps.
Pete