Block WM_MOUSEACTIVATE messages

G

Guest

Hello,

I am developping an application (using C#) and I need, whenever the user
click on the form, this one not to be activated. (the system mustn't give the
focus to the form).

After several search I saw a way : overriding the WndProc (wich belongs to
form class) and replace the WM_MOUSEACTIVATE message with WM_NULL message.
Here is the sample of code :

//Override wndProc method
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_MOUSEACTIVATE)
{
m = Message.Create(new IntPtr(0), 0, new IntPtr(0), new
IntPtr(0));
}

base.WndProc(ref m);
}

But this doesn't work, am I in the wrong way, is there any other messages... ?

Thanks for your answer.
 
K

Kevin Spencer

Check out the following article on the WM_MOUSEACTIVATE message. According
to this article, you need to return one of the following return values:

MA_ACTIVATE Activates the window, and does not discard the mouse message.
MA_ACTIVATEANDEAT Activates the window, and discards the mouse message.
MA_NOACTIVATE Does not activate the window, and does not discard the mouse
message.
MA_NOACTIVATEANDEAT Does not activate the window, but discards the mouse
message.

http://msdn2.microsoft.com/en-us/library/ms645612.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Virtual Carpenter
http://unclechutney.blogspot.com

Never trust a dunderhead with a blunderbuss.
 
G

Guest

Thanks for your answer, but i've already saw this article and i don't
understand what is the return value of a notification. Do you have a sample
of code ?

Thanks in advance.
 
G

Guest

Try this:
internal const uint WM_MOUSEACTIVATE = 0x21;
internal const uint MA_NOACTIVATEANDEAT = 4;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg == WM_MOUSEACTIVATE)
{
m.Result = (IntPtr)MA_NOACTIVATEANDEAT;
}else
base.WndProc(ref m);
}

HTH

Ciaran O'Donnell
 
G

Guest

I've tried your code but it's the same, the window get the focus after a click.
Is there another message which activate the window ?

Thanks in advance.
 
B

b6s

Basically MA_NOACTIVATEANDEAT or MA_NOACTIVATE are what you need, but
notice that the choices are different between forms and controls.

To my knowledge, if the form is expected to be always inactivated but
still functional, for example, dragging to move, MA_NOACTIVATE is
required.

For a control such as the button, however, MA_NOACTIVATEANDEAT is
better because MA_NOACTIVATE still propagate WM_NCACTIVATE and
WM_ACTIVATE to its parent container.

It is really a bad news that MA_NOACTIVATEANDEAT will also disable some
mouse down/click event like "pushed button" visual effect.

/Mike T. J. Jiang/

Ciaran O''Donnell ¼g¹D¡G
 
G

Guest

I choose another way because there are other problems that this method can't
deal with.
Thank for your answer, i've learnd many things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top