Home All Groups Group Topic Archive Search About

Block WM_MOUSEACTIVATE messages

Author
27 Nov 2006 10:14 AM
aimoz@intechinfo.fr
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.

Author
27 Nov 2006 11:50 AM
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.


Show quote
"ai***@intechinfo.fr" <aimozintechinf***@discussions.microsoft.com> wrote in
message news:19D575EF-98A4-4E8D-90C0-75745ACB53EB@microsoft.com...
> 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.
Author
27 Nov 2006 3:35 PM
aimoz@intechinfo.fr
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.
Author
27 Nov 2006 3:47 PM
Ciaran O''Donnell
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

Show quote
"ai***@intechinfo.fr" wrote:

> 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.
Author
28 Nov 2006 9:04 AM
aimoz@intechinfo.fr
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.
Author
28 Nov 2006 11:46 AM
Kevin Spencer
Here's the complete Windows API Mouse Input reference:

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

Also, a nice web site about using pinvoke:

http://www.pinvoke.net/index.aspx

--
HTH,

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

Never trust a dunderhead with a blunderbuss.


Show quote
"ai***@intechinfo.fr" <aimozintechinf***@discussions.microsoft.com> wrote in
message news:E9A009B8-4692-44F4-A12B-6E935B52F99E@microsoft.com...
> 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.
Author
29 Nov 2006 8:48 AM
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

Show quote
> 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
>
Author
3 Dec 2006 2:10 PM
aimoz@intechinfo.fr
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.

AddThis Social Bookmark Button